refactor: use named export in benchmarking code
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
index 0ae939949b1c115918db6bb1f33dfab2e253191a..7b3ad3f7516f2b9522aca6acf3ec6b0356aabac3 100644 (file)
@@ -2,10 +2,11 @@ import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
 import type { IPool } from '../pool'
 import type { IWorker } from '../worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
-import type {
-  IWorkerChoiceStrategy,
-  RequiredStatistics,
-  WorkerChoiceStrategyOptions
+import {
+  type IWorkerChoiceStrategy,
+  Measurements,
+  type TaskStatisticsRequirements,
+  type WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
 
 /**
@@ -24,10 +25,22 @@ export class FairShareWorkerChoiceStrategy<
   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: {
+      aggregate: true,
+      average: true,
+      median: false
+    }
   }
 
   /**
@@ -41,7 +54,7 @@ export class FairShareWorkerChoiceStrategy<
     opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     super(pool, opts)
-    this.setRequiredStatistics(this.opts)
+    this.setTaskStatisticsRequirements(this.opts)
   }
 
   /** @inheritDoc */
@@ -59,7 +72,6 @@ export class FairShareWorkerChoiceStrategy<
   /** @inheritDoc */
   public choose (): number {
     let minWorkerVirtualTaskEndTimestamp = Infinity
-    let chosenWorkerNodeKey!: number
     for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
       if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
         this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
@@ -68,10 +80,10 @@ export class FairShareWorkerChoiceStrategy<
         this.workersVirtualTaskEndTimestamp[workerNodeKey]
       if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) {
         minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
-        chosenWorkerNodeKey = workerNodeKey
+        this.nextWorkerNodeId = workerNodeKey
       }
     }
-    return chosenWorkerNodeKey
+    return this.nextWorkerNodeId
   }
 
   /** @inheritDoc */
@@ -97,10 +109,11 @@ export class FairShareWorkerChoiceStrategy<
     workerNodeKey: number,
     workerVirtualTaskStartTimestamp: number
   ): number {
-    const workerVirtualTaskRunTime = this.requiredStatistics.medRunTime
-      ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
-      : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
-    return workerVirtualTaskStartTimestamp + workerVirtualTaskRunTime
+    const workerTaskRunTime =
+      this.opts.measurement === Measurements.elu
+        ? this.getWorkerTaskElu(workerNodeKey)
+        : this.getWorkerTaskRunTime(workerNodeKey)
+    return workerVirtualTaskStartTimestamp + workerTaskRunTime
   }
 
   private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {