X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fworker-selection%2Fround-robin.mjs;fp=benchmarks%2Fworker-selection%2Fround-robin.mjs;h=b724a04ad5f6acc009bb86b0fd7e32318757a07b;hb=8f810074232deefe64634a8942b1db5b8d3bb0dc;hp=0000000000000000000000000000000000000000;hpb=8a97042123ae9a0404637711b8da7c6e7e4424c7;p=poolifier.git diff --git a/benchmarks/worker-selection/round-robin.mjs b/benchmarks/worker-selection/round-robin.mjs new file mode 100644 index 00000000..b724a04a --- /dev/null +++ b/benchmarks/worker-selection/round-robin.mjs @@ -0,0 +1,59 @@ +import Benchmark from 'benny' + +function generateWorkersArray (numberOfWorkers) { + return [...Array(numberOfWorkers).keys()] +} + +const workers = generateWorkersArray(60) + +let nextWorkerIndex + +function roundRobinTernaryOffByOne () { + nextWorkerIndex = + workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1 + return workers[nextWorkerIndex] +} + +function roundRobinTernaryWithNegation () { + nextWorkerIndex = + !nextWorkerIndex || workers.length - 1 === nextWorkerIndex + ? 0 + : nextWorkerIndex + 1 + return workers[nextWorkerIndex] +} + +function roundRobinTernaryWithPreChoosing () { + const chosenWorker = workers[nextWorkerIndex] + nextWorkerIndex = + workers.length - 1 === nextWorkerIndex ? 0 : nextWorkerIndex + 1 + return chosenWorker +} + +function roundRobinIncrementModulo () { + const chosenWorker = workers[nextWorkerIndex] + nextWorkerIndex++ + nextWorkerIndex %= workers.length + return chosenWorker +} + +Benchmark.suite( + 'Round robin tasks distribution', + Benchmark.add('Ternary off by one', () => { + nextWorkerIndex = 0 + roundRobinTernaryOffByOne() + }), + Benchmark.add('Ternary with negation', () => { + nextWorkerIndex = 0 + roundRobinTernaryWithNegation() + }), + Benchmark.add('Ternary with pre-choosing', () => { + nextWorkerIndex = 0 + roundRobinTernaryWithPreChoosing() + }), + Benchmark.add('Increment+Modulo', () => { + nextWorkerIndex = 0 + roundRobinIncrementModulo() + }), + Benchmark.cycle(), + Benchmark.complete() +)