Fix fair share strategy unix timestamp comparison
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-choice-strategy.ts
index cdb88016b1934f69abeff8c30aff378f8fa643d7..6e48f82f2bc877bcf396e0631fb02d9f76dab9a0 100644 (file)
@@ -2,6 +2,7 @@ import { cpus } from 'os'
 import type { AbstractPoolWorker } from '../abstract-pool-worker'
 import type { IPoolInternal } from '../pool-internal'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
+import type { RequiredStatistics } from './selection-strategies-types'
 
 /**
  * Task run time.
@@ -24,6 +25,11 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   Data,
   Response
 > extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+  /** @inheritDoc */
+  public requiredStatistics: RequiredStatistics = {
+    runTime: true
+  }
+
   /**
    * Worker index where the previous task was submitted.
    */
@@ -45,7 +51,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   >()
 
   /**
-   * Constructs a worker choice strategy that selects based a weighted round robin scheduling algorithm.
+   * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
    *
    * @param pool The pool instance.
    */
@@ -101,10 +107,10 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
 
   private computeWorkerWeight () {
     let cpusCycleTimeWeight = 0
-    for (let cpu = 0; cpu < cpus().length; cpu++) {
+    for (const cpu of cpus()) {
       // CPU estimated cycle time
-      const numberOfDigit = cpus()[cpu].speed.toString().length - 1
-      const cpuCycleTime = 1 / (cpus()[cpu].speed / Math.pow(10, numberOfDigit))
+      const numberOfDigit = cpu.speed.toString().length - 1
+      const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigit))
       cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigit)
     }
     return cpusCycleTimeWeight / cpus().length