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