feat: add support for mitata benchmark
[poolifier.git] / benchmarks / internal / bench.mjs
1 import { exit } from 'node:process'
2 import { parseArgs } from 'node:util'
3
4 import { run } from 'mitata'
5
6 import {
7 availableParallelism,
8 PoolTypes,
9 WorkerTypes
10 } from '../../lib/index.mjs'
11 import { TaskFunctions } from '../benchmarks-types.cjs'
12 import {
13 buildPoolifierBenchmarkMitata,
14 runPoolifierBenchmarkBenchmarkJs
15 } from '../benchmarks-utils.mjs'
16
17 const poolSize = availableParallelism()
18 const taskExecutions = 1
19 const workerData = {
20 function: TaskFunctions.factorial,
21 taskSize: 50000
22 }
23
24 const options = {
25 type: {
26 type: 'string',
27 short: 't'
28 }
29 }
30 const { values } = parseArgs({
31 args: process.argv,
32 options,
33 strict: true,
34 allowPositionals: true
35 })
36
37 let fixedThreadPool
38 let dynamicThreadPool
39 let fixedClusterPool
40 let dynamicClusterPool
41 switch (values.type) {
42 case 'mitata':
43 fixedThreadPool = buildPoolifierBenchmarkMitata(
44 'FixedThreadPool',
45 WorkerTypes.thread,
46 PoolTypes.fixed,
47 poolSize,
48 {
49 taskExecutions,
50 workerData
51 }
52 )
53 dynamicThreadPool = buildPoolifierBenchmarkMitata(
54 'DynamicThreadPool',
55 WorkerTypes.thread,
56 PoolTypes.dynamic,
57 poolSize,
58 {
59 taskExecutions,
60 workerData
61 }
62 )
63 fixedClusterPool = buildPoolifierBenchmarkMitata(
64 'FixedClusterPool',
65 WorkerTypes.cluster,
66 PoolTypes.fixed,
67 poolSize,
68 {
69 taskExecutions,
70 workerData
71 }
72 )
73 dynamicClusterPool = buildPoolifierBenchmarkMitata(
74 'DynamicClusterPool',
75 WorkerTypes.cluster,
76 PoolTypes.dynamic,
77 poolSize,
78 {
79 taskExecutions,
80 workerData
81 }
82 )
83 await run()
84 await fixedThreadPool.destroy()
85 await dynamicThreadPool.destroy()
86 await fixedClusterPool.destroy()
87 await dynamicClusterPool.destroy()
88 break
89 case 'benchmark.js':
90 default:
91 await runPoolifierBenchmarkBenchmarkJs(
92 'FixedThreadPool',
93 WorkerTypes.thread,
94 PoolTypes.fixed,
95 poolSize,
96 {
97 taskExecutions,
98 workerData
99 }
100 )
101 await runPoolifierBenchmarkBenchmarkJs(
102 'DynamicThreadPool',
103 WorkerTypes.thread,
104 PoolTypes.dynamic,
105 poolSize,
106 {
107 taskExecutions,
108 workerData
109 }
110 )
111 await runPoolifierBenchmarkBenchmarkJs(
112 'FixedClusterPool',
113 WorkerTypes.cluster,
114 PoolTypes.fixed,
115 poolSize,
116 {
117 taskExecutions,
118 workerData
119 }
120 )
121 await runPoolifierBenchmarkBenchmarkJs(
122 'DynamicClusterPool',
123 WorkerTypes.cluster,
124 PoolTypes.dynamic,
125 poolSize,
126 {
127 taskExecutions,
128 workerData
129 }
130 )
131 break
132 }
133
134 exit()