50dcc40c6edaff1a45d885c7c0dd5d531efe3ae6
[poolifier.git] / benchmarks / benchmarks-utils.js
1 const fs = require('fs')
2 const {
3 PoolTypes,
4 WorkerFunctions,
5 WorkerTypes
6 } = require('./benchmarks-types')
7 const {
8 DynamicClusterPool,
9 DynamicThreadPool,
10 FixedClusterPool,
11 FixedThreadPool
12 } = require('../lib')
13
14 async function runTest (pool, { taskExecutions, workerData }) {
15 return new Promise((resolve, reject) => {
16 let executions = 0
17 for (let i = 1; i <= taskExecutions; i++) {
18 pool
19 .execute(workerData)
20 .then(() => {
21 ++executions
22 if (executions === taskExecutions) {
23 return resolve({ ok: 1 })
24 }
25 return null
26 })
27 .catch(err => {
28 console.error(err)
29 return reject(err)
30 })
31 }
32 })
33 }
34
35 function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
36 if (max < min || max < 0 || min < 0) {
37 throw new RangeError('Invalid interval')
38 }
39 max = Math.floor(max)
40 if (min != null && min !== 0) {
41 min = Math.ceil(min)
42 return Math.floor(Math.random() * (max - min + 1)) + min
43 }
44 return Math.floor(Math.random() * (max + 1))
45 }
46
47 function jsonIntegerSerialization (n) {
48 for (let i = 0; i < n; i++) {
49 const o = {
50 a: i
51 }
52 JSON.stringify(o)
53 }
54 }
55
56 /**
57 * Intentionally inefficient implementation.
58 *
59 * @param {number} n - The number of fibonacci numbers to generate.
60 * @returns {number} - The nth fibonacci number.
61 */
62 function fibonacci (n) {
63 if (n <= 1) return 1
64 return fibonacci(n - 1) + fibonacci(n - 2)
65 }
66
67 /**
68 * Intentionally inefficient implementation.
69 *
70 * @param {number} n - The number to calculate the factorial of.
71 * @returns {number} - The factorial of n.
72 */
73 function factorial (n) {
74 if (n === 0) {
75 return 1
76 }
77 return factorial(n - 1) * n
78 }
79
80 function readWriteFiles (n) {
81 const baseDirectory = '/tmp/poolifier-benchmarks'
82 if (fs.existsSync(baseDirectory) === false) {
83 fs.mkdirSync(baseDirectory, { recursive: true })
84 }
85 for (let i = 0; i < n; i++) {
86 const filePath = `${baseDirectory}/${i}`
87 fs.writeFileSync(filePath, i.toString(), {
88 encoding: 'utf8',
89 flag: 'a'
90 })
91 fs.readFileSync(filePath, 'utf8')
92 }
93 }
94
95 function executeWorkerFunction (data) {
96 switch (data.function) {
97 case WorkerFunctions.jsonIntegerSerialization:
98 return jsonIntegerSerialization(data.taskSize || 1000)
99 case WorkerFunctions.fibonacci:
100 return fibonacci(data.taskSize || 1000)
101 case WorkerFunctions.factorial:
102 return factorial(data.taskSize || 1000)
103 case WorkerFunctions.readWriteFiles:
104 return readWriteFiles(data.taskSize || 1000)
105 default:
106 throw new Error('Unknown worker function')
107 }
108 }
109
110 function buildPool (poolType, poolSize, workerType, poolOptions) {
111 switch (poolType) {
112 case PoolTypes.FIXED:
113 switch (workerType) {
114 case WorkerTypes.THREAD:
115 return new FixedThreadPool(
116 poolSize,
117 './benchmarks/internal/thread-worker.js',
118 poolOptions
119 )
120 case WorkerTypes.CLUSTER:
121 return new FixedClusterPool(
122 poolSize,
123 './benchmarks/internal/cluster-worker.js',
124 poolOptions
125 )
126 }
127 break
128 case PoolTypes.DYNAMIC:
129 switch (workerType) {
130 case WorkerTypes.THREAD:
131 return new DynamicThreadPool(
132 poolSize / 2,
133 poolSize * 3,
134 './benchmarks/internal/thread-worker.js',
135 poolOptions
136 )
137 case WorkerTypes.CLUSTER:
138 return new DynamicClusterPool(
139 poolSize / 2,
140 poolSize * 3,
141 './benchmarks/internal/cluster-worker.js',
142 poolOptions
143 )
144 }
145 break
146 }
147 }
148
149 module.exports = {
150 WorkerFunctions,
151 buildPool,
152 executeWorkerFunction,
153 generateRandomInteger,
154 readWriteFiles,
155 runTest
156 }