readonly elu?: MeasurementOptions
/**
* Worker weights to use for weighted round robin worker selection strategies.
- * A weight is the tasks maximum execution time in milliseconds for each worker node.
+ * A weight is tasks maximum execution time in milliseconds for a worker node.
*
* @defaultValue Weights computed automatically given the CPU performance.
*/
import * as os from 'node:os'
+import { webcrypto } from 'node:crypto'
import type {
MeasurementStatisticsRequirements,
WorkerChoiceStrategyOptions
* @returns A number in the [0,1[ range
*/
export const secureRandom = (): number => {
- return crypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000
+ return webcrypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000
}
+const { randomInt } = require('crypto')
const { expect } = require('expect')
const {
CircularArray,
const {
availableParallelism,
average,
+ exponentialDelay,
isAsyncFunction,
isKillBehavior,
isPlainObject,
median,
round,
secureRandom,
+ sleep,
updateMeasurementStatistics
} = require('../lib/utils')
const { KillBehaviors } = require('../lib/worker/worker-options')
expect(Number.isSafeInteger(availableParallelism())).toBe(true)
})
+ it('Verify sleep() behavior', async () => {
+ const now = performance.now()
+ await sleep(1000)
+ const elapsed = performance.now() - now
+ expect(elapsed).toBeGreaterThanOrEqual(1000)
+ })
+
+ it('Verify exponentialDelay() behavior', () => {
+ expect(typeof exponentialDelay(randomInt(1000)) === 'number').toBe(true)
+ expect(exponentialDelay(randomInt(1000))).toBeGreaterThanOrEqual(
+ Number.MIN_VALUE
+ )
+ expect(exponentialDelay(randomInt(1000))).toBeLessThanOrEqual(
+ Number.MAX_VALUE
+ )
+ })
+
it('Verify average() computation', () => {
expect(average([])).toBe(0)
expect(average([0.08])).toBe(0.08)