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, {
90 if (pool.started && !pool.destroying) {
91 pool.destroy().then(resolve).catch(reject)
97 console.info(event.target.toString())
100 if (pool.started && !pool.destroying) {
104 return reject(event.target.error)
108 reject(event.target.error)
112 for (const workerChoiceStrategy of Object.values(
113 WorkerChoiceStrategies
115 for (const enableTasksQueue of [false, true]) {
116 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
117 for (const measurement of [
118 Measurements.runTime,
122 `${name} with ${workerChoiceStrategy}, with measurement ${measurement} and ${
123 enableTasksQueue ? 'with' : 'without'
126 await runPoolifierPool(pool, {
133 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
136 pool.enableTasksQueue(enableTasksQueue)
138 pool.opts.workerChoiceStrategy,
141 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
143 pool.opts.workerChoiceStrategyOptions.measurement,
152 `${name} with ${workerChoiceStrategy} and ${
153 enableTasksQueue ? 'with' : 'without'
156 await runPoolifierPool(pool, {
163 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
164 pool.enableTasksQueue(enableTasksQueue)
166 pool.opts.workerChoiceStrategy,
169 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
177 .on('complete', function () {
180 LIST_FORMATTER.format(this.filter('fastest').map('name'))
183 .run({ async: true })
195 export const buildPoolifierBenchmarkMitata = (
200 { taskExecutions, workerData }
203 const pool = buildPoolifierPool(workerType, poolType, poolSize)
204 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
205 for (const enableTasksQueue of [false, true]) {
206 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
207 for (const measurement of [Measurements.runTime, Measurements.elu]) {
210 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
211 enableTasksQueue ? 'with' : 'without'
214 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
217 pool.enableTasksQueue(enableTasksQueue)
219 pool.opts.workerChoiceStrategy,
222 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
224 pool.opts.workerChoiceStrategyOptions.measurement,
227 await runPoolifierPool(pool, {
238 `${name} with ${workerChoiceStrategy} and ${
239 enableTasksQueue ? 'with' : 'without'
242 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
243 pool.enableTasksQueue(enableTasksQueue)
245 pool.opts.workerChoiceStrategy,
248 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
249 await runPoolifierPool(pool, {
265 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
270 export { executeTaskFunction }