refactor: cleanup eslint configuration
[poolifier.git] / benchmarks / worker-selection / round-robin.mjs
CommitLineData
f1c674cd 1import Benchmark from 'benchmark'
ded253e2 2
d35e5717 3import { LIST_FORMATTER } from '../benchmarks-utils.cjs'
aacd8188 4
292ad316
JB
5function generateWorkersArray (numberOfWorkers) {
6 return [...Array(numberOfWorkers).keys()]
7}
aacd8188 8
292ad316 9const workers = generateWorkersArray(60)
aacd8188 10
e843b904 11let nextWorkerIndex
aacd8188 12
0d6f0c13 13function roundRobinTernaryOffByOne () {
aacd8188
S
14 nextWorkerIndex =
15 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
16 return workers[nextWorkerIndex]
17}
18
0d6f0c13 19function roundRobinTernaryWithNegation () {
472bf1f5
JB
20 nextWorkerIndex =
21 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
22 ? 0
23 : nextWorkerIndex + 1
24 return workers[nextWorkerIndex]
25}
26
0d6f0c13 27function roundRobinTernaryWithPreChoosing () {
aacd8188
S
28 const chosenWorker = workers[nextWorkerIndex]
29 nextWorkerIndex =
30 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
31 return chosenWorker
32}
33
0d6f0c13 34function roundRobinIncrementModulo () {
aacd8188
S
35 const chosenWorker = workers[nextWorkerIndex]
36 nextWorkerIndex++
37 nextWorkerIndex %= workers.length
38 return chosenWorker
39}
40
f1c674cd
JB
41new Benchmark.Suite('Round robin tasks distribution')
42 .add('Ternary off by one', () => {
aacd8188 43 nextWorkerIndex = 0
0d6f0c13 44 roundRobinTernaryOffByOne()
f1c674cd
JB
45 })
46 .add('Ternary with negation', () => {
472bf1f5 47 nextWorkerIndex = 0
0d6f0c13 48 roundRobinTernaryWithNegation()
f1c674cd
JB
49 })
50 .add('Ternary with pre-choosing', () => {
aacd8188 51 nextWorkerIndex = 0
0d6f0c13 52 roundRobinTernaryWithPreChoosing()
f1c674cd
JB
53 })
54 .add('Increment+Modulo', () => {
aacd8188 55 nextWorkerIndex = 0
0d6f0c13 56 roundRobinIncrementModulo()
f1c674cd
JB
57 })
58 .on('cycle', event => {
59 console.info(event.target.toString())
60 })
61 .on('complete', function () {
62 console.info(
63 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
64 )
65 })
66 .run()