refactor(benchmark): remove unneeded try {} catch {} logic
[poolifier.git] / benchmarks / benchmarks-utils.mjs
CommitLineData
0804b9b4
JB
1import { strictEqual } from 'node:assert'
2
3import Benchmark from 'benchmark'
4import { bench, group } from 'mitata'
5
6import {
7 DynamicClusterPool,
8 DynamicThreadPool,
9 FixedClusterPool,
10 FixedThreadPool,
11 Measurements,
12 PoolTypes,
13 WorkerChoiceStrategies,
14 WorkerTypes
15} from '../lib/index.mjs'
a6bef8d2 16import { executeTaskFunction } from './benchmarks-utils.cjs'
0804b9b4
JB
17
18const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
19 switch (poolType) {
20 case PoolTypes.fixed:
21 switch (workerType) {
22 case WorkerTypes.thread:
23 return new FixedThreadPool(
24 poolSize,
25 './benchmarks/internal/thread-worker.mjs',
26 poolOptions
27 )
28 case WorkerTypes.cluster:
29 return new FixedClusterPool(
30 poolSize,
31 './benchmarks/internal/cluster-worker.cjs',
32 poolOptions
33 )
34 }
35 break
36 case PoolTypes.dynamic:
37 switch (workerType) {
38 case WorkerTypes.thread:
39 return new DynamicThreadPool(
40 Math.floor(poolSize / 2),
41 poolSize,
42 './benchmarks/internal/thread-worker.mjs',
43 poolOptions
44 )
45 case WorkerTypes.cluster:
46 return new DynamicClusterPool(
47 Math.floor(poolSize / 2),
48 poolSize,
49 './benchmarks/internal/cluster-worker.cjs',
50 poolOptions
51 )
52 }
53 break
54 }
55}
56
57const runPoolifierPool = async (pool, { taskExecutions, workerData }) => {
58 return await new Promise((resolve, reject) => {
59 let executions = 0
60 for (let i = 1; i <= taskExecutions; i++) {
61 pool
62 .execute(workerData)
63 .then(() => {
64 ++executions
65 if (executions === taskExecutions) {
66 resolve({ ok: 1 })
67 }
68 return undefined
69 })
70 .catch(err => {
71 console.error(err)
72 reject(err)
73 })
74 }
75 })
76}
77
78export const runPoolifierBenchmarkBenchmarkJs = async (
79 name,
80 workerType,
81 poolType,
82 poolSize,
83 { taskExecutions, workerData }
84) => {
85 return await new Promise((resolve, reject) => {
6aec0be3 86 const pool = buildPoolifierPool(workerType, poolType, poolSize)
47ab5d23
JB
87 const suite = new Benchmark.Suite(name, {
88 onComplete: () => {
89 if (pool.started && !pool.destroying) {
90 pool.destroy().then(resolve).catch(reject)
91 } else {
92 resolve()
fe7488e9 93 }
47ab5d23
JB
94 },
95 onCycle: event => {
96 console.info(event.target.toString())
97 },
98 onError: event => {
99 if (pool.started && !pool.destroying) {
100 pool
101 .destroy()
102 .then(() => {
103 return reject(event.target.error)
104 })
105 .catch(() => {})
106 } else {
107 reject(event.target.error)
108 }
109 }
110 })
111 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
112 for (const enableTasksQueue of [false, true]) {
113 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
114 for (const measurement of [Measurements.runTime, Measurements.elu]) {
0804b9b4 115 suite.add(
47ab5d23 116 `${name} with ${workerChoiceStrategy}, with measurement ${measurement} and ${
0804b9b4
JB
117 enableTasksQueue ? 'with' : 'without'
118 } tasks queue`,
119 async () => {
0804b9b4
JB
120 await runPoolifierPool(pool, {
121 taskExecutions,
122 workerData
123 })
083213c6
JB
124 },
125 {
126 onStart: () => {
47ab5d23
JB
127 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
128 measurement
129 })
083213c6
JB
130 pool.enableTasksQueue(enableTasksQueue)
131 strictEqual(
132 pool.opts.workerChoiceStrategy,
133 workerChoiceStrategy
134 )
135 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
47ab5d23
JB
136 strictEqual(
137 pool.opts.workerChoiceStrategyOptions.measurement,
138 measurement
139 )
083213c6 140 }
0804b9b4
JB
141 }
142 )
143 }
47ab5d23
JB
144 } else {
145 suite.add(
146 `${name} with ${workerChoiceStrategy} and ${
147 enableTasksQueue ? 'with' : 'without'
148 } tasks queue`,
149 async () => {
150 await runPoolifierPool(pool, {
151 taskExecutions,
152 workerData
153 })
154 },
155 {
156 onStart: () => {
157 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
158 pool.enableTasksQueue(enableTasksQueue)
159 strictEqual(
160 pool.opts.workerChoiceStrategy,
161 workerChoiceStrategy
162 )
163 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
164 }
165 }
166 )
0804b9b4
JB
167 }
168 }
0804b9b4 169 }
47ab5d23
JB
170 suite
171 .on('complete', function () {
172 console.info(
173 'Fastest is ' +
174 LIST_FORMATTER.format(this.filter('fastest').map('name'))
175 )
176 })
177 .run({ async: true })
0804b9b4
JB
178 })
179}
180
181export const buildPoolifierBenchmarkMitata = (
182 name,
183 workerType,
184 poolType,
185 poolSize,
186 { taskExecutions, workerData }
187) => {
188 try {
189 const pool = buildPoolifierPool(workerType, poolType, poolSize)
190 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
191 for (const enableTasksQueue of [false, true]) {
192 if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
193 for (const measurement of [Measurements.runTime, Measurements.elu]) {
194 group(name, () => {
195 bench(
196 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
197 enableTasksQueue ? 'with' : 'without'
198 } tasks queue`,
199 async () => {
200 pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
201 measurement
202 })
203 pool.enableTasksQueue(enableTasksQueue)
204 strictEqual(
205 pool.opts.workerChoiceStrategy,
206 workerChoiceStrategy
207 )
208 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
209 strictEqual(
210 pool.opts.workerChoiceStrategyOptions.measurement,
211 measurement
212 )
213 await runPoolifierPool(pool, {
214 taskExecutions,
215 workerData
216 })
217 }
218 )
219 })
220 }
221 } else {
222 group(name, () => {
223 bench(
224 `${name} with ${workerChoiceStrategy} and ${
225 enableTasksQueue ? 'with' : 'without'
226 } tasks queue`,
227 async () => {
228 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
229 pool.enableTasksQueue(enableTasksQueue)
230 strictEqual(
231 pool.opts.workerChoiceStrategy,
232 workerChoiceStrategy
233 )
234 strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
235 await runPoolifierPool(pool, {
236 taskExecutions,
237 workerData
238 })
239 }
240 )
241 })
242 }
243 }
244 }
245 return pool
246 } catch (error) {
247 console.error(error)
248 }
249}
250
251const LIST_FORMATTER = new Intl.ListFormat('en-US', {
252 style: 'long',
253 type: 'conjunction'
254})
a6bef8d2
JB
255
256export { executeTaskFunction }