X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils.ts;h=e1d6c11859936dfef95d5e4777e509ca50df5f0f;hb=b4c2a50df77a5f3110d228436ce9f3008cbd8297;hp=b84c4c803efe974b1dde4d8e3988465bef6d94b7;hpb=98446b391031d7add0e0d7f7e310e25a014370e2;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index b84c4c80..e1d6c118 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,18 @@ import * as os from 'node:os' import { webcrypto } from 'node:crypto' +import { Worker as ClusterWorker } from 'node:cluster' +import { Worker as ThreadWorker } from 'node:worker_threads' import type { MeasurementStatisticsRequirements, WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' import type { KillBehavior } from './worker/worker-options' -import type { MeasurementStatistics } from './pools/worker' +import { + type IWorker, + type MeasurementStatistics, + type WorkerType, + WorkerTypes +} from './pools/worker' /** * Default task name. @@ -24,7 +31,7 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => { */ export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = { - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -52,14 +59,48 @@ export const availableParallelism = (): number => { try { availableParallelism = os.availableParallelism() } catch { - const numberOfCpus = os.cpus() - if (Array.isArray(numberOfCpus) && numberOfCpus.length > 0) { - availableParallelism = numberOfCpus.length + const cpus = os.cpus() + if (Array.isArray(cpus) && cpus.length > 0) { + availableParallelism = cpus.length } } return availableParallelism } +/** + * Returns the worker type of the given worker. + * + * @param worker - The worker to get the type of. + * @returns The worker type of the given worker. + * @internal + */ +export const getWorkerType = ( + worker: Worker +): WorkerType | undefined => { + if (worker instanceof ThreadWorker) { + return WorkerTypes.thread + } else if (worker instanceof ClusterWorker) { + return WorkerTypes.cluster + } +} + +/** + * Returns the worker id of the given worker. + * + * @param worker - The worker to get the id of. + * @returns The worker id of the given worker. + * @internal + */ +export const getWorkerId = ( + worker: Worker +): number | undefined => { + if (worker instanceof ThreadWorker) { + return worker.threadId + } else if (worker instanceof ClusterWorker) { + return worker.id + } +} + /** * Sleeps for the given amount of milliseconds. * @@ -216,38 +257,18 @@ export const updateMeasurementStatistics = ( measurementStatistics.history.push(measurementValue) if (measurementRequirements.average) { measurementStatistics.average = average(measurementStatistics.history) + } else if (measurementStatistics.average != null) { + delete measurementStatistics.average } if (measurementRequirements.median) { measurementStatistics.median = median(measurementStatistics.history) + } else if (measurementStatistics.median != null) { + delete measurementStatistics.median } } } } -/** - * Executes a function once at a time. - * - * @param fn - The function to execute. - * @param context - The context to bind the function to. - * @returns The function to execute. - */ -export const once = ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - fn: (...args: any[]) => void, - context: unknown - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): ((...args: any[]) => void) => { - let called = false - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return function (...args: any[]): void { - if (!called) { - called = true - fn.apply(context, args) - called = false - } - } -} - /** * Generate a cryptographically secure random number in the [0,1[ range *