test: refine benchmark configuration
[poolifier.git] / benchmarks / internal / bench.mjs
CommitLineData
1676d5b1 1import assert from 'node:assert'
f1c674cd 2import Benchmark from 'benchmark'
d09b37fc 3import {
d9d8c14e 4 PoolTypes,
d09b37fc 5 WorkerChoiceStrategies,
d9d8c14e 6 WorkerTypes,
d09b37fc
JB
7 availableParallelism
8} from '../../lib/index.mjs'
dbca3be9 9import { TaskFunctions } from '../benchmarks-types.mjs'
f1c674cd
JB
10import {
11 LIST_FORMATTER,
12 buildPoolifierPool,
13 runPoolifierTest
14} from '../benchmarks-utils.mjs'
cdace0e5 15
d09b37fc 16const poolSize = availableParallelism()
1676d5b1
JB
17const fixedThreadPool = buildPoolifierPool(
18 WorkerTypes.thread,
19 PoolTypes.fixed,
20 poolSize
21)
0819631d 22
cdace0e5
JB
23const taskExecutions = 1
24const workerData = {
dbca3be9 25 function: TaskFunctions.jsonIntegerSerialization,
e8114b23 26 taskSize: 1000
cdace0e5 27}
f1c674cd 28
1676d5b1
JB
29const poolifierSuite = new Benchmark.Suite('Poolifier')
30
31for (const pool of [fixedThreadPool]) {
32 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
33 for (const enableTasksQueue of [false, true]) {
34 poolifierSuite.add(
35 `${pool.constructor.name}|${workerChoiceStrategy}|${
36 enableTasksQueue ? 'with' : 'without'
37 } tasks queue`,
38 async () => {
39 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
40 pool.enableTasksQueue(enableTasksQueue)
41 assert.strictEqual(
42 pool.opts.workerChoiceStrategy,
43 workerChoiceStrategy
44 )
45 assert.strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
46 await runPoolifierTest(pool, {
47 taskExecutions,
48 workerData
49 })
50 }
51 )
52 }
53 }
f1c674cd 54}
325f50bc 55
1676d5b1
JB
56poolifierSuite
57 .on('cycle', event => {
58 console.info(event.target.toString())
59 })
60 .on('complete', function () {
61 console.info(
62 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
63 )
64 fixedThreadPool.destroy()
65 })
66 .run({ async: true })