X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fworker-selection%2Fleast.mjs;h=5c9ff1ba6f7698d345c71b9e67a0ab1ad9ee47e2;hb=3a5027122ca6401ae1d755843b20f714c61e3240;hp=0969369fb6e1a49c6b10563aa5ab5b6fc1935b5b;hpb=ab7bb4f826a86d6d121b8902d6449f9fc4e59d70;p=poolifier.git diff --git a/benchmarks/worker-selection/least.mjs b/benchmarks/worker-selection/least.mjs index 0969369f..5c9ff1ba 100644 --- a/benchmarks/worker-selection/least.mjs +++ b/benchmarks/worker-selection/least.mjs @@ -1,13 +1,19 @@ -import Benchmark from 'benchmark' -import { LIST_FORMATTER, generateRandomInteger } from '../benchmarks-utils.js' +import { randomInt } from 'node:crypto' +import { bench, group, run } from 'tatami-ng' + +/** + * + * @param numberOfWorkers + * @param maxNumberOfTasksPerWorker + */ function generateRandomTasksMap ( numberOfWorkers, maxNumberOfTasksPerWorker = 10 ) { const tasksArray = [] for (let i = 0; i < numberOfWorkers; i++) { - const task = [i, generateRandomInteger(maxNumberOfTasksPerWorker)] + const task = [i, randomInt(maxNumberOfTasksPerWorker)] tasksArray.push(task) } return new Map(tasksArray) @@ -15,9 +21,13 @@ function generateRandomTasksMap ( const tasksMap = generateRandomTasksMap(60, 20) +/** + * + * @param tasksMap + */ function loopSelect (tasksMap) { let minKey - let minValue = Infinity + let minValue = Number.POSITIVE_INFINITY for (const [key, value] of tasksMap) { if (value === 0) { return key @@ -29,6 +39,10 @@ function loopSelect (tasksMap) { return [minKey, minValue] } +/** + * + * @param tasksMap + */ function arraySortSelect (tasksMap) { const tasksArray = Array.from(tasksMap) return tasksArray.sort((a, b) => { @@ -50,15 +64,29 @@ const defaultPivotIndexSelect = (leftIndex, rightIndex) => { } const randomPivotIndexSelect = (leftIndex, rightIndex) => { - return generateRandomInteger(rightIndex, leftIndex) + 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 + */ function partition ( array, leftIndex, @@ -79,6 +107,15 @@ function partition ( return storeIndex } +/** + * + * @param array + * @param k + * @param leftIndex + * @param rightIndex + * @param compare + * @param pivotIndexSelect + */ function selectLoop ( array, k, @@ -101,6 +138,15 @@ function selectLoop ( } } +/** + * + * @param array + * @param k + * @param leftIndex + * @param rightIndex + * @param compare + * @param pivotIndexSelect + */ function selectRecursion ( array, k, @@ -121,6 +167,10 @@ function selectRecursion ( } } +/** + * + * @param tasksMap + */ function quickSelectLoop (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -129,6 +179,10 @@ function quickSelectLoop (tasksMap) { }) } +/** + * + * @param tasksMap + */ function quickSelectLoopRandomPivot (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -144,6 +198,10 @@ function quickSelectLoopRandomPivot (tasksMap) { ) } +/** + * + * @param tasksMap + */ function quickSelectRecursion (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -152,6 +210,10 @@ function quickSelectRecursion (tasksMap) { }) } +/** + * + * @param tasksMap + */ function quickSelectRecursionRandomPivot (tasksMap) { const tasksArray = Array.from(tasksMap) @@ -167,31 +229,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 })