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