X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fworker-selection%2Fleast.mjs;h=189a5e6c8f05da89f70fe25e0ffe8dc5a8af7f64;hb=3b594fe1b0f89d6665da2eb2ebdc14eb7628fe70;hp=d08bec0e109a8a0d17adde1a966edcef5f0e1107;hpb=ded253e27e59ae936fe91d789d8454b7eb11dd6a;p=poolifier.git diff --git a/benchmarks/worker-selection/least.mjs b/benchmarks/worker-selection/least.mjs index d08bec0e..189a5e6c 100644 --- a/benchmarks/worker-selection/least.mjs +++ b/benchmarks/worker-selection/least.mjs @@ -1,9 +1,13 @@ import { randomInt } from 'node:crypto' -import Benchmark from 'benchmark' - -import { LIST_FORMATTER } from '../benchmarks-utils.cjs' +import { bench, group, run } from 'tatami-ng' +/** + * + * @param numberOfWorkers + * @param maxNumberOfTasksPerWorker + * @returns + */ function generateRandomTasksMap ( numberOfWorkers, maxNumberOfTasksPerWorker = 10 @@ -18,9 +22,14 @@ function generateRandomTasksMap ( const tasksMap = generateRandomTasksMap(60, 20) +/** + * + * @param tasksMap + * @returns + */ function loopSelect (tasksMap) { let minKey - let minValue = Infinity + let minValue = Number.POSITIVE_INFINITY for (const [key, value] of tasksMap) { if (value === 0) { return key @@ -32,6 +41,11 @@ function loopSelect (tasksMap) { return [minKey, minValue] } +/** + * + * @param tasksMap + * @returns + */ function arraySortSelect (tasksMap) { const tasksArray = Array.from(tasksMap) return tasksArray.sort((a, b) => { @@ -56,12 +70,27 @@ const randomPivotIndexSelect = (leftIndex, rightIndex) => { return randomInt(leftIndex, rightIndex) } +/** + * + * @param array + * @param index1 + * @param index2 + */ function swap (array, index1, index2) { const tmp = array[index1] array[index1] = array[index2] array[index2] = tmp } +/** + * + * @param array + * @param leftIndex + * @param rightIndex + * @param pivotIndex + * @param compare + * @returns + */ function partition ( array, leftIndex, @@ -82,6 +111,16 @@ function partition ( return storeIndex } +/** + * + * @param array + * @param k + * @param leftIndex + * @param rightIndex + * @param compare + * @param pivotIndexSelect + * @returns + */ function selectLoop ( array, k, @@ -104,6 +143,16 @@ function selectLoop ( } } +/** + * + * @param array + * @param k + * @param leftIndex + * @param rightIndex + * @param compare + * @param pivotIndexSelect + * @returns + */ function selectRecursion ( array, k, @@ -124,6 +173,11 @@ function selectRecursion ( } } +/** + * + * @param tasksMap + * @returns + */ function quickSelectLoop (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -132,6 +186,11 @@ function quickSelectLoop (tasksMap) { }) } +/** + * + * @param tasksMap + * @returns + */ function quickSelectLoopRandomPivot (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -147,6 +206,11 @@ function quickSelectLoopRandomPivot (tasksMap) { ) } +/** + * + * @param tasksMap + * @returns + */ function quickSelectRecursion (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -155,6 +219,11 @@ function quickSelectRecursion (tasksMap) { }) } +/** + * + * @param tasksMap + * @returns + */ function quickSelectRecursionRandomPivot (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -170,31 +239,25 @@ function quickSelectRecursionRandomPivot (tasksMap) { ) } -new Benchmark.Suite('Least used worker tasks distribution') - .add('Loop select', () => { +group('Least used worker tasks distribution', () => { + bench('Loop select', () => { loopSelect(tasksMap) }) - .add('Array sort select', () => { + bench('Array sort select', () => { arraySortSelect(tasksMap) }) - .add('Quick select loop', () => { + bench('Quick select loop', () => { quickSelectLoop(tasksMap) }) - .add('Quick select loop with random pivot', () => { + bench('Quick select loop with random pivot', () => { quickSelectLoopRandomPivot(tasksMap) }) - .add('Quick select recursion', () => { + bench('Quick select recursion', () => { quickSelectRecursion(tasksMap) }) - .add('Quick select recursion with random pivot', () => { + bench('Quick select recursion with random pivot', () => { quickSelectRecursionRandomPivot(tasksMap) }) - .on('cycle', event => { - console.info(event.target.toString()) - }) - .on('complete', function () { - console.info( - 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name')) - ) - }) - .run() +}) + +await run({ units: true })