build(deps-dev): apply updates
[poolifier.git] / benchmarks / worker-selection / round-robin.mjs
1 import { bench, group, run } from 'tatami-ng'
2
3 /**
4 *
5 * @param numberOfWorkers
6 */
7 function generateWorkersArray (numberOfWorkers) {
8 return [...Array(numberOfWorkers).keys()]
9 }
10
11 const workers = generateWorkersArray(60)
12
13 let nextWorkerIndex
14
15 /**
16 *
17 */
18 function roundRobinTernaryOffByOne () {
19 nextWorkerIndex =
20 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
21 return workers[nextWorkerIndex]
22 }
23
24 /**
25 *
26 */
27 function roundRobinTernaryWithNegation () {
28 nextWorkerIndex =
29 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
30 ? 0
31 : nextWorkerIndex + 1
32 return workers[nextWorkerIndex]
33 }
34
35 /**
36 *
37 */
38 function roundRobinTernaryWithPreChoosing () {
39 const chosenWorker = workers[nextWorkerIndex]
40 nextWorkerIndex =
41 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
42 return chosenWorker
43 }
44
45 /**
46 *
47 */
48 function roundRobinIncrementModulo () {
49 const chosenWorker = workers[nextWorkerIndex]
50 nextWorkerIndex++
51 nextWorkerIndex %= workers.length
52 return chosenWorker
53 }
54
55 group('Round robin tasks distribution', () => {
56 bench('Ternary off by one', () => {
57 nextWorkerIndex = 0
58 roundRobinTernaryOffByOne()
59 })
60 bench('Ternary with negation', () => {
61 nextWorkerIndex = 0
62 roundRobinTernaryWithNegation()
63 })
64 bench('Ternary with pre-choosing', () => {
65 nextWorkerIndex = 0
66 roundRobinTernaryWithPreChoosing()
67 })
68 bench('Increment+Modulo', () => {
69 nextWorkerIndex = 0
70 roundRobinIncrementModulo()
71 })
72 })
73
74 await run({ units: true })