refactor: align worker choice strategy options namespace
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
index 5d8bbe2f1e4466bdfa12b0167d79ee53fc94e065..4fcbeeda43d6021cc0d71dfb4daa66b8a046f0de 100644 (file)
@@ -1,11 +1,10 @@
-import { cpus } from 'node:os'
 import type { IWorker } from '../worker'
 import type { IPool } from '../pool'
 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
 import type {
   IWorkerChoiceStrategy,
-  RequiredStatistics,
+  TaskStatisticsRequirements,
   WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
 
@@ -25,10 +24,18 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
-  public readonly requiredStatistics: RequiredStatistics = {
-    runTime: true,
-    avgRunTime: true,
-    medRunTime: false
+  public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
+    runTime: {
+      aggregate: true,
+      average: true,
+      median: false
+    },
+    waitTime: {
+      aggregate: false,
+      average: false,
+      median: false
+    },
+    elu: false
   }
 
   /**
@@ -50,7 +57,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     super(pool, opts)
-    this.checkOptions(this.opts)
+    this.setTaskStatisticsRequirements(this.opts)
     this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
   }
 
@@ -75,7 +82,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     if (workerVirtualTaskRunTime < workerWeight) {
       this.workerVirtualTaskRunTime =
         workerVirtualTaskRunTime +
-        this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey)
+        this.getWorkerTaskRunTime(chosenWorkerNodeKey)
     } else {
       this.currentWorkerNodeId =
         this.currentWorkerNodeId === this.pool.workerNodes.length - 1
@@ -98,21 +105,4 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     }
     return true
   }
-
-  private getWorkerVirtualTaskRunTime (workerNodeKey: number): number {
-    return this.requiredStatistics.medRunTime
-      ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
-      : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
-  }
-
-  private computeDefaultWorkerWeight (): number {
-    let cpusCycleTimeWeight = 0
-    for (const cpu of cpus()) {
-      // CPU estimated cycle time
-      const numberOfDigits = cpu.speed.toString().length - 1
-      const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
-      cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
-    }
-    return Math.round(cpusCycleTimeWeight / cpus().length)
-  }
 }