Update documentation
[poolifier.git] / benchmarks / worker-selection / round-robin.js
... / ...
CommitLineData
1const Benchmark = require('benchmark')
2const { LIST_FORMATTER } = require('../benchmarks-utils')
3
4const suite = new Benchmark.Suite()
5
6function generateWorkersArray (numberOfWorkers) {
7 return [...Array(numberOfWorkers).keys()]
8}
9
10const workers = generateWorkersArray(60)
11
12let nextWorkerIndex
13
14function roundRobinTernaryOffByOne () {
15 nextWorkerIndex =
16 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
17 return workers[nextWorkerIndex]
18}
19
20function roundRobinTernaryWithNegation () {
21 nextWorkerIndex =
22 !nextWorkerIndex || workers.length - 1 === nextWorkerIndex
23 ? 0
24 : nextWorkerIndex + 1
25 return workers[nextWorkerIndex]
26}
27
28function roundRobinTernaryWithPreChoosing () {
29 const chosenWorker = workers[nextWorkerIndex]
30 nextWorkerIndex =
31 workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1
32 return chosenWorker
33}
34
35function roundRobinIncrementModulo () {
36 const chosenWorker = workers[nextWorkerIndex]
37 nextWorkerIndex++
38 nextWorkerIndex %= workers.length
39 return chosenWorker
40}
41
42suite
43 .add('Ternary off by one', function () {
44 nextWorkerIndex = 0
45 roundRobinTernaryOffByOne()
46 })
47 .add('Ternary with negation', function () {
48 nextWorkerIndex = 0
49 roundRobinTernaryWithNegation()
50 })
51 .add('Ternary with pre-choosing', function () {
52 nextWorkerIndex = 0
53 roundRobinTernaryWithPreChoosing()
54 })
55 .add('Increment+Modulo', function () {
56 nextWorkerIndex = 0
57 roundRobinIncrementModulo()
58 })
59 .on('cycle', function (event) {
60 console.log(event.target.toString())
61 })
62 .on('complete', function () {
63 console.log(
64 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
65 )
66 // eslint-disable-next-line n/no-process-exit
67 process.exit()
68 })
69 .run()