test: improve IWRR coverage
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index 7324c511cca18f76b89c254f1deb777ae06c2dbe..d91f3c69e3ccf8782a5d945ad696aa1eea88d4c1 100644 (file)
@@ -1,7 +1,6 @@
-import { cpus } from 'node:os'
 import {
   DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-  getDefaultInternalWorkerChoiceStrategyOptions
+  buildInternalWorkerChoiceStrategyOptions
 } from '../../utils'
 import type { IPool } from '../pool'
 import type { IWorker } from '../worker'
@@ -58,7 +57,11 @@ export abstract class AbstractWorkerChoiceStrategy<
     protected readonly pool: IPool<Worker, Data, Response>,
     protected opts: InternalWorkerChoiceStrategyOptions
   ) {
-    this.setOptions(this.opts)
+    this.opts = buildInternalWorkerChoiceStrategyOptions(
+      this.pool.info.maxSize,
+      this.opts
+    )
+    this.setTaskStatisticsRequirements(this.opts)
     this.choose = this.choose.bind(this)
   }
 
@@ -112,13 +115,10 @@ export abstract class AbstractWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
-    this.opts = {
-      ...getDefaultInternalWorkerChoiceStrategyOptions(
-        this.pool.info.maxSize +
-          Object.keys((opts?.weights as Record<number, number>) ?? {}).length
-      ),
-      ...opts
-    }
+    this.opts = buildInternalWorkerChoiceStrategyOptions(
+      this.pool.info.maxSize,
+      opts
+    )
     this.setTaskStatisticsRequirements(this.opts)
   }
 
@@ -196,15 +196,4 @@ export abstract class AbstractWorkerChoiceStrategy<
   protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
     this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey
   }
-
-  protected 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)
-  }
 }