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