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
=
26 runTime
: { median
: false },
27 waitTime
: { median
: false },
28 elu
: { median
: false }
32 * Default measurement statistics requirements.
34 export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
: MeasurementStatisticsRequirements
=
42 * Returns safe host OS optimized estimate of the default amount of parallelism a pool should use.
43 * Always returns a value greater than zero.
45 * @returns The host OS optimized maximum pool size.
47 export const availableParallelism
= (): number => {
48 let availableParallelism
= 1
50 availableParallelism
= os
.availableParallelism()
52 const numberOfCpus
= os
.cpus()
53 if (Array.isArray(numberOfCpus
) && numberOfCpus
.length
> 0) {
54 availableParallelism
= numberOfCpus
.length
57 return availableParallelism
61 * Computes the median of the given data set.
63 * @param dataSet - Data set.
64 * @returns The median of the given data set.
66 export const median
= (dataSet
: number[]): number => {
67 if (Array.isArray(dataSet
) && dataSet
.length
=== 0) {
70 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
73 const sortedDataSet
= dataSet
.slice().sort((a
, b
) => a
- b
)
75 (sortedDataSet
[(sortedDataSet
.length
- 1) >> 1] +
76 sortedDataSet
[sortedDataSet
.length
>> 1]) /
82 * Rounds the given number to the given scale.
83 * The rounding is done using the "round half away from zero" method.
85 * @param num - The number to round.
86 * @param scale - The scale to round to.
87 * @returns The rounded number.
89 export const round
= (num
: number, scale
= 2): number => {
90 const rounder
= Math.pow(10, scale
)
91 return Math.round(num
* rounder
* (1 + Number.EPSILON
)) / rounder
95 * Is the given object a plain object?
97 * @param obj - The object to check.
98 * @returns `true` if the given object is a plain object, `false` otherwise.
100 export const isPlainObject
= (obj
: unknown
): boolean =>
101 typeof obj
=== 'object' &&
103 obj
?.constructor
=== Object &&
104 Object.prototype
.toString
.call(obj
) === '[object Object]'
107 * Detects whether the given value is a kill behavior or not.
109 * @typeParam KB - Which specific KillBehavior type to test against.
110 * @param killBehavior - Which kind of kill behavior to detect.
111 * @param value - Any value.
112 * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
114 export const isKillBehavior
= <KB
extends KillBehavior
>(
118 return value
=== killBehavior
122 * Detects whether the given value is an asynchronous function or not.
124 * @param fn - Any value.
125 * @returns `true` if `fn` was an asynchronous function, otherwise `false`.
127 export const isAsyncFunction
= (
129 ): fn
is (...args
: unknown
[]) => Promise
<unknown
> => {
130 return typeof fn
=== 'function' && fn
.constructor
.name
=== 'AsyncFunction'
134 * Updates the given measurement statistics.
136 * @param measurementStatistics - The measurement statistics to update.
137 * @param measurementRequirements - The measurement statistics requirements.
138 * @param measurementValue - The measurement value.
139 * @param numberOfMeasurements - The number of measurements.
141 export const updateMeasurementStatistics
= (
142 measurementStatistics
: MeasurementStatistics
,
143 measurementRequirements
: MeasurementStatisticsRequirements
,
144 measurementValue
: number,
145 numberOfMeasurements
: number
147 if (measurementRequirements
.aggregate
) {
148 measurementStatistics
.aggregate
=
149 (measurementStatistics
.aggregate
?? 0) + measurementValue
150 measurementStatistics
.minimum
= Math.min(
152 measurementStatistics
.minimum
?? Infinity
154 measurementStatistics
.maximum
= Math.max(
156 measurementStatistics
.maximum
?? -Infinity
158 if (measurementRequirements
.average
&& numberOfMeasurements
!== 0) {
159 measurementStatistics
.average
=
160 measurementStatistics
.aggregate
/ numberOfMeasurements
162 if (measurementRequirements
.median
&& measurementValue
!= null) {
163 measurementStatistics
.history
.push(measurementValue
)
164 measurementStatistics
.median
= median(measurementStatistics
.history
)