test: cleanup helpers
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index 18b47a1d9d38bcbb1ac4361dd4938d0106d47fe7..476c58bc8bcf45f69450adb196e8886abe6f3b88 100644 (file)
@@ -4,6 +4,7 @@ import type { IPool } from '../pool'
 import type { IWorker } from '../worker'
 import type {
   IWorkerChoiceStrategy,
+  StrategyPolicy,
   TaskStatisticsRequirements,
   WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
@@ -20,10 +21,21 @@ export abstract class AbstractWorkerChoiceStrategy<
   Data = unknown,
   Response = unknown
 > implements IWorkerChoiceStrategy {
+  // /**
+  //  * Toggles finding the last free worker node key.
+  //  */
+  // private toggleFindLastFreeWorkerNodeKey: boolean = false
+
   /**
-   * Toggles finding the last free worker node key.
+   * Id of the next worker node.
    */
-  private toggleFindLastFreeWorkerNodeKey: boolean = false
+  protected nextWorkerNodeId: number = 0
+
+  /** @inheritDoc */
+  public readonly strategyPolicy: StrategyPolicy = {
+    useDynamicWorker: false
+  }
+
   /** @inheritDoc */
   public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
     runTime: {
@@ -36,7 +48,11 @@ export abstract class AbstractWorkerChoiceStrategy<
       average: false,
       median: false
     },
-    elu: false
+    elu: {
+      aggregate: false,
+      average: false,
+      median: false
+    }
   }
 
   /**
@@ -87,6 +103,20 @@ export abstract class AbstractWorkerChoiceStrategy<
       this.taskStatisticsRequirements.waitTime.median = opts.waitTime
         .median as boolean
     }
+    if (
+      this.taskStatisticsRequirements.elu.average &&
+      opts.elu?.median === true
+    ) {
+      this.taskStatisticsRequirements.elu.average = false
+      this.taskStatisticsRequirements.elu.median = opts.elu.median as boolean
+    }
+    if (
+      this.taskStatisticsRequirements.elu.median &&
+      opts.elu?.median === false
+    ) {
+      this.taskStatisticsRequirements.elu.average = true
+      this.taskStatisticsRequirements.elu.median = opts.elu.median as boolean
+    }
   }
 
   /** @inheritDoc */
@@ -108,19 +138,19 @@ export abstract class AbstractWorkerChoiceStrategy<
     this.opts = opts
   }
 
-  /**
-   * Finds a free worker node key.
-   *
-   * @returns The free worker node key or `-1` if there is no free worker node.
-   */
-  protected findFreeWorkerNodeKey (): number {
-    if (this.toggleFindLastFreeWorkerNodeKey) {
-      this.toggleFindLastFreeWorkerNodeKey = false
-      return this.findLastFreeWorkerNodeKey()
-    }
-    this.toggleFindLastFreeWorkerNodeKey = true
-    return this.findFirstFreeWorkerNodeKey()
-  }
+  // /**
+  //  * Finds a free worker node key.
+  //  *
+  //  * @returns The free worker node key or `-1` if there is no free worker node.
+  //  */
+  // protected findFreeWorkerNodeKey (): number {
+  //   if (this.toggleFindLastFreeWorkerNodeKey) {
+  //     this.toggleFindLastFreeWorkerNodeKey = false
+  //     return this.findLastFreeWorkerNodeKey()
+  //   }
+  //   this.toggleFindLastFreeWorkerNodeKey = true
+  //   return this.findFirstFreeWorkerNodeKey()
+  // }
 
   /**
    * Gets the worker task runtime.
@@ -144,10 +174,24 @@ export abstract class AbstractWorkerChoiceStrategy<
    * @param workerNodeKey - The worker node key.
    * @returns The worker task wait time.
    */
