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