refactor: sensible defaults for worker choice strategy policy
[poolifier.git] / benchmarks / worker-selection / round-robin.mjs
1 import Benchmark from 'benny'
2
3 function generateWorkersArray (numberOfWorkers) {
4 return [...Array(numberOfWorkers).keys()]
5 }
6
7 const workers = generateWorkersArray(60)
8
9 let nextWorkerIndex
10
11 function roundRobinTernaryOffByOne () {
12 nextWorkerIndex =
13 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
14 return workers[nextWorkerIndex]
15 }
16
17 function roundRobinTernaryWithNegation () {
18 nextWorkerIndex =
19 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
20 ? 0
21 : nextWorkerIndex + 1
22 return workers[nextWorkerIndex]
23 }
24
25 function roundRobinTernaryWithPreChoosing () {
26 const chosenWorker = workers[nextWorkerIndex]
27 nextWorkerIndex =
28 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
29 return chosenWorker
30 }
31
32 function roundRobinIncrementModulo () {
33 const chosenWorker = workers[nextWorkerIndex]
34 nextWorkerIndex++
35 nextWorkerIndex %= workers.length
36 return chosenWorker
37 }
38
39 Benchmark.suite(
40 'Round robin tasks distribution',
41 Benchmark.add('Ternary off by one', () => {
42 nextWorkerIndex = 0
43 roundRobinTernaryOffByOne()
44 }),
45 Benchmark.add('Ternary with negation', () => {
46 nextWorkerIndex = 0
47 roundRobinTernaryWithNegation()
48 }),
49 Benchmark.add('Ternary with pre-choosing', () => {
50 nextWorkerIndex = 0
51 roundRobinTernaryWithPreChoosing()
52 }),
53 Benchmark.add('Increment+Modulo', () => {
54 nextWorkerIndex = 0
55 roundRobinIncrementModulo()
56 }),
57 Benchmark.cycle(),
58 Benchmark.complete()
59 )