1 import { strictEqual } from 'node:assert'
3 import Benchmark from 'benchmark'
4 import { bench, group } from 'mitata'
13 WorkerChoiceStrategies,
15 } from '../lib/index.mjs'
16 import { executeTaskFunction } from './benchmarks-utils.cjs'
18 const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
22 case WorkerTypes.thread:
23 return new FixedThreadPool(
25 './benchmarks/internal/thread-worker.mjs',
28 case WorkerTypes.cluster:
29 return new FixedClusterPool(
31 './benchmarks/internal/cluster-worker.cjs',
36 case PoolTypes.dynamic:
38 case WorkerTypes.thread:
39 return new DynamicThreadPool(
40 Math.floor(poolSize / 2),
42 './benchmarks/internal/thread-worker.mjs',
45 case WorkerTypes.cluster:
46 return new DynamicClusterPool(
47 Math.floor(poolSize / 2),
49 './benchmarks/internal/cluster-worker.cjs',
57 const runPoolifierPool = async (pool, { taskExecutions, workerData }) => {
58 return await new Promise((resolve, reject) => {
60 for (let i = 1; i <= taskExecutions; i++) {
65 if (executions === taskExecutions) {
78 export const runPoolifierBenchmarkBenchmarkJs = async (
83 { taskExecutions, workerData }
85 return await new Promise((resolve, reject) => {
86 const pool = buildPoolifierPool(workerType, poolType, poolSize)
88 const suite = new Benchmark.Suite(name)
89 for (const workerChoiceStrategy of Object.values(
90 WorkerChoiceStrategies
92 for (const enableTasksQueue of [false, true]) {
93 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
94 for (const measurement of [
99 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
100 enableTasksQueue ? 'with' : 'without'
103 await runPoolifierPool(pool, {
110 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
113 pool.enableTasksQueue(enableTasksQueue)
115 pool.opts.workerChoiceStrategy,
118 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
120 pool.opts.workerChoiceStrategyOptions.measurement,
129 `${name} with ${workerChoiceStrategy} and ${
130 enableTasksQueue ? 'with' : 'without'
133 await runPoolifierPool(pool, {
140 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
141 pool.enableTasksQueue(enableTasksQueue)
143 pool.opts.workerChoiceStrategy,
146 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
154 .on('cycle', event => {
155 console.info(event.target.toString())
157 .on('complete', function () {
160 LIST_FORMATTER.format(this.filter('fastest').map('name'))
162 const destroyTimeout = setTimeout(() => {
163 console.error('Pool destroy timeout reached (30s)')
171 clearTimeout(destroyTimeout)
175 .run({ async: true })
187 export const buildPoolifierBenchmarkMitata = (
192 { taskExecutions, workerData }
195 const pool = buildPoolifierPool(workerType, poolType, poolSize)
196 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
197 for (const enableTasksQueue of [false, true]) {
198 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
199 for (const measurement of [Measurements.runTime, Measurements.elu]) {
202 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
203 enableTasksQueue ? 'with' : 'without'
206 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
209 pool.enableTasksQueue(enableTasksQueue)
211 pool.opts.workerChoiceStrategy,
214 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
216 pool.opts.workerChoiceStrategyOptions.measurement,
219 await runPoolifierPool(pool, {
230 `${name} with ${workerChoiceStrategy} and ${
231 enableTasksQueue ? 'with' : 'without'
234 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
235 pool.enableTasksQueue(enableTasksQueue)
237 pool.opts.workerChoiceStrategy,
240 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
241 await runPoolifierPool(pool, {
257 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
262 export { executeTaskFunction }