-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
+ * @returns
+ */
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)
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
return [minKey, minValue]
}
+/**
+ *
+ * @param tasksMap
+ * @returns
+ */
function arraySortSelect (tasksMap) {
const tasksArray = Array.from(tasksMap)
return tasksArray.sort((a, b) => {
}
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
+ * @returns
+ */
function partition (
array,
leftIndex,
return storeIndex
}
+/**
+ *
+ * @param array
+ * @param k
+ * @param leftIndex
+ * @param rightIndex
+ * @param compare
+ * @param pivotIndexSelect
+ * @returns
+ */
function selectLoop (
array,
k,
}
}
+/**
+ *
+ * @param array
+ * @param k
+ * @param leftIndex
+ * @param rightIndex
+ * @param compare
+ * @param pivotIndexSelect
+ * @returns
+ */
function selectRecursion (
array,
k,
}
}
+/**
+ *
+ * @param tasksMap
+ * @returns
+ */
function quickSelectLoop (tasksMap) {
const tasksArray = Array.from(tasksMap)
})
}
+/**
+ *
+ * @param tasksMap
+ * @returns
+ */
function quickSelectLoopRandomPivot (tasksMap) {
const tasksArray = Array.from(tasksMap)
)
}
+/**
+ *
+ * @param tasksMap
+ * @returns
+ */
function quickSelectRecursion (tasksMap) {
const tasksArray = Array.from(tasksMap)
})
}
+/**
+ *
+ * @param tasksMap
+ * @returns
+ */
function quickSelectRecursionRandomPivot (tasksMap) {
const tasksArray = Array.from(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 })