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 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
106 pool.enableTasksQueue(enableTasksQueue)
108 pool.opts.workerChoiceStrategy,
111 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
113 pool.opts.workerChoiceStrategyOptions.measurement,
116 await runPoolifierPool(pool, {
125 `${name} with ${workerChoiceStrategy} and ${
126 enableTasksQueue ? 'with' : 'without'
129 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
130 pool.enableTasksQueue(enableTasksQueue)
132 pool.opts.workerChoiceStrategy,
135 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
136 await runPoolifierPool(pool, {
146 .on('cycle', event => {
147 console.info(event.target.toString())
149 .on('complete', function () {
152 LIST_FORMATTER.format(this.filter('fastest').map('name'))
154 const destroyTimeout = setTimeout(() => {
155 console.error('Pool destroy timeout reached (30s)')
163 clearTimeout(destroyTimeout)
167 .run({ async: true })
179 export const buildPoolifierBenchmarkMitata = (
184 { taskExecutions, workerData }
187 const pool = buildPoolifierPool(workerType, poolType, poolSize)
188 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
189 for (const enableTasksQueue of [false, true]) {
190 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
191 for (const measurement of [Measurements.runTime, Measurements.elu]) {
194 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
195 enableTasksQueue ? 'with' : 'without'
198 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
201 pool.enableTasksQueue(enableTasksQueue)
203 pool.opts.workerChoiceStrategy,
206 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
208 pool.opts.workerChoiceStrategyOptions.measurement,
211 await runPoolifierPool(pool, {
222 `${name} with ${workerChoiceStrategy} and ${
223 enableTasksQueue ? 'with' : 'without'
226 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
227 pool.enableTasksQueue(enableTasksQueue)
229 pool.opts.workerChoiceStrategy,
232 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
233 await runPoolifierPool(pool, {
249 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
254 export { executeTaskFunction }