From f4ff1ce25e8ec840112e33e306e492b063738e6d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 2 Apr 2023 16:30:02 +0200 Subject: [PATCH] perf: take into account the number of run tasks in LRU worker choice strategy MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 1 + src/pools/abstract-pool.ts | 11 +++++++---- src/pools/pool-internal.ts | 10 +++++++++- .../less-recently-used-worker-choice-strategy.ts | 14 +++++++------- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b21ed16f..84e8ac47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Optimize worker storage in pool. - Optimize worker alive status check. +- Optimize `LESS_RECENTLY_USED` worker choice strategy. ### Fixed diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 363db0e3..b3ebc194 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -165,14 +165,17 @@ export abstract class AbstractPool< /** {@inheritDoc} */ public getWorkerRunningTasks (worker: Worker): number | undefined { - return this.workers.get(this.getWorkerKey(worker) as number)?.tasksUsage - ?.running + return this.getWorkerTasksUsage(worker)?.running + } + + /** {@inheritDoc} */ + public getWorkerRunTasks (worker: Worker): number | undefined { + return this.getWorkerTasksUsage(worker)?.run } /** {@inheritDoc} */ public getWorkerAverageTasksRunTime (worker: Worker): number | undefined { - return this.workers.get(this.getWorkerKey(worker) as number)?.tasksUsage - ?.avgRunTime + return this.getWorkerTasksUsage(worker)?.avgRunTime } /** {@inheritDoc} */ diff --git a/src/pools/pool-internal.ts b/src/pools/pool-internal.ts index 97e5d5d2..9656175f 100644 --- a/src/pools/pool-internal.ts +++ b/src/pools/pool-internal.ts @@ -42,7 +42,7 @@ export interface IPoolInternal< Response = unknown > extends IPool { /** - * Map of workers. + * Pool workers map. */ readonly workers: Map> @@ -84,6 +84,14 @@ export interface IPoolInternal< */ getWorkerRunningTasks: (worker: Worker) => number | undefined + /** + * Gets worker run tasks. + * + * @param worker - The worker. + * @returns The number of tasks run on the worker. + */ + getWorkerRunTasks: (worker: Worker) => number | undefined + /** * Gets worker average tasks runtime. * diff --git a/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts index 467cf2c6..9e582a6e 100644 --- a/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts @@ -20,19 +20,19 @@ export class LessRecentlyUsedWorkerChoiceStrategy< /** {@inheritDoc} */ public choose (): Worker { - let minNumberOfRunningTasks = Infinity + let minNumberOfTasks = Infinity // A worker is always found because it picks the one with fewer tasks let lessRecentlyUsedWorker!: Worker for (const value of this.pool.workers.values()) { const worker = value.worker - const workerRunningTasks = this.pool.getWorkerRunningTasks( - worker - ) as number - if (!this.isDynamicPool && workerRunningTasks === 0) { + const workerTasks = + (this.pool.getWorkerRunTasks(worker) as number) + + (this.pool.getWorkerRunningTasks(worker) as number) + if (!this.isDynamicPool && workerTasks === 0) { return worker - } else if (workerRunningTasks < minNumberOfRunningTasks) { + } else if (workerTasks < minNumberOfTasks) { + minNumberOfTasks = workerTasks lessRecentlyUsedWorker = worker - minNumberOfRunningTasks = workerRunningTasks } } return lessRecentlyUsedWorker -- 2.34.1