From 0bbf65c3768b86f989056de36de0199d6e35562a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 31 May 2023 21:18:18 +0200 Subject: [PATCH] refactor: factor out common code in worker choice strategies MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../abstract-worker-choice-strategy.ts | 12 ++++++++++++ ...ed-weighted-round-robin-worker-choice-strategy.ts | 12 ------------ .../weighted-round-robin-worker-choice-strategy.ts | 12 ------------ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 2026e674..6e798fbc 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -1,3 +1,4 @@ +import { cpus } from 'node:os' import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import type { IPool } from '../pool' import type { IWorker } from '../worker' @@ -126,6 +127,17 @@ export abstract class AbstractWorkerChoiceStrategy< : this.pool.workerNodes[workerNodeKey].tasksUsage.avgWaitTime } + 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) + } + /** * Finds the first free worker node key based on the number of tasks the worker has applied. * diff --git a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts index 12a8055e..7248f03a 100644 --- a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts @@ -1,4 +1,3 @@ -import { cpus } from 'node:os' import type { IWorker } from '../worker' import type { IPool } from '../pool' import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' @@ -123,17 +122,6 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< this.roundWeights = this.getRoundWeights() } - private 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) - } - private getRoundWeights (): number[] { if (this.opts.weights == null) { return [this.defaultWorkerWeight] diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index df45feb3..17ae6647 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -1,4 +1,3 @@ -import { cpus } from 'node:os' import type { IWorker } from '../worker' import type { IPool } from '../pool' import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' @@ -101,15 +100,4 @@ export class WeightedRoundRobinWorkerChoiceStrategy< } return true } - - private 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) - } } -- 2.34.1