perf: factor out strategies helper
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 29 Apr 2024 14:11:10 +0000 (16:11 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 29 Apr 2024 14:11:10 +0000 (16:11 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/selection-strategies-utils.ts

index eaa03f3d51b44e749798f55ec54315845322c54f..849b8a4f96feed20bcdb6bf22ba7330c8964517f 100644 (file)
@@ -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
index 777be6e4d4a2be104afd7986bd835e7e04312e96..6e63f781dd0da7b1d0185dda8aa2b2243c451dc7 100644 (file)
@@ -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<WorkerChoiceStrategy, IWorkerChoiceStrategy>
 ): StrategyPolicy => {