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