perf: take into account the number of run tasks in LRU worker choice
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Apr 2023 14:30:02 +0000 (16:30 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Apr 2023 14:30:02 +0000 (16:30 +0200)
strategy

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/abstract-pool.ts
src/pools/pool-internal.ts
src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts

index b21ed16fd7ff9b08229f84a1f8bcca0605446b2c..84e8ac470ae86b65845b0b18c9aefd640c0f0fd1 100644 (file)
@@ -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
 
index 363db0e3f9b64ff650a0c351de05135c8de1de96..b3ebc19483d95e7c5f9863870e85823f976de157 100644 (file)
@@ -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} */
index 97e5d5d2d56ae75435b19d1ccb8afae10ff2e8ed..9656175fde51952c0a62da5fa6925db36ad40f7c 100644 (file)
@@ -42,7 +42,7 @@ export interface IPoolInternal<
   Response = unknown
 > extends IPool<Data, Response> {
   /**
-   * Map of workers.
+   * Pool workers map.
    */
   readonly workers: Map<number, WorkerType<Worker>>
 
@@ -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.
    *
index 467cf2c6e39ff102360e6df25617134fa7129885..9e582a6e2b89aa63813124148003272b26d46a92 100644 (file)
@@ -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