---
[poolifier.git] / benchmarks / worker-selection / round-robin.mjs
1 import { bench, group, run } from 'tatami-ng'
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 group('Round robin tasks distribution', () => {
40 bench('Ternary off by one', () => {
41 nextWorkerIndex = 0
42 roundRobinTernaryOffByOne()
43 })
44 bench('Ternary with negation', () => {
45 nextWorkerIndex = 0
46 roundRobinTernaryWithNegation()
47 })
48 bench('Ternary with pre-choosing', () => {
49 nextWorkerIndex = 0
50 roundRobinTernaryWithPreChoosing()
51 })
52 bench('Increment+Modulo', () => {
53 nextWorkerIndex = 0
54 roundRobinIncrementModulo()
55 })
56 })
57
58 await run({ units: true })