1 import * as os from
'node:os'
3 MeasurementStatisticsRequirements
,
4 WorkerChoiceStrategyOptions
5 } from
'./pools/selection-strategies/selection-strategies-types'
6 import type { KillBehavior
} from
'./worker/worker-options'
7 import type { MeasurementStatistics
} from
'./pools/worker'
12 export const DEFAULT_TASK_NAME
= 'default'
15 * An intentional empty function.
17 export const EMPTY_FUNCTION
: () => void = Object.freeze(() => {
18 /* Intentionally empty */
22 * Default worker choice strategy options.
24 export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
: WorkerChoiceStrategyOptions
=
27 runTime
: { median
: false },
28 waitTime
: { median
: false },
29 elu
: { median
: false }
33 * Default measurement statistics requirements.
35 export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
: MeasurementStatisticsRequirements
=
43 * Returns safe host OS optimized estimate of the default amount of parallelism a pool should use.
44 * Always returns a value greater than zero.
46 * @returns The host OS optimized maximum pool size.
49 export const availableParallelism
= (): number => {
50 let availableParallelism
= 1
52 availableParallelism
= os
.availableParallelism()
54 const numberOfCpus
= os
.cpus()
55 if (Array.isArray(numberOfCpus
) && numberOfCpus
.length
> 0) {
56 availableParallelism
= numberOfCpus
.length
59 return availableParallelism
63 // * Computes the retry delay in milliseconds using an exponential back off algorithm.
65 // * @param retryNumber - The number of retries that have already been attempted
66 // * @param maxDelayRatio - The maximum ratio of the delay that can be randomized
67 // * @returns Delay in milliseconds
70 // export const exponentialDelay = (
72 // maxDelayRatio = 0.2
74 // const delay = Math.pow(2, retryNumber) * 100
75 // const randomSum = delay * maxDelayRatio * Math.random() // 0-(maxDelayRatio*100)% of the delay
76 // return delay + randomSum
80 * Computes the median of the given data set.
82 * @param dataSet - Data set.
83 * @returns The median of the given data set.
86 export const median
= (dataSet
: number[]): number => {
87 if (Array.isArray(dataSet
) && dataSet
.length
=== 0) {
90 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
93 const sortedDataSet
= dataSet
.slice().sort((a
, b
) => a
- b
)
95 (sortedDataSet
[(sortedDataSet
.length
- 1) >> 1] +
96 sortedDataSet
[sortedDataSet
.length
>> 1]) /
102 * Rounds the given number to the given scale.
103 * The rounding is done using the "round half away from zero" method.
105 * @param num - The number to round.
106 * @param scale - The scale to round to.
107 * @returns The rounded number.
109 export const round
= (num
: number, scale
= 2): number => {
110 const rounder
= Math.pow(10, scale
)
111 return Math.round(num
* rounder
* (1 + Number.EPSILON
)) / rounder
115 * Is the given object a plain object?
117 * @param obj - The object to check.
118 * @returns `true` if the given object is a plain object, `false` otherwise.
120 export const isPlainObject
= (obj
: unknown
): boolean =>
121 typeof obj
=== 'object' &&
123 obj
?.constructor
=== Object &&
124 Object.prototype
.toString
.call(obj
) === '[object Object]'
127 * Detects whether the given value is a kill behavior or not.
129 * @typeParam KB - Which specific KillBehavior type to test against.
130 * @param killBehavior - Which kind of kill behavior to detect.
131 * @param value - Any value.
132 * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
135 export const isKillBehavior
= <KB
extends KillBehavior
>(
139 return value
=== killBehavior
143 * Detects whether the given value is an asynchronous function or not.
145 * @param fn - Any value.
146 * @returns `true` if `fn` was an asynchronous function, otherwise `false`.
148 export const isAsyncFunction
= (
150 ): fn
is (...args
: unknown
[]) => Promise
<unknown
> => {
151 return typeof fn
=== 'function' && fn
.constructor
.name
=== 'AsyncFunction'
155 * Updates the given measurement statistics.
157 * @param measurementStatistics - The measurement statistics to update.
158 * @param measurementRequirements - The measurement statistics requirements.
159 * @param measurementValue - The measurement value.
160 * @param numberOfMeasurements - The number of measurements.
163 export const updateMeasurementStatistics
= (
164 measurementStatistics
: MeasurementStatistics
,
165 measurementRequirements
: MeasurementStatisticsRequirements
,
166 measurementValue
: number,
167 numberOfMeasurements
: number
169 if (measurementRequirements
.aggregate
) {
170 measurementStatistics
.aggregate
=
171 (measurementStatistics
.aggregate
?? 0) + measurementValue
172 measurementStatistics
.minimum
= Math.min(
174 measurementStatistics
.minimum
?? Infinity
176 measurementStatistics
.maximum
= Math.max(
178 measurementStatistics
.maximum
?? -Infinity
180 if (measurementRequirements
.average
&& numberOfMeasurements
!== 0) {
181 measurementStatistics
.average
=
182 measurementStatistics
.aggregate
/ numberOfMeasurements
184 if (measurementRequirements
.median
&& measurementValue
!= null) {
185 measurementStatistics
.history
.push(measurementValue
)
186 measurementStatistics
.median
= median(measurementStatistics
.history
)
192 * Executes a function once at a time.
194 * @param fn - The function to execute.
195 * @param context - The context to bind the function to.
196 * @returns The function to execute.
198 export const once
= (
199 // eslint-disable-next-line @typescript-eslint/no-explicit-any
200 fn
: (...args
: any[]) => void,
202 // eslint-disable-next-line @typescript-eslint/no-explicit-any
203 ): ((...args
: any[]) => void) => {
205 // eslint-disable-next-line @typescript-eslint/no-explicit-any
206 return function (...args
: any[]): void {
209 fn
.apply(context
, args
)