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.
48 export const availableParallelism
= (): number => {
49 let availableParallelism
= 1
51 availableParallelism
= os
.availableParallelism()
53 const numberOfCpus
= os
.cpus()
54 if (Array.isArray(numberOfCpus
) && numberOfCpus
.length
> 0) {
55 availableParallelism
= numberOfCpus
.length
58 return availableParallelism
62 // * Computes the retry delay in milliseconds using an exponential back off algorithm.
64 // * @param retryNumber - The number of retries that have already been attempted
65 // * @param maxDelayRatio - The maximum ratio of the delay that can be randomized
66 // * @returns Delay in milliseconds
68 // export const exponentialDelay = (
70 // maxDelayRatio = 0.2
72 // const delay = Math.pow(2, retryNumber) * 100
73 // const randomSum = delay * maxDelayRatio * Math.random() // 0-(maxDelayRatio*100)% of the delay
74 // return delay + randomSum
78 * Computes the median of the given data set.
80 * @param dataSet - Data set.
81 * @returns The median of the given data set.
83 export const median
= (dataSet
: number[]): number => {
84 if (Array.isArray(dataSet
) && dataSet
.length
=== 0) {
87 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
90 const sortedDataSet
= dataSet
.slice().sort((a
, b
) => a
- b
)
92 (sortedDataSet
[(sortedDataSet
.length
- 1) >> 1] +
93 sortedDataSet
[sortedDataSet
.length
>> 1]) /
99 * Rounds the given number to the given scale.
100 * The rounding is done using the "round half away from zero" method.
102 * @param num - The number to round.
103 * @param scale - The scale to round to.
104 * @returns The rounded number.
106 export const round
= (num
: number, scale
= 2): number => {
107 const rounder
= Math.pow(10, scale
)
108 return Math.round(num
* rounder
* (1 + Number.EPSILON
)) / rounder
112 * Is the given object a plain object?
114 * @param obj - The object to check.
115 * @returns `true` if the given object is a plain object, `false` otherwise.
117 export const isPlainObject
= (obj
: unknown
): boolean =>
118 typeof obj
=== 'object' &&
120 obj
?.constructor
=== Object &&
121 Object.prototype
.toString
.call(obj
) === '[object Object]'
124 * Detects whether the given value is a kill behavior or not.
126 * @typeParam KB - Which specific KillBehavior type to test against.
127 * @param killBehavior - Which kind of kill behavior to detect.
128 * @param value - Any value.
129 * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
131 export const isKillBehavior
= <KB
extends KillBehavior
>(
135 return value
=== killBehavior
139 * Detects whether the given value is an asynchronous function or not.
141 * @param fn - Any value.
142 * @returns `true` if `fn` was an asynchronous function, otherwise `false`.
144 export const isAsyncFunction
= (
146 ): fn
is (...args
: unknown
[]) => Promise
<unknown
> => {
147 return typeof fn
=== 'function' && fn
.constructor
.name
=== 'AsyncFunction'
151 * Updates the given measurement statistics.
153 * @param measurementStatistics - The measurement statistics to update.
154 * @param measurementRequirements - The measurement statistics requirements.
155 * @param measurementValue - The measurement value.
156 * @param numberOfMeasurements - The number of measurements.
158 export const updateMeasurementStatistics
= (
159 measurementStatistics
: MeasurementStatistics
,
160 measurementRequirements
: MeasurementStatisticsRequirements
,
161 measurementValue
: number,
162 numberOfMeasurements
: number
164 if (measurementRequirements
.aggregate
) {
165 measurementStatistics
.aggregate
=
166 (measurementStatistics
.aggregate
?? 0) + measurementValue
167 measurementStatistics
.minimum
= Math.min(
169 measurementStatistics
.minimum
?? Infinity
171 measurementStatistics
.maximum
= Math.max(
173 measurementStatistics
.maximum
?? -Infinity
175 if (measurementRequirements
.average
&& numberOfMeasurements
!== 0) {
176 measurementStatistics
.average
=
177 measurementStatistics
.aggregate
/ numberOfMeasurements
179 if (measurementRequirements
.median
&& measurementValue
!= null) {
180 measurementStatistics
.history
.push(measurementValue
)
181 measurementStatistics
.median
= median(measurementStatistics
.history
)