Commit | Line | Data |
---|---|---|
ca6c7d70 | 1 | const Benchmark = require('benny') |
aacd8188 | 2 | |
292ad316 JB |
3 | function generateWorkersArray (numberOfWorkers) { |
4 | return [...Array(numberOfWorkers).keys()] | |
5 | } | |
aacd8188 | 6 | |
292ad316 | 7 | const workers = generateWorkersArray(60) |
aacd8188 | 8 | |
e843b904 | 9 | let nextWorkerIndex |
aacd8188 | 10 | |
0d6f0c13 | 11 | function roundRobinTernaryOffByOne () { |
aacd8188 S |
12 | nextWorkerIndex = |
13 | workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1 | |
14 | return workers[nextWorkerIndex] | |
15 | } | |
16 | ||
0d6f0c13 | 17 | function roundRobinTernaryWithNegation () { |
472bf1f5 JB |
18 | nextWorkerIndex = |
19 | !nextWorkerIndex || workers.length - 1 === nextWorkerIndex | |
20 | ? 0 | |
21 | : nextWorkerIndex + 1 | |
22 | return workers[nextWorkerIndex] | |
23 | } | |
24 | ||
0d6f0c13 | 25 | function roundRobinTernaryWithPreChoosing () { |
aacd8188 S |
26 | const chosenWorker = workers[nextWorkerIndex] |
27 | nextWorkerIndex = | |
28 | workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1 | |
29 | return chosenWorker | |
30 | } | |
31 | ||
0d6f0c13 | 32 | function roundRobinIncrementModulo () { |
aacd8188 S |
33 | const chosenWorker = workers[nextWorkerIndex] |
34 | nextWorkerIndex++ | |
35 | nextWorkerIndex %= workers.length | |
36 | return chosenWorker | |
37 | } | |
38 | ||
ca6c7d70 | 39 | Benchmark.suite( |
b15ff04e | 40 | 'Round robin tasks distribution', |
ca6c7d70 | 41 | Benchmark.add('Ternary off by one', () => { |
aacd8188 | 42 | nextWorkerIndex = 0 |
0d6f0c13 | 43 | roundRobinTernaryOffByOne() |
ca6c7d70 JB |
44 | }), |
45 | Benchmark.add('Ternary with negation', () => { | |
472bf1f5 | 46 | nextWorkerIndex = 0 |
0d6f0c13 | 47 | roundRobinTernaryWithNegation() |
ca6c7d70 JB |
48 | }), |
49 | Benchmark.add('Ternary with pre-choosing', () => { | |
aacd8188 | 50 | nextWorkerIndex = 0 |
0d6f0c13 | 51 | roundRobinTernaryWithPreChoosing() |
ca6c7d70 JB |
52 | }), |
53 | Benchmark.add('Increment+Modulo', () => { | |
aacd8188 | 54 | nextWorkerIndex = 0 |
0d6f0c13 | 55 | roundRobinIncrementModulo() |
ca6c7d70 JB |
56 | }), |
57 | Benchmark.cycle(), | |
58 | Benchmark.complete() | |
59 | ) |