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