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