feat: account tasks wait time more accurately
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 29 May 2023 20:49:20 +0000 (22:49 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 29 May 2023 20:49:20 +0000 (22:49 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/utility-types.ts
src/worker/abstract-worker.ts

index e396744603eed1011518cbb3f62b59bb3cedd305..81012fa0b4f736559fca73eb395bf7c622bce48a 100644 (file)
@@ -419,18 +419,7 @@ export abstract class AbstractPool<
     workerNodeKey: number,
     task: Task<Data>
   ): void {
-    const workerTasksUsage = this.workerNodes[workerNodeKey].tasksUsage
-    ++workerTasksUsage.running
-    if (this.workerChoiceStrategyContext.getRequiredStatistics().waitTime) {
-      const waitTime = performance.now() - (task.submissionTimestamp ?? 0)
-      workerTasksUsage.waitTime += waitTime
-      if (
-        this.workerChoiceStrategyContext.getRequiredStatistics().medWaitTime
-      ) {
-        workerTasksUsage.waitTimeHistory.push(waitTime)
-        workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory)
-      }
-    }
+    ++this.workerNodes[workerNodeKey].tasksUsage.running
   }
 
   /**
@@ -468,13 +457,22 @@ export abstract class AbstractPool<
         workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory)
       }
     }
-    if (
-      this.workerChoiceStrategyContext.getRequiredStatistics().waitTime &&
-      this.workerChoiceStrategyContext.getRequiredStatistics().avgWaitTime &&
-      workerTasksUsage.run !== 0
-    ) {
-      workerTasksUsage.avgWaitTime =
-        workerTasksUsage.waitTime / workerTasksUsage.run
+    if (this.workerChoiceStrategyContext.getRequiredStatistics().waitTime) {
+      workerTasksUsage.waitTime += message.waitTime ?? 0
+      if (
+        this.workerChoiceStrategyContext.getRequiredStatistics().avgWaitTime &&
+        workerTasksUsage.run !== 0
+      ) {
+        workerTasksUsage.avgWaitTime =
+          workerTasksUsage.waitTime / workerTasksUsage.run
+      }
+      if (
+        this.workerChoiceStrategyContext.getRequiredStatistics().medWaitTime &&
+        message.waitTime != null
+      ) {
+        workerTasksUsage.waitTimeHistory.push(message.waitTime)
+        workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory)
+      }
     }
   }
 
index 08861ebd30045ae430de21eae055f9634a007341..cc04f2cb99113b9cb66305c102ae5c8fe88563ad 100644 (file)
@@ -33,6 +33,10 @@ export interface MessageValue<
    * Runtime.
    */
   readonly runTime?: number
+  /**
+   * Wait time.
+   */
+  readonly waitTime?: number
   /**
    * Reference to main worker.
    */
index 5d0c9492204e0361b8007ef4dccfb3c0154bebca..176a5893caeeec7cb9d82095840b97498b3822b0 100644 (file)
@@ -207,12 +207,14 @@ export abstract class AbstractWorker<
   ): void {
     try {
       const startTimestamp = performance.now()
+      const waitTime = startTimestamp - (message.submissionTimestamp ?? 0)
       const res = fn(message.data)
       const runTime = performance.now() - startTimestamp
       this.sendToMainWorker({
         data: res,
         id: message.id,
-        runTime
+        runTime,
+        waitTime
       })
     } catch (e) {
       const err = this.handleError(e as Error)
@@ -233,13 +235,15 @@ export abstract class AbstractWorker<
     message: MessageValue<Data>
   ): void {
     const startTimestamp = performance.now()
+    const waitTime = startTimestamp - (message.submissionTimestamp ?? 0)
     fn(message.data)
       .then(res => {
         const runTime = performance.now() - startTimestamp
         this.sendToMainWorker({
           data: res,
           id: message.id,
-          runTime
+          runTime,
+          waitTime
         })
         return null
       })