X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Futils.ts;h=eb800c4ab50f9d39e2b6fc98ebb4cf39bfc134b5;hb=ec2627347c1e7ed1a83a149cbff649421c8b29f0;hp=4aac5cda47d361fd723198a53ad44f254235b56c;hpb=bde6b5d701d895c3e1b447f48750d42a7ac66603;p=poolifier.git diff --git a/src/pools/utils.ts b/src/pools/utils.ts index 4aac5cda..eb800c4a 100644 --- a/src/pools/utils.ts +++ b/src/pools/utils.ts @@ -1,19 +1,14 @@ import { existsSync } from 'node:fs' -import { isPlainObject } from '../utils' +import { average, isPlainObject, max, median, min } from '../utils' import { + type MeasurementStatisticsRequirements, WorkerChoiceStrategies, type WorkerChoiceStrategy } from './selection-strategies/selection-strategies-types' import type { TasksQueueOptions } from './pool' +import type { IWorker, MeasurementStatistics } from './worker' export const checkFilePath = (filePath: string): void => { - if ( - filePath == null || - typeof filePath !== 'string' || - (typeof filePath === 'string' && filePath.trim().length === 0) - ) { - throw new Error('Please specify a file with a worker implementation') - } if (!existsSync(filePath)) { throw new Error(`Cannot find the worker file '${filePath}'`) } @@ -90,3 +85,71 @@ export const checkValidTasksQueueOptions = ( ) } } + +export const checkWorkerNodeArguments = ( + worker: Worker, + tasksQueueBackPressureSize: number +): void => { + if (worker == null) { + throw new TypeError('Cannot construct a worker node without a worker') + } + if (tasksQueueBackPressureSize == null) { + throw new TypeError( + 'Cannot construct a worker node without a tasks queue back pressure size' + ) + } + if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { + throw new TypeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' + ) + } + if (tasksQueueBackPressureSize <= 0) { + throw new RangeError( + 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' + ) + } +} + +/** + * Updates the given measurement statistics. + * + * @param measurementStatistics - The measurement statistics to update. + * @param measurementRequirements - The measurement statistics requirements. + * @param measurementValue - The measurement value. + * @param numberOfMeasurements - The number of measurements. + * @internal + */ +export const updateMeasurementStatistics = ( + measurementStatistics: MeasurementStatistics, + measurementRequirements: MeasurementStatisticsRequirements, + measurementValue: number +): void => { + if (measurementRequirements.aggregate) { + measurementStatistics.aggregate = + (measurementStatistics.aggregate ?? 0) + measurementValue + measurementStatistics.minimum = min( + measurementValue, + measurementStatistics.minimum ?? Infinity + ) + measurementStatistics.maximum = max( + measurementValue, + measurementStatistics.maximum ?? -Infinity + ) + if ( + (measurementRequirements.average || measurementRequirements.median) && + measurementValue != null + ) { + 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 + } + } + } +}