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)
87 const suite = new Benchmark.Suite(name, {
89 if (pool.started && !pool.destroying) {
90 pool.destroy().then(resolve).catch(reject)
96 console.info(event.target.toString())
99 if (pool.started && !pool.destroying) {
103 return reject(event.target.error)
107 reject(event.target.error)
111 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
112 for (const enableTasksQueue of [false, true]) {
113 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
114 for (const measurement of [Measurements.runTime, Measurements.elu]) {
116 `${name} with ${workerChoiceStrategy}, with measurement ${measurement} and ${
117 enableTasksQueue ? 'with' : 'without'
120 await runPoolifierPool(pool, {
127 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
130 pool.enableTasksQueue(enableTasksQueue)
132 pool.opts.workerChoiceStrategy,
135 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
137 pool.opts.workerChoiceStrategyOptions.measurement,
146 `${name} with ${workerChoiceStrategy} and ${
147 enableTasksQueue ? 'with' : 'without'
150 await runPoolifierPool(pool, {
157 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
158 pool.enableTasksQueue(enableTasksQueue)
160 pool.opts.workerChoiceStrategy,
163 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
171 .on('complete', function () {
174 LIST_FORMATTER.format(this.filter('fastest').map('name'))
177 .run({ async: true })
181 export const buildPoolifierBenchmarkMitata = (
186 { taskExecutions, workerData }
189 const pool = buildPoolifierPool(workerType, poolType, poolSize)
190 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
191 for (const enableTasksQueue of [false, true]) {
192 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
193 for (const measurement of [Measurements.runTime, Measurements.elu]) {
196 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
197 enableTasksQueue ? 'with' : 'without'
200 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
203 pool.enableTasksQueue(enableTasksQueue)
205 pool.opts.workerChoiceStrategy,
208 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
210 pool.opts.workerChoiceStrategyOptions.measurement,
213 await runPoolifierPool(pool, {
224 `${name} with ${workerChoiceStrategy} and ${
225 enableTasksQueue ? 'with' : 'without'
228 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
229 pool.enableTasksQueue(enableTasksQueue)
231 pool.opts.workerChoiceStrategy,
234 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
235 await runPoolifierPool(pool, {
251 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
256 export { executeTaskFunction }