Fix fair share strategy unix timestamp comparison
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-choice-strategy.ts
index 0adc4ed430232604e43a29aed4ef7dd5657cf2d3..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.
    */
@@ -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