-  protected getWorkerWaitTime (workerNodeKey: number): number {
+  protected getWorkerTaskWaitTime (workerNodeKey: number): number {
     return this.taskStatisticsRequirements.waitTime.median
-      ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median
-      : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average
+      ? this.pool.workerNodes[workerNodeKey].workerUsage.waitTime.median
+      : this.pool.workerNodes[workerNodeKey].workerUsage.waitTime.average
+  }
+
+  /**
+   * Gets the worker task ELU.
+   * If the task statistics require the average ELU, the average ELU is returned.
+   * If the task statistics require the median ELU, the median ELU is returned.
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns The worker task ELU.
+   */
+  protected getWorkerTaskElu (workerNodeKey: number): number {
+    return this.taskStatisticsRequirements.elu.median
+      ? this.pool.workerNodes[workerNodeKey].workerUsage.elu.active.median
+      : this.pool.workerNodes[workerNodeKey].workerUsage.elu.active.average
   }
 
   protected computeDefaultWorkerWeight (): number {
@@ -161,46 +205,46 @@ export abstract class AbstractWorkerChoiceStrategy<
     return Math.round(cpusCycleTimeWeight / cpus().length)
   }
 
-  /**
-   * Finds the first free worker node key based on the number of tasks the worker has applied.
-   *
-   * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
-   *
-   * If no free worker is found, `-1` is returned.
-   *
-   * @returns A worker node key if there is one, `-1` otherwise.
-   */
-  private findFirstFreeWorkerNodeKey (): number {
-    return this.pool.workerNodes.findIndex(workerNode => {
-      return workerNode.workerUsage.tasks.executing === 0
-    })
-  }
+  // /**
+  //  * Finds the first free worker node key based on the number of tasks the worker has applied.
+  //  *
+  //  * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
+  //  *
+  //  * If no free worker is found, `-1` is returned.
+  //  *
+  //  * @returns A worker node key if there is one, `-1` otherwise.
+  //  */
+  // private findFirstFreeWorkerNodeKey (): number {
+  //   return this.pool.workerNodes.findIndex(workerNode => {
+  //     return workerNode.workerUsage.tasks.executing === 0
+  //   })
+  // }
 
-  /**
-   * Finds the last free worker node key based on the number of tasks the worker has applied.
-   *
-   * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
-   *
-   * If no free worker is found, `-1` is returned.
-   *
-   * @returns A worker node key if there is one, `-1` otherwise.
-   */
-  private findLastFreeWorkerNodeKey (): number {
-    // It requires node >= 18.0.0:
-    // return this.workerNodes.findLastIndex(workerNode => {
-    //   return workerNode.workerUsage.tasks.executing === 0
-    // })
-    for (
-      let workerNodeKey = this.pool.workerNodes.length - 1;
-      workerNodeKey >= 0;
-      workerNodeKey--
-    ) {
-      if (
-        this.pool.workerNodes[workerNodeKey].workerUsage.tasks.executing === 0
-      ) {
-        return workerNodeKey
-      }
-    }
-    return -1
-  }
+  // /**
+  //  * Finds the last free worker node key based on the number of tasks the worker has applied.
+  //  *
+  //  * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
+  //  *
+  //  * If no free worker is found, `-1` is returned.
+  //  *
+  //  * @returns A worker node key if there is one, `-1` otherwise.
+  //  */
+  // private findLastFreeWorkerNodeKey (): number {
+  //   // It requires node >= 18.0.0:
+  //   // return this.workerNodes.findLastIndex(workerNode => {
+  //   //   return workerNode.workerUsage.tasks.executing === 0
+  //   // })
+  //   for (
+  //     let workerNodeKey = this.pool.workerNodes.length - 1;
+  //     workerNodeKey >= 0;
+  //     workerNodeKey--
+  //   ) {
+  //     if (
+  //       this.pool.workerNodes[workerNodeKey].workerUsage.tasks.executing === 0
+  //     ) {
+  //       return workerNodeKey
+  //     }
+  //   }
+  //   return -1
+  // }
 }