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