From c7ed5d3b88f8f2c75675d953a4e9df87a3d9a54c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 1 Jan 2024 13:47:42 +0100 Subject: [PATCH] refactor: remove home made random integer generator MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/benchmarks-utils.cjs | 13 ------------- benchmarks/worker-selection/least.mjs | 7 ++++--- ...hted-round-robin-worker-choice-strategy.test.mjs | 9 +++------ tests/test-utils.cjs | 13 ------------- 4 files changed, 7 insertions(+), 35 deletions(-) diff --git a/benchmarks/benchmarks-utils.cjs b/benchmarks/benchmarks-utils.cjs index e9491098..4dd1227b 100644 --- a/benchmarks/benchmarks-utils.cjs +++ b/benchmarks/benchmarks-utils.cjs @@ -186,18 +186,6 @@ const LIST_FORMATTER = new Intl.ListFormat('en-US', { type: 'conjunction' }) -const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => { - if (max < min || max < 0 || min < 0) { - throw new RangeError('Invalid interval') - } - max = Math.floor(max) - if (min != null && min !== 0) { - min = Math.ceil(min) - return Math.floor(Math.random() * (max - min + 1)) + min - } - return Math.floor(Math.random() * (max + 1)) -} - const jsonIntegerSerialization = n => { for (let i = 0; i < n; i++) { const o = { @@ -268,6 +256,5 @@ const executeTaskFunction = data => { module.exports = { LIST_FORMATTER, executeTaskFunction, - generateRandomInteger, runPoolifierPoolBenchmark } diff --git a/benchmarks/worker-selection/least.mjs b/benchmarks/worker-selection/least.mjs index 6dd91df0..f67bebf7 100644 --- a/benchmarks/worker-selection/least.mjs +++ b/benchmarks/worker-selection/least.mjs @@ -1,5 +1,6 @@ +import { randomInt } from 'node:crypto' import Benchmark from 'benchmark' -import { LIST_FORMATTER, generateRandomInteger } from '../benchmarks-utils.cjs' +import { LIST_FORMATTER } from '../benchmarks-utils.cjs' function generateRandomTasksMap ( numberOfWorkers, @@ -7,7 +8,7 @@ function generateRandomTasksMap ( ) { 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) @@ -50,7 +51,7 @@ const defaultPivotIndexSelect = (leftIndex, rightIndex) => { } const randomPivotIndexSelect = (leftIndex, rightIndex) => { - return generateRandomInteger(rightIndex, leftIndex) + return randomInt(leftIndex, rightIndex) } function swap (array, index1, index2) { diff --git a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs index 0e756886..95f1397e 100644 --- a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs +++ b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs @@ -1,7 +1,7 @@ +import { randomInt } from 'node:crypto' import { expect } from 'expect' import { FixedThreadPool } from '../../../lib/index.cjs' import { WeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.cjs' -import { generateRandomInteger } from '../../test-utils.cjs' describe('Weighted round robin strategy worker choice strategy test suite', () => { // const min = 1 @@ -21,11 +21,8 @@ describe('Weighted round robin strategy worker choice strategy test suite', () = it('Verify that reset() resets internals', () => { const strategy = new WeightedRoundRobinWorkerChoiceStrategy(pool) - strategy.currentWorkerId = generateRandomInteger(Number.MAX_SAFE_INTEGER, 1) - strategy.workerVirtualTaskRunTime = generateRandomInteger( - Number.MAX_SAFE_INTEGER, - 1 - ) + strategy.currentWorkerId = randomInt(281474976710655) + strategy.workerVirtualTaskRunTime = randomInt(281474976710655) expect(strategy.reset()).toBe(true) expect(strategy.nextWorkerNodeKey).toBe(0) expect(strategy.previousWorkerNodeKey).toBe(0) diff --git a/tests/test-utils.cjs b/tests/test-utils.cjs index 6dba5866..735d406a 100644 --- a/tests/test-utils.cjs +++ b/tests/test-utils.cjs @@ -55,18 +55,6 @@ const sleepTaskFunction = async ( }) } -const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => { - if (max < min || max < 0 || min < 0) { - throw new RangeError('Invalid interval') - } - max = Math.floor(max) - if (min != null && min !== 0) { - min = Math.ceil(min) - return Math.floor(Math.random() * (max - min + 1)) + min - } - return Math.floor(Math.random() * (max + 1)) -} - const jsonIntegerSerialization = n => { for (let i = 0; i < n; i++) { const o = { @@ -116,7 +104,6 @@ module.exports = { executeTaskFunction, factorial, fibonacci, - generateRandomInteger, jsonIntegerSerialization, sleep, sleepTaskFunction, -- 2.34.1