From: Jérôme Benoit Date: Sat, 13 May 2023 13:40:46 +0000 (+0200) Subject: refactor: factor out worker runtime getter X-Git-Tag: v2.5.0~40 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f6b641d651fdd5114bcd63bf1c991955d5b7d3eb;p=poolifier.git refactor: factor out worker runtime getter Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index c3fb28c4..a4455d28 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -87,6 +87,20 @@ export abstract class AbstractWorkerChoiceStrategy< return this.findFirstFreeWorkerNodeKey() } + /** + * Gets the worker task run time. + * If the required statistics are `avgRunTime`, the average run time is returned. + * If the required statistics are `medRunTime`, the median run time is returned. + * + * @param workerNodeKey - The worker node key. + * @returns The worker task run time. + */ + protected getWorkerTaskRunTime (workerNodeKey: number): number { + return this.requiredStatistics.medRunTime + ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime + : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime + } + /** * Finds the first free worker node key based on the number of tasks the worker has applied. * diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index 0ae93994..e26f6b8f 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -97,10 +97,9 @@ export class FairShareWorkerChoiceStrategy< workerNodeKey: number, workerVirtualTaskStartTimestamp: number ): number { - const workerVirtualTaskRunTime = this.requiredStatistics.medRunTime - ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime - : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime - return workerVirtualTaskStartTimestamp + workerVirtualTaskRunTime + return ( + workerVirtualTaskStartTimestamp + this.getWorkerTaskRunTime(workerNodeKey) + ) } private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number { 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 bda206a7..a0553fcb 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 @@ -75,7 +75,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< if (workerVirtualTaskRunTime < workerWeight) { this.workerVirtualTaskRunTime = workerVirtualTaskRunTime + - this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey) + this.getWorkerTaskRunTime(chosenWorkerNodeKey) } else { this.currentWorkerNodeId = this.currentWorkerNodeId === this.pool.workerNodes.length - 1 @@ -99,12 +99,6 @@ export class WeightedRoundRobinWorkerChoiceStrategy< return true } - private getWorkerVirtualTaskRunTime (workerNodeKey: number): number { - return this.requiredStatistics.medRunTime - ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime - : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime - } - private computeDefaultWorkerWeight (): number { let cpusCycleTimeWeight = 0 for (const cpu of cpus()) {