Commit | Line | Data |
---|---|---|
aa4bf4b2 | 1 | import * as os from 'node:os' |
19910d84 | 2 | import { getRandomValues, randomInt } from 'node:crypto' |
75de9f41 JB |
3 | import { Worker as ClusterWorker } from 'node:cluster' |
4 | import { Worker as ThreadWorker } from 'node:worker_threads' | |
00e1bdeb | 5 | import { cpus } from 'node:os' |
3c93feb9 | 6 | import type { |
39618ede JB |
7 | MeasurementStatisticsRequirements, |
8 | WorkerChoiceStrategyOptions | |
d35e5717 JB |
9 | } from './pools/selection-strategies/selection-strategies-types.js' |
10 | import type { KillBehavior } from './worker/worker-options.js' | |
11 | import { type IWorker, type WorkerType, WorkerTypes } from './pools/worker.js' | |
39618ede | 12 | import type { IPool } from './pools/pool.js' |
bbeadd16 | 13 | |
ff128cc9 JB |
14 | /** |
15 | * Default task name. | |
16 | */ | |
17 | export const DEFAULT_TASK_NAME = 'default' | |
18 | ||
6e9d10db JB |
19 | /** |
20 | * An intentional empty function. | |
21 | */ | |
4f3c3d89 | 22 | export const EMPTY_FUNCTION: () => void = Object.freeze(() => { |
6e9d10db | 23 | /* Intentionally empty */ |
4f3c3d89 | 24 | }) |
78099a15 | 25 | |
3c93feb9 JB |
26 | /** |
27 | * Default measurement statistics requirements. | |
28 | */ | |
29 | export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: MeasurementStatisticsRequirements = | |
30 | { | |
31 | aggregate: false, | |
32 | average: false, | |
33 | median: false | |
34 | } | |
35 | ||
51474716 | 36 | /** |
ab80dc46 JB |
37 | * Returns safe host OS optimized estimate of the default amount of parallelism a pool should use. |
38 | * Always returns a value greater than zero. | |
39 | * | |
40 | * @returns The host OS optimized maximum pool size. | |
51474716 JB |
41 | */ |
42 | export const availableParallelism = (): number => { | |
43 | let availableParallelism = 1 | |
44 | try { | |
aa4bf4b2 | 45 | availableParallelism = os.availableParallelism() |
51474716 | 46 | } catch { |
562a4037 JB |
47 | const cpus = os.cpus() |
48 | if (Array.isArray(cpus) && cpus.length > 0) { | |
49 | availableParallelism = cpus.length | |
51474716 JB |
50 | } |
51 | } | |
52 | return availableParallelism | |
53 | } | |
54 | ||
9fe8fd69 JB |
55 | /** |
56 | * Returns the worker type of the given worker. | |
57 | * | |
58 | * @param worker - The worker to get the type of. | |
59 | * @returns The worker type of the given worker. | |
60 | * @internal | |
61 | */ | |
187601ff | 62 | export const getWorkerType = (worker: IWorker): WorkerType | undefined => { |
9fe8fd69 JB |
63 | if (worker instanceof ThreadWorker) { |
64 | return WorkerTypes.thread | |
65 | } else if (worker instanceof ClusterWorker) { | |
66 | return WorkerTypes.cluster | |
67 | } | |
68 | } | |
69 | ||
70 | /** | |
71 | * Returns the worker id of the given worker. | |
72 | * | |
73 | * @param worker - The worker to get the id of. | |
74 | * @returns The worker id of the given worker. | |
75 | * @internal | |
76 | */ | |
187601ff | 77 | export const getWorkerId = (worker: IWorker): number | undefined => { |
9fe8fd69 JB |
78 | if (worker instanceof ThreadWorker) { |
79 | return worker.threadId | |
80 | } else if (worker instanceof ClusterWorker) { | |
81 | return worker.id | |
82 | } | |
83 | } | |
84 | ||
68cbdc84 JB |
85 | /** |
86 | * Sleeps for the given amount of milliseconds. | |
87 | * | |
88 | * @param ms - The amount of milliseconds to sleep. | |
89 | * @returns A promise that resolves after the given amount of milliseconds. | |
57a29f75 | 90 | * @internal |
68cbdc84 JB |
91 | */ |
92 | export const sleep = async (ms: number): Promise<void> => { | |
041dc05b | 93 | await new Promise(resolve => { |
68cbdc84 JB |
94 | setTimeout(resolve, ms) |
95 | }) | |
96 | } | |
97 | ||
98 | /** | |
99 | * Computes the retry delay in milliseconds using an exponential back off algorithm. | |
100 | * | |
101 | * @param retryNumber - The number of retries that have already been attempted | |
147be6fe | 102 | * @param delayFactor - The base delay factor in milliseconds |
68cbdc84 JB |
103 | * @returns Delay in milliseconds |
104 | * @internal | |
105 | */ | |
106 | export const exponentialDelay = ( | |
107 | retryNumber = 0, | |
147be6fe | 108 | delayFactor = 100 |
68cbdc84 | 109 | ): number => { |
147be6fe JB |
110 | const delay = Math.pow(2, retryNumber) * delayFactor |
111 | const randomSum = delay * 0.2 * secureRandom() // 0-20% of the delay | |
68cbdc84 JB |
112 | return delay + randomSum |
113 | } | |
8990357d | 114 | |
dc021bcc JB |
115 | /** |
116 | * Computes the average of the given data set. | |
117 | * | |
118 | * @param dataSet - Data set. | |
119 | * @returns The average of the given data set. | |
120 | * @internal | |
121 | */ | |
122 | export const average = (dataSet: number[]): number => { | |
123 | if (Array.isArray(dataSet) && dataSet.length === 0) { | |
124 | return 0 | |
125 | } | |
126 | if (Array.isArray(dataSet) && dataSet.length === 1) { | |
127 | return dataSet[0] | |
128 | } | |
129 | return ( | |
130 | dataSet.reduce((accumulator, number) => accumulator + number, 0) / | |
131 | dataSet.length | |
132 | ) | |
133 | } | |
134 | ||
bbeadd16 | 135 | /** |
afe0d5bf | 136 | * Computes the median of the given data set. |
78099a15 JB |
137 | * |
138 | * @param dataSet - Data set. | |
139 | * @returns The median of the given data set. | |
4bffc062 | 140 | * @internal |
78099a15 JB |
141 | */ |
142 | export const median = (dataSet: number[]): number => { | |
4a45e8d2 JB |
143 | if (Array.isArray(dataSet) && dataSet.length === 0) { |
144 | return 0 | |
145 | } | |
78099a15 JB |
146 | if (Array.isArray(dataSet) && dataSet.length === 1) { |
147 | return dataSet[0] | |
148 | } | |
c6f42dd6 JB |
149 | const sortedDataSet = dataSet.slice().sort((a, b) => a - b) |
150 | return ( | |
151 | (sortedDataSet[(sortedDataSet.length - 1) >> 1] + | |
152 | sortedDataSet[sortedDataSet.length >> 1]) / | |
153 | 2 | |
154 | ) | |
78099a15 | 155 | } |
0d80593b | 156 | |
afe0d5bf JB |
157 | /** |
158 | * Rounds the given number to the given scale. | |
64383951 | 159 | * The rounding is done using the "round half away from zero" method. |
afe0d5bf JB |
160 | * |
161 | * @param num - The number to round. | |
162 | * @param scale - The scale to round to. | |
163 | * @returns The rounded number. | |
57a29f75 | 164 | * @internal |
afe0d5bf JB |
165 | */ |
166 | export const round = (num: number, scale = 2): number => { | |
167 | const rounder = Math.pow(10, scale) | |
168 | return Math.round(num * rounder * (1 + Number.EPSILON)) / rounder | |
169 | } | |
170 | ||
3c653a03 JB |
171 | /** |
172 | * Is the given object a plain object? | |
173 | * | |
174 | * @param obj - The object to check. | |
175 | * @returns `true` if the given object is a plain object, `false` otherwise. | |
57a29f75 | 176 | * @internal |
3c653a03 | 177 | */ |
0d80593b JB |
178 | export const isPlainObject = (obj: unknown): boolean => |
179 | typeof obj === 'object' && | |
180 | obj !== null && | |
c63a35a0 | 181 | obj.constructor === Object && |
0d80593b | 182 | Object.prototype.toString.call(obj) === '[object Object]' |
59317253 JB |
183 | |
184 | /** | |
185 | * Detects whether the given value is a kill behavior or not. | |
186 | * | |
187 | * @typeParam KB - Which specific KillBehavior type to test against. | |
188 | * @param killBehavior - Which kind of kill behavior to detect. | |
189 | * @param value - Any value. | |
190 | * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`. | |
4bffc062 | 191 | * @internal |
59317253 JB |
192 | */ |
193 | export const isKillBehavior = <KB extends KillBehavior>( | |
194 | killBehavior: KB, | |
195 | value: unknown | |
196 | ): value is KB => { | |
197 | return value === killBehavior | |
198 | } | |
49d1b48c JB |
199 | |
200 | /** | |
201 | * Detects whether the given value is an asynchronous function or not. | |
202 | * | |
203 | * @param fn - Any value. | |
204 | * @returns `true` if `fn` was an asynchronous function, otherwise `false`. | |
57a29f75 | 205 | * @internal |
49d1b48c JB |
206 | */ |
207 | export const isAsyncFunction = ( | |
208 | fn: unknown | |
209 | ): fn is (...args: unknown[]) => Promise<unknown> => { | |
210 | return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction' | |
211 | } | |
e4f20deb | 212 | |
68cbdc84 | 213 | /** |
57a29f75 | 214 | * Generates a cryptographically secure random number in the [0,1[ range |
68cbdc84 JB |
215 | * |
216 | * @returns A number in the [0,1[ range | |
57a29f75 | 217 | * @internal |
68cbdc84 | 218 | */ |
970b38d6 | 219 | export const secureRandom = (): number => { |
304d379e | 220 | return getRandomValues(new Uint32Array(1))[0] / 0x100000000 |
68cbdc84 | 221 | } |
68e7ed58 | 222 | |
57a29f75 JB |
223 | /** |
224 | * Returns the minimum of the given numbers. | |
225 | * If no numbers are given, `Infinity` is returned. | |
226 | * | |
227 | * @param args - The numbers to get the minimum of. | |
228 | * @returns The minimum of the given numbers. | |
229 | * @internal | |
230 | */ | |
90d6701c JB |
231 | export const min = (...args: number[]): number => |
232 | args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) | |
233 | ||
57a29f75 JB |
234 | /** |
235 | * Returns the maximum of the given numbers. | |
236 | * If no numbers are given, `-Infinity` is returned. | |
237 | * | |
238 | * @param args - The numbers to get the maximum of. | |
239 | * @returns The maximum of the given numbers. | |
240 | * @internal | |
241 | */ | |
90d6701c JB |
242 | export const max = (...args: number[]): number => |
243 | args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) | |
d91689fd JB |
244 | |
245 | /** | |
246 | * Wraps a function so that it can only be called once. | |
247 | * | |
248 | * @param fn - The function to wrap. | |
249 | * @param context - The context to bind the function to. | |
250 | * @returns The wrapped function. | |
251 | * @internal | |
252 | */ | |
253 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
254 | export const once = <T, A extends any[], R>( | |
255 | fn: (...args: A) => R, | |
256 | context: T | |
257 | ): ((...args: A) => R) => { | |
258 | let result: R | |
259 | return (...args: A) => { | |
c63a35a0 | 260 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
d91689fd JB |
261 | if (fn != null) { |
262 | result = fn.apply<T, A, R>(context, args) | |
263 | ;(fn as unknown as undefined) = (context as unknown as undefined) = | |
264 | undefined | |
265 | } | |
266 | return result | |
267 | } | |
268 | } | |
00e1bdeb | 269 | |
39618ede JB |
270 | export const getWorkerChoiceStrategyRetries = < |
271 | Worker extends IWorker, | |
272 | Data, | |
273 | Response | |
274 | >( | |
275 | pool: IPool<Worker, Data, Response>, | |
276 | opts?: WorkerChoiceStrategyOptions | |
277 | ): number => { | |
278 | return ( | |
279 | pool.info.maxSize + | |
280 | Object.keys(opts?.weights ?? getDefaultWeights(pool.info.maxSize)).length | |
281 | ) | |
282 | } | |
283 | ||
d4efe3b8 JB |
284 | const clone = <T>(object: T): T => { |
285 | return structuredClone<T>(object) | |
00e1bdeb JB |
286 | } |
287 | ||
39618ede JB |
288 | export const buildWorkerChoiceStrategyOptions = < |
289 | Worker extends IWorker, | |
290 | Data, | |
291 | Response | |
292 | >( | |
293 | pool: IPool<Worker, Data, Response>, | |
294 | opts?: WorkerChoiceStrategyOptions | |
295 | ): WorkerChoiceStrategyOptions => { | |
00e1bdeb | 296 | opts = clone(opts ?? {}) |
c63a35a0 | 297 | opts.weights = opts.weights ?? getDefaultWeights(pool.info.maxSize) |
00e1bdeb | 298 | return { |
39618ede JB |
299 | ...{ |
300 | runTime: { median: false }, | |
301 | waitTime: { median: false }, | |
302 | elu: { median: false } | |
303 | }, | |
00e1bdeb JB |
304 | ...opts |
305 | } | |
306 | } | |
307 | ||
308 | const getDefaultWeights = ( | |
309 | poolMaxSize: number, | |
ee79e8e5 | 310 | defaultWorkerWeight?: number |
00e1bdeb | 311 | ): Record<number, number> => { |
ee79e8e5 | 312 | defaultWorkerWeight = defaultWorkerWeight ?? getDefaultWorkerWeight() |
00e1bdeb JB |
313 | const weights: Record<number, number> = {} |
314 | for (let workerNodeKey = 0; workerNodeKey < poolMaxSize; workerNodeKey++) { | |
315 | weights[workerNodeKey] = defaultWorkerWeight | |
316 | } | |
317 | return weights | |
318 | } | |
319 | ||
320 | const getDefaultWorkerWeight = (): number => { | |
19910d84 | 321 | const cpuSpeed = randomInt(500, 2500) |
00e1bdeb JB |
322 | let cpusCycleTimeWeight = 0 |
323 | for (const cpu of cpus()) { | |
c63a35a0 | 324 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
19910d84 JB |
325 | if (cpu.speed == null || cpu.speed === 0) { |
326 | cpu.speed = | |
c63a35a0 | 327 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
19910d84 JB |
328 | cpus().find(cpu => cpu.speed != null && cpu.speed !== 0)?.speed ?? |
329 | cpuSpeed | |
330 | } | |
00e1bdeb JB |
331 | // CPU estimated cycle time |
332 | const numberOfDigits = cpu.speed.toString().length - 1 | |
333 | const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits)) | |
334 | cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits) | |
335 | } | |
336 | return Math.round(cpusCycleTimeWeight / cpus().length) | |
337 | } |