5db55b3b11c9b8c956e46397674e71a34471d241
[poolifier.git] / benchmarks / benchmarks-utils.mjs
1 import { strictEqual } from 'node:assert'
2
3 import Benchmark from 'benchmark'
4 import { bench, group } from 'mitata'
5
6 import {
7 DynamicClusterPool,
8 DynamicThreadPool,
9 FixedClusterPool,
10 FixedThreadPool,
11 Measurements,
12 PoolTypes,
13 WorkerChoiceStrategies,
14 WorkerTypes
15 } from '../lib/index.mjs'
16 import { executeTaskFunction } from './benchmarks-utils.cjs'
17
18 const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
19 switch (poolType) {
20 case PoolTypes.fixed:
21 switch (workerType) {
22 case WorkerTypes.thread:
23 return new FixedThreadPool(
24 poolSize,
25 './benchmarks/internal/thread-worker.mjs',
26 poolOptions
27 )
28 case WorkerTypes.cluster:
29 return new FixedClusterPool(
30 poolSize,
31 './benchmarks/internal/cluster-worker.cjs',
32 poolOptions
33 )
34 }
35 break
36 case PoolTypes.dynamic:
37 switch (workerType) {
38 case WorkerTypes.thread:
39 return new DynamicThreadPool(
40 Math.floor(poolSize / 2),
41 poolSize,
42 './benchmarks/internal/thread-worker.mjs',
43 poolOptions
44 )
45 case WorkerTypes.cluster:
46 return new DynamicClusterPool(
47 Math.floor(poolSize / 2),
48 poolSize,
49 './benchmarks/internal/cluster-worker.cjs',
50 poolOptions
51 )
52 }
53 break
54 }
55 }
56
57 const runPoolifierPool = async (pool, { taskExecutions, workerData }) => {
58 return await new Promise((resolve, reject) => {
59 let executions = 0
60 for (let i = 1; i <= taskExecutions; i++) {
61 pool
62 .execute(workerData)
63 .then(() => {
64 ++executions
65 if (executions === taskExecutions) {
66 resolve({ ok: 1 })
67 }
68 return undefined
69 })
70 .catch(err => {
71 console.error(err)
72 reject(err)
73 })
74 }
75 })
76 }
77
78 export const runPoolifierBenchmarkBenchmarkJs = async (
79 name,
80 workerType,
81 poolType,
82 poolSize,
83 poolOptions,
84 { taskExecutions, workerData }
85 ) => {
86 return await new Promise((resolve, reject) => {
87 const pool = buildPoolifierPool(workerType, poolType, poolSize, poolOptions)
88 let workerChoiceStrategy
89 let enableTasksQueue
90 let workerChoiceStrategyOptions
91 if (poolOptions != null) {
92 ({
93 workerChoiceStrategy,
94 enableTasksQueue,
95 workerChoiceStrategyOptions
96 } = poolOptions)
97 }
98 const measurement = workerChoiceStrategyOptions?.measurement
99 new Benchmark(
100 `${name} with ${workerChoiceStrategy ?? pool.opts.workerChoiceStrategy}${
101 measurement != null ? `, with ${measurement}` : ''
102 } and ${enableTasksQueue ? 'with' : 'without'} tasks queue`,
103 async () => {
104 await runPoolifierPool(pool, {
105 taskExecutions,
106 workerData
107 })
108 },
109 {
110 onStart: () => {
111 if (workerChoiceStrategy != null) {
112 strictEqual(pool.opts.workerChoiceStrategy, workerChoiceStrategy)
113 }
114 if (enableTasksQueue != null) {
115 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
116 }
117 if (measurement != null) {
118 strictEqual(
119 pool.opts.workerChoiceStrategyOptions.measurement,
120 measurement
121 )
122 }
123 },
124 onComplete: event => {
125 console.info(event.target.toString())
126 if (pool.started && !pool.destroying) {
127 pool.destroy().then(resolve).catch(reject)
128 } else {
129 resolve()
130 }
131 },
132 onError: event => {
133 if (pool.started && !pool.destroying) {
134 pool
135 .destroy()
136 .then(() => {
137 return reject(event.target.error)
138 })
139 .catch(() => {})
140 } else {
141 reject(event.target.error)
142 }
143 }
144 }
145 ).run({ async: true })
146 })
147 }
148
149 export const runPoolifierBenchmarkBenchmarkJsSuite = async (
150 name,
151 workerType,
152 poolType,
153 poolSize,
154 { taskExecutions, workerData }
155 ) => {
156 return await new Promise((resolve, reject) => {
157 const pool = buildPoolifierPool(workerType, poolType, poolSize)
158 const suite = new Benchmark.Suite(name, {
159 onComplete: () => {
160 if (pool.started && !pool.destroying) {
161 pool.destroy().then(resolve).catch(reject)
162 } else {
163 resolve()
164 }
165 },
166 onCycle: event => {
167 console.info(event.target.toString())
168 },
169 onError: event => {
170 if (pool.started && !pool.destroying) {
171 pool
172 .destroy()
173 .then(() => {
174 return reject(event.target.error)
175 })
176 .catch(() => {})
177 } else {
178 reject(event.target.error)
179 }
180 }
181 })
182 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
183 for (const enableTasksQueue of [false, true]) {
184 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
185 for (const measurement of [Measurements.runTime, Measurements.elu]) {
186 suite.add(
187 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
188 enableTasksQueue ? 'with' : 'without'
189 } tasks queue`,
190 async () => {
191 await runPoolifierPool(pool, {
192 taskExecutions,
193 workerData
194 })
195 },
196 {
197 onStart: () => {
198 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
199 measurement
200 })
201 pool.enableTasksQueue(enableTasksQueue)
202 strictEqual(
203 pool.opts.workerChoiceStrategy,
204 workerChoiceStrategy
205 )
206 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
207 strictEqual(
208 pool.opts.workerChoiceStrategyOptions.measurement,
209 measurement
210 )
211 }
212 }
213 )
214 }
215 } else {
216 suite.add(
217 `${name} with ${workerChoiceStrategy} and ${
218 enableTasksQueue ? 'with' : 'without'
219 } tasks queue`,
220 async () => {
221 await runPoolifierPool(pool, {
222 taskExecutions,
223 workerData
224 })
225 },
226 {
227 onStart: () => {
228 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
229 pool.enableTasksQueue(enableTasksQueue)
230 strictEqual(
231 pool.opts.workerChoiceStrategy,
232 workerChoiceStrategy
233 )
234 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
235 }
236 }
237 )
238 }
239 }
240 }
241 suite
242 .on('complete', function () {
243 console.info(
244 'Fastest is ' +
245 LIST_FORMATTER.format(this.filter('fastest').map('name'))
246 )
247 })
248 .run({ async: true })
249 })
250 }
251
252 export const buildPoolifierBenchmarkMitata = (
253 name,
254 workerType,
255 poolType,
256 poolSize,
257 { taskExecutions, workerData }
258 ) => {
259 try {
260 const pool = buildPoolifierPool(workerType, poolType, poolSize)
261 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
262 for (const enableTasksQueue of [false, true]) {
263 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
264 for (const measurement of [Measurements.runTime, Measurements.elu]) {
265 group(name, () => {
266 bench(
267 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
268 enableTasksQueue ? 'with' : 'without'
269 } tasks queue`,
270 async () => {
271 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
272 measurement
273 })
274 pool.enableTasksQueue(enableTasksQueue)
275 strictEqual(
276 pool.opts.workerChoiceStrategy,
277 workerChoiceStrategy
278 )
279 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
280 strictEqual(
281 pool.opts.workerChoiceStrategyOptions.measurement,
282 measurement
283 )
284 await runPoolifierPool(pool, {
285 taskExecutions,
286 workerData
287 })
288 }
289 )
290 })
291 }
292 } else {
293 group(name, () => {
294 bench(
295 `${name} with ${workerChoiceStrategy} and ${
296 enableTasksQueue ? 'with' : 'without'
297 } tasks queue`,
298 async () => {
299 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
300 pool.enableTasksQueue(enableTasksQueue)
301 strictEqual(
302 pool.opts.workerChoiceStrategy,
303 workerChoiceStrategy
304 )
305 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
306 await runPoolifierPool(pool, {
307 taskExecutions,
308 workerData
309 })
310 }
311 )
312 })
313 }
314 }
315 }
316 return pool
317 } catch (error) {
318 console.error(error)
319 }
320 }
321
322 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
323 style: 'long',
324 type: 'conjunction'
325 })
326
327 export { executeTaskFunction }