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 let pool = buildPoolifierPool(workerType, poolType, poolSize)
88 const suite = new Benchmark.Suite(name, {
90 const destroyTimeout = setTimeout(() => {
91 console.error('Pool destroy timeout reached (30s)')
100 clearTimeout(destroyTimeout)
105 console.info(event.target.toString())
111 return reject(event.target.error)
116 for (const workerChoiceStrategy of Object.values(
117 WorkerChoiceStrategies
119 for (const enableTasksQueue of [false, true]) {
120 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
121 for (const measurement of [
122 Measurements.runTime,
126 `${name} with ${workerChoiceStrategy}, with measurement ${measurement} and ${
127 enableTasksQueue ? 'with' : 'without'
130 await runPoolifierPool(pool, {
137 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
140 pool.enableTasksQueue(enableTasksQueue)
142 pool.opts.workerChoiceStrategy,
145 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
147 pool.opts.workerChoiceStrategyOptions.measurement,
156 `${name} with ${workerChoiceStrategy} and ${
157 enableTasksQueue ? 'with' : 'without'
160 await runPoolifierPool(pool, {
167 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
168 pool.enableTasksQueue(enableTasksQueue)
170 pool.opts.workerChoiceStrategy,
173 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
181 .on('complete', function () {
184 LIST_FORMATTER.format(this.filter('fastest').map('name'))
187 .run({ async: true, queued: true })
199 export const buildPoolifierBenchmarkMitata = (
204 { taskExecutions, workerData }
207 const pool = buildPoolifierPool(workerType, poolType, poolSize)
208 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
209 for (const enableTasksQueue of [false, true]) {
210 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
211 for (const measurement of [Measurements.runTime, Measurements.elu]) {
214 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
215 enableTasksQueue ? 'with' : 'without'
218 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
221 pool.enableTasksQueue(enableTasksQueue)
223 pool.opts.workerChoiceStrategy,
226 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
228 pool.opts.workerChoiceStrategyOptions.measurement,
231 await runPoolifierPool(pool, {
242 `${name} with ${workerChoiceStrategy} and ${
243 enableTasksQueue ? 'with' : 'without'
246 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
247 pool.enableTasksQueue(enableTasksQueue)
249 pool.opts.workerChoiceStrategy,
252 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
253 await runPoolifierPool(pool, {
269 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
274 export { executeTaskFunction }