Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/express-hybrid...
[poolifier.git] / benchmarks / worker-selection / round-robin.mjs
... / ...
CommitLineData
1import { bench, group, run } from 'tatami-ng'
2
3function generateWorkersArray (numberOfWorkers) {
4 return [...Array(numberOfWorkers).keys()]
5}
6
7const workers = generateWorkersArray(60)
8
9let nextWorkerIndex
10
11function roundRobinTernaryOffByOne () {
12 nextWorkerIndex =
13 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
14 return workers[nextWorkerIndex]
15}
16
17function roundRobinTernaryWithNegation () {
18 nextWorkerIndex =
19 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
20 ? 0
21 : nextWorkerIndex + 1
22 return workers[nextWorkerIndex]
23}
24
25function roundRobinTernaryWithPreChoosing () {
26 const chosenWorker = workers[nextWorkerIndex]
27 nextWorkerIndex =
28 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
29 return chosenWorker
30}
31
32function roundRobinIncrementModulo () {
33 const chosenWorker = workers[nextWorkerIndex]
34 nextWorkerIndex++
35 nextWorkerIndex %= workers.length
36 return chosenWorker
37}
38
39group('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
58await run({ units: true })