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