1 import { strictEqual } from 'node:assert'
3 import Benchmark from 'benchmark'
4 import { bench, clear, group, run } from 'tatami-ng'
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 (
84 { taskExecutions, workerData }
86 return await new Promise((resolve, reject) => {
87 const pool = buildPoolifierPool(workerType, poolType, poolSize, poolOptions)
88 let workerChoiceStrategy
90 let workerChoiceStrategyOptions
91 if (poolOptions != null) {
95 workerChoiceStrategyOptions
98 const measurement = workerChoiceStrategyOptions?.measurement
100 `${name} with ${workerChoiceStrategy ?? pool.opts.workerChoiceStrategy}${
101 measurement != null ? `, with ${measurement}` : ''
102 } and ${enableTasksQueue ? 'with' : 'without'} tasks queue`,
104 await runPoolifierPool(pool, {
111 if (workerChoiceStrategy != null) {
112 strictEqual(pool.opts.workerChoiceStrategy, workerChoiceStrategy)
114 if (enableTasksQueue != null) {
115 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
117 if (measurement != null) {
119 pool.opts.workerChoiceStrategyOptions.measurement,
124 onComplete: event => {
125 console.info(event.target.toString())
126 if (pool.started && !pool.destroying) {
127 pool.destroy().then(resolve).catch(reject)
133 if (pool.started && !pool.destroying) {
137 return reject(event.target.error)
141 reject(event.target.error)
145 ).run({ async: true })
149 export const runPoolifierBenchmarkBenchmarkJsSuite = async (
154 { taskExecutions, workerData }
156 return await new Promise((resolve, reject) => {
157 const pool = buildPoolifierPool(workerType, poolType, poolSize)
158 const suite = new Benchmark.Suite(name, {
160 if (pool.started && !pool.destroying) {
161 pool.destroy().then(resolve).catch(reject)
167 console.info(event.target.toString())
170 if (pool.started && !pool.destroying) {
174 return reject(event.target.error)
178 reject(event.target.error)
182 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
183 for (const enableTasksQueue of [false, true]) {
184 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
185 for (const measurement of [Measurements.runTime, Measurements.elu]) {
187 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
188 enableTasksQueue ? 'with' : 'without'
191 await runPoolifierPool(pool, {
198 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
201 pool.enableTasksQueue(enableTasksQueue)
203 pool.opts.workerChoiceStrategy,
206 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
208 pool.opts.workerChoiceStrategyOptions.measurement,
217 `${name} with ${workerChoiceStrategy} and ${
218 enableTasksQueue ? 'with' : 'without'
221 await runPoolifierPool(pool, {
228 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
229 pool.enableTasksQueue(enableTasksQueue)
231 pool.opts.workerChoiceStrategy,
234 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
242 .on('complete', function () {
245 LIST_FORMATTER.format(this.filter('fastest').map('name'))
248 .run({ async: true })
252 export const runPoolifierBenchmarkTatamiNg = async (
257 { taskExecutions, workerData }
260 const pool = buildPoolifierPool(workerType, poolType, poolSize)
261 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
262 for (const enableTasksQueue of [false, true]) {
263 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
264 for (const measurement of [Measurements.runTime, Measurements.elu]) {
267 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
268 enableTasksQueue ? 'with' : 'without'
271 await runPoolifierPool(pool, {
278 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
281 pool.enableTasksQueue(enableTasksQueue)
283 pool.opts.workerChoiceStrategy,
286 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
288 pool.opts.workerChoiceStrategyOptions.measurement,
299 `${name} with ${workerChoiceStrategy} and ${
300 enableTasksQueue ? 'with' : 'without'
303 await runPoolifierPool(pool, {
310 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
311 pool.enableTasksQueue(enableTasksQueue)
313 pool.opts.workerChoiceStrategy,
316 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
332 const LIST_FORMATTER = new Intl.ListFormat('en-US', {
337 export { executeTaskFunction }