e94910984831770041715c33b301f6a68c4ec862
[poolifier.git] / benchmarks / benchmarks-utils.cjs
1 const { randomInt } = require('node:crypto')
2 const { strictEqual } = require('node:assert')
3 const {
4 existsSync,
5 mkdirSync,
6 readFileSync,
7 rmSync,
8 writeFileSync
9 } = require('node:fs')
10 const Benchmark = require('benchmark')
11 const {
12 DynamicClusterPool,
13 DynamicThreadPool,
14 FixedClusterPool,
15 FixedThreadPool,
16 Measurements,
17 PoolTypes,
18 WorkerChoiceStrategies,
19 WorkerTypes
20 } = require('../lib/index.cjs')
21 const { TaskFunctions } = require('./benchmarks-types.cjs')
22
23 const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
24 switch (poolType) {
25 case PoolTypes.fixed:
26 switch (workerType) {
27 case WorkerTypes.thread:
28 return new FixedThreadPool(
29 poolSize,
30 './benchmarks/internal/thread-worker.mjs',
31 poolOptions
32 )
33 case WorkerTypes.cluster:
34 return new FixedClusterPool(
35 poolSize,
36 './benchmarks/internal/cluster-worker.cjs',
37 poolOptions
38 )
39 }
40 break
41 case PoolTypes.dynamic:
42 switch (workerType) {
43 case WorkerTypes.thread:
44 return new DynamicThreadPool(
45 Math.floor(poolSize / 2),
46 poolSize,
47 './benchmarks/internal/thread-worker.mjs',
48 poolOptions
49 )
50 case WorkerTypes.cluster:
51 return new DynamicClusterPool(
52 Math.floor(poolSize / 2),
53 poolSize,
54 './benchmarks/internal/cluster-worker.cjs',
55 poolOptions
56 )
57 }
58 break
59 }
60 }
61
62 const runPoolifierPool = async (pool, { taskExecutions, workerData }) => {
63 return await new Promise((resolve, reject) => {
64 let executions = 0
65 for (let i = 1; i <= taskExecutions; i++) {
66 pool
67 .execute(workerData)
68 .then(() => {
69 ++executions
70 if (executions === taskExecutions) {
71 resolve({ ok: 1 })
72 }
73 return undefined
74 })
75 .catch(err => {
76 console.error(err)
77 reject(err)
78 })
79 }
80 })
81 }
82
83 const runPoolifierPoolBenchmark = async (
84 name,
85 workerType,
86 poolType,
87 poolSize,
88 { taskExecutions, workerData }
89 ) => {
90 return await new Promise((resolve, reject) => {
91 const pool = buildPoolifierPool(workerType, poolType, poolSize)
92 const suite = new Benchmark.Suite(name)
93 try {
94 for (const workerChoiceStrategy of Object.values(
95 WorkerChoiceStrategies
96 )) {
97 for (const enableTasksQueue of [false, true]) {
98 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
99 for (const measurement of [
100 Measurements.runTime,
101 Measurements.elu
102 ]) {
103 suite.add(
104 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
105 enableTasksQueue ? 'with' : 'without'
106 } tasks queue`,
107 async () => {
108 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
109 measurement
110 })
111 pool.enableTasksQueue(enableTasksQueue)
112 strictEqual(
113 pool.opts.workerChoiceStrategy,
114 workerChoiceStrategy
115 )
116 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
117 strictEqual(
118 pool.opts.workerChoiceStrategyOptions.measurement,
119 measurement
120 )
121 await runPoolifierPool(pool, {
122 taskExecutions,
123 workerData
124 })
125 }
126 )
127 }
128 } else {
129 suite.add(
130 `${name} with ${workerChoiceStrategy} and ${
131 enableTasksQueue ? 'with' : 'without'
132 } tasks queue`,
133 async () => {
134 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
135 pool.enableTasksQueue(enableTasksQueue)
136 strictEqual(
137 pool.opts.workerChoiceStrategy,
138 workerChoiceStrategy
139 )
140 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
141 await runPoolifierPool(pool, {
142 taskExecutions,
143 workerData
144 })
145 }
146 )
147 }
148 }
149 }
150 suite
151 .on('cycle', event => {
152 console.info(event.target.toString())
153 })
154 .on('complete', function () {
155 console.info(
156 'Fastest is ' +
157 LIST_FORMATTER.format(this.filter('fastest').map('name'))
158 )
159 const destroyTimeout = setTimeout(() => {
160 console.error('Pool destroy timeout reached (30s)')
161 resolve()
162 }, 30000)
163 pool
164 .destroy()
165 .then(resolve)
166 .catch(reject)
167 .finally(() => {
168 clearTimeout(destroyTimeout)
169 })
170 .catch(() => {})
171 })
172 .run({ async: true })
173 } catch (error) {
174 pool
175 .destroy()
176 .then(() => {
177 return reject(error)
178 })
179 .catch(() => {})
180 }
181 })
182 }
183
184 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
185 style: 'long',
186 type: 'conjunction'
187 })
188
189 const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => {
190 if (max < min || max < 0 || min < 0) {
191 throw new RangeError('Invalid interval')
192 }
193 max = Math.floor(max)
194 if (min != null && min !== 0) {
195 min = Math.ceil(min)
196 return Math.floor(Math.random() * (max - min + 1)) + min
197 }
198 return Math.floor(Math.random() * (max + 1))
199 }
200
201 const jsonIntegerSerialization = n => {
202 for (let i = 0; i < n; i++) {
203 const o = {
204 a: i
205 }
206 JSON.stringify(o)
207 }
208 return { ok: 1 }
209 }
210
211 /**
212 * Intentionally inefficient implementation.
213 * @param {number} n - The number of fibonacci numbers to generate.
214 * @returns {number} - The nth fibonacci number.
215 */
216 const fibonacci = n => {
217 if (n <= 1) return n
218 return fibonacci(n - 1) + fibonacci(n - 2)
219 }
220
221 /**
222 * Intentionally inefficient implementation.
223 * @param {number} n - The number to calculate the factorial of.
224 * @returns {number} - The factorial of n.
225 */
226 const factorial = n => {
227 if (n === 0) {
228 return 1
229 }
230 return factorial(n - 1) * n
231 }
232
233 const readWriteFiles = (
234 n,
235 baseDirectory = `/tmp/poolifier-benchmarks/${randomInt(281474976710655)}`
236 ) => {
237 if (existsSync(baseDirectory) === true) {
238 rmSync(baseDirectory, { recursive: true })
239 }
240 mkdirSync(baseDirectory, { recursive: true })
241 for (let i = 0; i < n; i++) {
242 const filePath = `${baseDirectory}/${i}`
243 writeFileSync(filePath, i.toString(), {
244 encoding: 'utf8',
245 flag: 'a'
246 })
247 readFileSync(filePath, 'utf8')
248 }
249 rmSync(baseDirectory, { recursive: true })
250 return { ok: 1 }
251 }
252
253 const executeTaskFunction = data => {
254 switch (data.function) {
255 case TaskFunctions.jsonIntegerSerialization:
256 return jsonIntegerSerialization(data.taskSize || 1000)
257 case TaskFunctions.fibonacci:
258 return fibonacci(data.taskSize || 1000)
259 case TaskFunctions.factorial:
260 return factorial(data.taskSize || 1000)
261 case TaskFunctions.readWriteFiles:
262 return readWriteFiles(data.taskSize || 1000)
263 default:
264 throw new Error('Unknown task function')
265 }
266 }
267
268 module.exports = {
269 LIST_FORMATTER,
270 executeTaskFunction,
271 generateRandomInteger,
272 runPoolifierPoolBenchmark
273 }