refactor: convert internal benchmark code to ESM
[poolifier.git] / benchmarks / internal / bench.mjs
CommitLineData
8a970421
JB
1import Benchmark from 'benny'
2import { WorkerChoiceStrategies } from '../../lib/index.mjs'
3import { PoolTypes, WorkerFunctions, WorkerTypes } from '../benchmarks-types.js'
4import { buildPool, runTest } from '../benchmarks-utils.js'
cdace0e5
JB
5
6const poolSize = 30
7const taskExecutions = 1
8const workerData = {
9 function: WorkerFunctions.jsonIntegerSerialization,
10 taskSize: 1000
11}
12const tasksQueuePoolOption = { enableTasksQueue: true }
13const workerChoiceStrategyRoundRobinPoolOption = {
14 workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN
15}
e4543b14
JB
16const workerChoiceStrategyLeastUsedPoolOption = {
17 workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED
cdace0e5 18}
e4543b14
JB
19const workerChoiceStrategyLeastBusyPoolOption = {
20 workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY
cdace0e5
JB
21}
22const workerChoiceStrategyWeightedRoundRobinPoolOption = {
23 workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
24}
25const workerChoiceStrategyFairSharePoolOption = {
26 workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE
27}
28
29const fixedThreadPoolRoundRobin = buildPool(
bb615bd0
JB
30 WorkerTypes.thread,
31 PoolTypes.fixed,
cdace0e5 32 poolSize,
cdace0e5
JB
33 workerChoiceStrategyRoundRobinPoolOption
34)
35
36const fixedThreadPoolRoundRobinTasksQueue = buildPool(
bb615bd0
JB
37 WorkerTypes.thread,
38 PoolTypes.fixed,
cdace0e5 39 poolSize,
cdace0e5
JB
40 { ...workerChoiceStrategyRoundRobinPoolOption, ...tasksQueuePoolOption }
41)
42
e4543b14 43const fixedThreadPoolLeastUsed = buildPool(
bb615bd0
JB
44 WorkerTypes.thread,
45 PoolTypes.fixed,
cdace0e5 46 poolSize,
e4543b14 47 workerChoiceStrategyLeastUsedPoolOption
cdace0e5
JB
48)
49
e4543b14 50const fixedThreadPoolLeastBusy = buildPool(
bb615bd0
JB
51 WorkerTypes.thread,
52 PoolTypes.fixed,
cdace0e5 53 poolSize,
e4543b14 54 workerChoiceStrategyLeastBusyPoolOption
cdace0e5
JB
55)
56
57const fixedThreadPoolWeightedRoundRobin = buildPool(
bb615bd0
JB
58 WorkerTypes.thread,
59 PoolTypes.fixed,
cdace0e5 60 poolSize,
cdace0e5
JB
61 workerChoiceStrategyWeightedRoundRobinPoolOption
62)
63
64const fixedThreadPoolFairShare = buildPool(
bb615bd0
JB
65 WorkerTypes.thread,
66 PoolTypes.fixed,
cdace0e5 67 poolSize,
cdace0e5
JB
68 workerChoiceStrategyFairSharePoolOption
69)
70
d9f14be6 71const fixedThreadPoolFairShareTasksQueue = buildPool(
bb615bd0
JB
72 WorkerTypes.thread,
73 PoolTypes.fixed,
d9f14be6
JB
74 poolSize,
75 { ...workerChoiceStrategyFairSharePoolOption, ...tasksQueuePoolOption }
76)
77
cdace0e5 78const dynamicThreadPoolRoundRobin = buildPool(
bb615bd0
JB
79 WorkerTypes.thread,
80 PoolTypes.dynamic,
cdace0e5 81 poolSize,
cdace0e5
JB
82 workerChoiceStrategyRoundRobinPoolOption
83)
84
e4543b14 85const dynamicThreadPoolLeastUsed = buildPool(
bb615bd0
JB
86 WorkerTypes.thread,
87 PoolTypes.dynamic,
cdace0e5 88 poolSize,
e4543b14 89 workerChoiceStrategyLeastUsedPoolOption
cdace0e5
JB
90)
91
e4543b14 92const dynamicThreadPoolLeastBusy = buildPool(
bb615bd0
JB
93 WorkerTypes.thread,
94 PoolTypes.dynamic,
cdace0e5 95 poolSize,
e4543b14 96 workerChoiceStrategyLeastBusyPoolOption
cdace0e5
JB
97)
98
99const dynamicThreadPoolWeightedRoundRobin = buildPool(
bb615bd0
JB
100 WorkerTypes.thread,
101 PoolTypes.dynamic,
cdace0e5 102 poolSize,
cdace0e5
JB
103 workerChoiceStrategyWeightedRoundRobinPoolOption
104)
105
106const dynamicThreadPoolFairShare = buildPool(
bb615bd0
JB
107 WorkerTypes.thread,
108 PoolTypes.dynamic,
cdace0e5 109 poolSize,
cdace0e5
JB
110 workerChoiceStrategyFairSharePoolOption
111)
112
113const fixedClusterPoolRoundRobin = buildPool(
bb615bd0
JB
114 WorkerTypes.cluster,
115 PoolTypes.fixed,
cdace0e5 116 poolSize,
cdace0e5
JB
117 workerChoiceStrategyRoundRobinPoolOption
118)
119
120const fixedClusterPoolRoundRobinTasksQueue = buildPool(
bb615bd0
JB
121 WorkerTypes.cluster,
122 PoolTypes.fixed,
cdace0e5 123 poolSize,
cdace0e5
JB
124 { ...workerChoiceStrategyRoundRobinPoolOption, ...tasksQueuePoolOption }
125)
126
e4543b14 127const fixedClusterPoolLeastUsed = buildPool(
bb615bd0
JB
128 WorkerTypes.cluster,
129 PoolTypes.fixed,
cdace0e5 130 poolSize,
e4543b14 131 workerChoiceStrategyLeastUsedPoolOption
cdace0e5
JB
132)
133
e4543b14 134const fixedClusterPoolLeastBusy = buildPool(
bb615bd0
JB
135 WorkerTypes.cluster,
136 PoolTypes.fixed,
cdace0e5 137 poolSize,
e4543b14 138 workerChoiceStrategyLeastBusyPoolOption
cdace0e5
JB
139)
140
141const fixedClusterPoolWeightedRoundRobin = buildPool(
bb615bd0
JB
142 WorkerTypes.cluster,
143 PoolTypes.fixed,
cdace0e5 144 poolSize,
cdace0e5
JB
145 workerChoiceStrategyWeightedRoundRobinPoolOption
146)
147
148const fixedClusterPoolFairShare = buildPool(
bb615bd0
JB
149 WorkerTypes.cluster,
150 PoolTypes.fixed,
cdace0e5 151 poolSize,
cdace0e5
JB
152 workerChoiceStrategyFairSharePoolOption
153)
154
d9f14be6 155const fixedClusterPoolFairShareTaskQueue = buildPool(
bb615bd0
JB
156 WorkerTypes.cluster,
157 PoolTypes.fixed,
d9f14be6
JB
158 poolSize,
159 { ...workerChoiceStrategyFairSharePoolOption, ...tasksQueuePoolOption }
160)
161
cdace0e5 162const dynamicClusterPoolRoundRobin = buildPool(
bb615bd0
JB
163 WorkerTypes.cluster,
164 PoolTypes.dynamic,
cdace0e5 165 poolSize,
cdace0e5
JB
166 workerChoiceStrategyRoundRobinPoolOption
167)
168
e4543b14 169const dynamicClusterPoolLeastUsed = buildPool(
bb615bd0
JB
170 WorkerTypes.cluster,
171 PoolTypes.dynamic,
cdace0e5 172 poolSize,
e4543b14 173 workerChoiceStrategyLeastUsedPoolOption
cdace0e5
JB
174)
175
e4543b14 176const dynamicClusterPoolLeastBusy = buildPool(
bb615bd0
JB
177 WorkerTypes.cluster,
178 PoolTypes.dynamic,
cdace0e5 179 poolSize,
e4543b14 180 workerChoiceStrategyLeastBusyPoolOption
cdace0e5
JB
181)
182
183const dynamicClusterPoolWeightedRoundRobin = buildPool(
bb615bd0
JB
184 WorkerTypes.cluster,
185 PoolTypes.dynamic,
cdace0e5 186 poolSize,
cdace0e5
JB
187 workerChoiceStrategyWeightedRoundRobinPoolOption
188)
189
190const dynamicClusterPoolFairShare = buildPool(
bb615bd0
JB
191 WorkerTypes.cluster,
192 PoolTypes.dynamic,
cdace0e5 193 poolSize,
cdace0e5
JB
194 workerChoiceStrategyFairSharePoolOption
195)
325f50bc 196
ca6c7d70
JB
197const resultsFile = 'poolifier'
198const resultsFolder = 'benchmarks/internal/results'
57df5469 199
ca6c7d70
JB
200Benchmark.suite(
201 'Poolifier',
cdace0e5
JB
202 Benchmark.add('Fixed:ThreadPool:RoundRobin', async () => {
203 await runTest(fixedThreadPoolRoundRobin, {
204 taskExecutions,
205 workerData
206 })
ca6c7d70 207 }),
cdace0e5
JB
208 Benchmark.add(
209 'Fixed:ThreadPool:RoundRobin:{ enableTasksQueue: true }',
210 async () => {
211 await runTest(fixedThreadPoolRoundRobinTasksQueue, {
212 taskExecutions,
213 workerData
214 })
215 }
216 ),
e4543b14
JB
217 Benchmark.add('Fixed:ThreadPool:LeastUsed', async () => {
218 await runTest(fixedThreadPoolLeastUsed, {
cdace0e5
JB
219 taskExecutions,
220 workerData
221 })
ca6c7d70 222 }),
e4543b14
JB
223 Benchmark.add('Fixed:ThreadPool:LeastBusy', async () => {
224 await runTest(fixedThreadPoolLeastBusy, {
cdace0e5
JB
225 taskExecutions,
226 workerData
227 })
d4abc60a 228 }),
cdace0e5
JB
229 Benchmark.add('Fixed:ThreadPool:WeightedRoundRobin', async () => {
230 await runTest(fixedThreadPoolWeightedRoundRobin, {
231 taskExecutions,
232 workerData
233 })
ca6c7d70 234 }),
cdace0e5
JB
235 Benchmark.add('Fixed:ThreadPool:FairShare', async () => {
236 await runTest(fixedThreadPoolFairShare, {
237 taskExecutions,
238 workerData
239 })
ca6c7d70 240 }),
d9f14be6
JB
241 Benchmark.add(
242 'Fixed:ThreadPool:FairShare:{ enableTasksQueue: true }',
243 async () => {
244 await runTest(fixedThreadPoolFairShareTasksQueue, {
245 taskExecutions,
246 workerData
247 })
248 }
249 ),
cdace0e5
JB
250 Benchmark.add('Dynamic:ThreadPool:RoundRobin', async () => {
251 await runTest(dynamicThreadPoolRoundRobin, {
252 taskExecutions,
253 workerData
254 })
ca6c7d70 255 }),
e4543b14
JB
256 Benchmark.add('Dynamic:ThreadPool:LeastUsed', async () => {
257 await runTest(dynamicThreadPoolLeastUsed, {
cdace0e5
JB
258 taskExecutions,
259 workerData
260 })
ca6c7d70 261 }),
e4543b14
JB
262 Benchmark.add('Dynamic:ThreadPool:LeastBusy', async () => {
263 await runTest(dynamicThreadPoolLeastBusy, {
cdace0e5
JB
264 taskExecutions,
265 workerData
266 })
d4abc60a 267 }),
cdace0e5
JB
268 Benchmark.add('Dynamic:ThreadPool:WeightedRoundRobin', async () => {
269 await runTest(dynamicThreadPoolWeightedRoundRobin, {
270 taskExecutions,
271 workerData
272 })
ca6c7d70 273 }),
cdace0e5
JB
274 Benchmark.add('Dynamic:ThreadPool:FairShare', async () => {
275 await runTest(dynamicThreadPoolFairShare, {
276 taskExecutions,
277 workerData
278 })
ca6c7d70 279 }),
cdace0e5
JB
280 Benchmark.add('Fixed:ClusterPool:RoundRobin', async () => {
281 await runTest(fixedClusterPoolRoundRobin, {
282 taskExecutions,
283 workerData
284 })
ca6c7d70 285 }),
cdace0e5
JB
286 Benchmark.add(
287 'Fixed:ClusterPool:RoundRobin:{ enableTasksQueue: true }',
288 async () => {
289 await runTest(fixedClusterPoolRoundRobinTasksQueue, {
290 taskExecutions,
291 workerData
292 })
293 }
294 ),
e4543b14
JB
295 Benchmark.add('Fixed:ClusterPool:LeastUsed', async () => {
296 await runTest(fixedClusterPoolLeastUsed, {
cdace0e5
JB
297 taskExecutions,
298 workerData
299 })
869b1a45 300 }),
e4543b14
JB
301 Benchmark.add('Fixed:ClusterPool:LeastBusy', async () => {
302 await runTest(fixedClusterPoolLeastBusy, {
cdace0e5
JB
303 taskExecutions,
304 workerData
305 })
ca6c7d70 306 }),
cdace0e5
JB
307 Benchmark.add('Fixed:ClusterPool:WeightedRoundRobin', async () => {
308 await runTest(fixedClusterPoolWeightedRoundRobin, {
309 taskExecutions,
310 workerData
311 })
d4abc60a 312 }),
cdace0e5
JB
313 Benchmark.add('Fixed:ClusterPool:FairShare', async () => {
314 await runTest(fixedClusterPoolFairShare, {
315 taskExecutions,
316 workerData
317 })
ca6c7d70 318 }),
d9f14be6
JB
319 Benchmark.add(
320 'Fixed:ClusterPool:FairShare:{ enableTasksQueue: true }',
321 async () => {
322 await runTest(fixedClusterPoolFairShareTaskQueue, {
323 taskExecutions,
324 workerData
325 })
326 }
327 ),
cdace0e5
JB
328 Benchmark.add('Dynamic:ClusterPool:RoundRobin', async () => {
329 await runTest(dynamicClusterPoolRoundRobin, {
330 taskExecutions,
331 workerData
332 })
ca6c7d70 333 }),
e4543b14
JB
334 Benchmark.add('Dynamic:ClusterPool:LeastUsed', async () => {
335 await runTest(dynamicClusterPoolLeastUsed, {
cdace0e5
JB
336 taskExecutions,
337 workerData
338 })
ca6c7d70 339 }),
e4543b14
JB
340 Benchmark.add('Dynamic:ClusterPool:LeastBusy', async () => {
341 await runTest(dynamicClusterPoolLeastBusy, {
cdace0e5
JB
342 taskExecutions,
343 workerData
344 })
ca6c7d70 345 }),
cdace0e5
JB
346 Benchmark.add('Dynamic:ClusterPool:WeightedRoundRobin', async () => {
347 await runTest(dynamicClusterPoolWeightedRoundRobin, {
348 taskExecutions,
349 workerData
350 })
d4abc60a 351 }),
cdace0e5
JB
352 Benchmark.add('Dynamic:ClusterPool:FairShare', async () => {
353 await runTest(dynamicClusterPoolFairShare, {
354 taskExecutions,
355 workerData
356 })
ca6c7d70
JB
357 }),
358 Benchmark.cycle(),
359 Benchmark.complete(),
360 Benchmark.save({
361 file: resultsFile,
362 folder: resultsFolder,
363 format: 'json',
364 details: true
365 }),
366 Benchmark.save({
367 file: resultsFile,
368 folder: resultsFolder,
369 format: 'chart.html',
370 details: true
371 }),
372 Benchmark.save({
373 file: resultsFile,
374 folder: resultsFolder,
375 format: 'table.html',
376 details: true
377 })
378)
18cac485
JB
379 .then(() => {
380 // eslint-disable-next-line n/no-process-exit
381 return process.exit()
382 })
383 .catch(err => console.error(err))