From: Jérôme Benoit Date: Mon, 29 Apr 2024 14:11:10 +0000 (+0200) Subject: perf: factor out strategies helper X-Git-Tag: v4.0.0~1^2~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=763abb1c8e61ceb4f69c3488839a95d056cb84f0;p=poolifier.git perf: factor out strategies helper Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index eaa03f3d..849b8a4f 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -3,12 +3,14 @@ import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js' import type { IWorker } from '../worker.js' import type { IWorkerChoiceStrategy, - MeasurementStatisticsRequirements, StrategyPolicy, TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types.js' -import { buildWorkerChoiceStrategyOptions } from './selection-strategies-utils.js' +import { + buildWorkerChoiceStrategyOptions, + toggleMedianMeasurementStatisticsRequirements +} from './selection-strategies-utils.js' /** * Worker choice strategy abstract base class. @@ -62,37 +64,23 @@ export abstract class AbstractWorkerChoiceStrategy< protected setTaskStatisticsRequirements ( opts: WorkerChoiceStrategyOptions | undefined ): void { - this.toggleMedianMeasurementStatisticsRequirements( + toggleMedianMeasurementStatisticsRequirements( this.taskStatisticsRequirements.runTime, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion opts!.runTime!.median ) - this.toggleMedianMeasurementStatisticsRequirements( + toggleMedianMeasurementStatisticsRequirements( this.taskStatisticsRequirements.waitTime, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion opts!.waitTime!.median ) - this.toggleMedianMeasurementStatisticsRequirements( + toggleMedianMeasurementStatisticsRequirements( this.taskStatisticsRequirements.elu, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion opts!.elu!.median ) } - private toggleMedianMeasurementStatisticsRequirements ( - measurementStatisticsRequirements: MeasurementStatisticsRequirements, - toggleMedian: boolean - ): void { - if (measurementStatisticsRequirements.average && toggleMedian) { - measurementStatisticsRequirements.average = false - measurementStatisticsRequirements.median = toggleMedian - } - if (measurementStatisticsRequirements.median && !toggleMedian) { - measurementStatisticsRequirements.average = true - measurementStatisticsRequirements.median = toggleMedian - } - } - protected resetWorkerNodeKeyProperties (): void { this.nextWorkerNodeKey = 0 this.previousWorkerNodeKey = 0 diff --git a/src/pools/selection-strategies/selection-strategies-utils.ts b/src/pools/selection-strategies/selection-strategies-utils.ts index 777be6e4..6e63f781 100644 --- a/src/pools/selection-strategies/selection-strategies-utils.ts +++ b/src/pools/selection-strategies/selection-strategies-utils.ts @@ -10,6 +10,7 @@ import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strate import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy.js' import { type IWorkerChoiceStrategy, + type MeasurementStatisticsRequirements, type StrategyPolicy, type TaskStatisticsRequirements, WorkerChoiceStrategies, @@ -100,6 +101,20 @@ export const buildWorkerChoiceStrategyOptions = < } } +export const toggleMedianMeasurementStatisticsRequirements = ( + measurementStatisticsRequirements: MeasurementStatisticsRequirements, + toggleMedian: boolean +): void => { + if (measurementStatisticsRequirements.average && toggleMedian) { + measurementStatisticsRequirements.average = false + measurementStatisticsRequirements.median = toggleMedian + } + if (measurementStatisticsRequirements.median && !toggleMedian) { + measurementStatisticsRequirements.average = true + measurementStatisticsRequirements.median = toggleMedian + } +} + export const buildWorkerChoiceStrategiesPolicy = ( workerChoiceStrategies: Map ): StrategyPolicy => {