Commit | Line | Data |
---|---|---|
98f60ddd | 1 | import { bench, group, run } from 'tatami-ng' |
aacd8188 | 2 | |
3a502712 JB |
3 | /** |
4 | * | |
5 | * @param numberOfWorkers | |
ed20267e | 6 | * @returns |
3a502712 | 7 | */ |
292ad316 JB |
8 | function generateWorkersArray (numberOfWorkers) { |
9 | return [...Array(numberOfWorkers).keys()] | |
10 | } | |
aacd8188 | 11 | |
292ad316 | 12 | const workers = generateWorkersArray(60) |
aacd8188 | 13 | |
e843b904 | 14 | let nextWorkerIndex |
aacd8188 | 15 | |
3a502712 | 16 | /** |
ed20267e | 17 | * @returns |
3a502712 | 18 | */ |
0d6f0c13 | 19 | function 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 | 28 | function 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 | 39 | function 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 | 49 | function roundRobinIncrementModulo () { |
aacd8188 S |
50 | const chosenWorker = workers[nextWorkerIndex] |
51 | nextWorkerIndex++ | |
52 | nextWorkerIndex %= workers.length | |
53 | return chosenWorker | |
54 | } | |
55 | ||
0804b9b4 JB |
56 | group('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 | ||
75 | await run({ units: true }) |