build(deps-dev): apply updates
[poolifier.git] / benchmarks / benchmarks-utils.mjs
CommitLineData
0804b9b4
JB
1import { strictEqual } from 'node:assert'
2
98f60ddd 3import { bench, clear, group, run } from 'tatami-ng'
0804b9b4
JB
4
5import {
6 DynamicClusterPool,
7 DynamicThreadPool,
8 FixedClusterPool,
9 FixedThreadPool,
10 Measurements,
11 PoolTypes,
12 WorkerChoiceStrategies,
13 WorkerTypes
14} from '../lib/index.mjs'
a6bef8d2 15import { executeTaskFunction } from './benchmarks-utils.cjs'
0804b9b4
JB
16
17const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
18 switch (poolType) {
19 case PoolTypes.fixed:
20 switch (workerType) {
21 case WorkerTypes.thread:
22 return new FixedThreadPool(
23 poolSize,
24 './benchmarks/internal/thread-worker.mjs',
25 poolOptions
26 )
27 case WorkerTypes.cluster:
28 return new FixedClusterPool(
29 poolSize,
30 './benchmarks/internal/cluster-worker.cjs',
31 poolOptions
32 )
33 }
34 break
35 case PoolTypes.dynamic:
36 switch (workerType) {
37 case WorkerTypes.thread:
38 return new DynamicThreadPool(
39 Math.floor(poolSize / 2),
40 poolSize,
41 './benchmarks/internal/thread-worker.mjs',
42 poolOptions
43 )
44 case WorkerTypes.cluster:
45 return new DynamicClusterPool(
46 Math.floor(poolSize / 2),
47 poolSize,
48 './benchmarks/internal/cluster-worker.cjs',
49 poolOptions
50 )
51 }
52 break
53 }
54}
55
56const runPoolifierPool = async (pool, { taskExecutions, workerData }) => {
de62923a
JB
57 for (let i = 1; i <= taskExecutions; i++) {
58 await pool.execute(workerData)
59 }
0804b9b4
JB
60}
61
b5ca7c94 62export const runPoolifierBenchmarkTatamiNg = async (
0804b9b4
JB
63 name,
64 workerType,
65 poolType,
66 poolSize,
67 { taskExecutions, workerData }
68) => {
69 try {
70 const pool = buildPoolifierPool(workerType, poolType, poolSize)
71 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
72 for (const enableTasksQueue of [false, true]) {
73 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
74 for (const measurement of [Measurements.runTime, Measurements.elu]) {
75 group(name, () => {
76 bench(
77 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
78 enableTasksQueue ? 'with' : 'without'
79 } tasks queue`,
80 async () => {
0804b9b4
JB
81 await runPoolifierPool(pool, {
82 taskExecutions,
83 workerData
84 })
2baf75cf
JB
85 },
86 {
87 before: () => {
88 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
89 measurement
90 })
91 pool.enableTasksQueue(enableTasksQueue)
92 strictEqual(
93 pool.opts.workerChoiceStrategy,
94 workerChoiceStrategy
95 )
96 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
97 strictEqual(
98 pool.opts.workerChoiceStrategyOptions.measurement,
99 measurement
100 )
101 }
0804b9b4
JB
102 }
103 )
104 })
105 }
106 } else {
107 group(name, () => {
108 bench(
109 `${name} with ${workerChoiceStrategy} and ${
110 enableTasksQueue ? 'with' : 'without'
111 } tasks queue`,
112 async () => {
0804b9b4
JB
113 await runPoolifierPool(pool, {
114 taskExecutions,
115 workerData
116 })
2baf75cf
JB
117 },
118 {
119 before: () => {
120 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
121 pool.enableTasksQueue(enableTasksQueue)
122 strictEqual(
123 pool.opts.workerChoiceStrategy,
124 workerChoiceStrategy
125 )
126 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
127 }
0804b9b4
JB
128 }
129 )
130 })
131 }
132 }
133 }
8f01ffbe 134 const report = await run()
16534b42 135 clear()
2baf75cf 136 await pool.destroy()
8f01ffbe 137 return report
0804b9b4
JB
138 } catch (error) {
139 console.error(error)
140 }
141}
142
8f01ffbe
JB
143export const convertTatamiNgToBmf = report => {
144 return report.benchmarks
145 .map(({ name, stats }) => {
146 return {
147 [name]: {
148 latency: {
149 value: stats?.avg,
150 lower_value: stats?.min,
151 upper_value: stats?.max
152 },
153 throughput: {
154 value: stats?.iter
155 }
156 }
157 }
158 })
159 .reduce((obj, item) => Object.assign(obj, item), {})
160}
a6bef8d2
JB
161
162export { executeTaskFunction }