feat: add average and median to pool info runTime and waitTime
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Jul 2023 23:06:49 +0000 (01:06 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Jul 2023 23:06:49 +0000 (01:06 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/pools/pool.ts
src/pools/selection-strategies/least-elu-worker-choice-strategy.ts

index 5b6c18747bf51e74bb5676052071ba69b8db3f9a..88f2c9e9f9ff9318f0777fb0c4e065482b59fc4b 100644 (file)
@@ -301,31 +301,83 @@ export abstract class AbstractPool<
       ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
         .runTime.aggregate && {
         runTime: {
-          minimum: Math.min(
-            ...this.workerNodes.map(
-              workerNode => workerNode.usage.runTime?.minimum ?? Infinity
+          minimum: round(
+            Math.min(
+              ...this.workerNodes.map(
+                workerNode => workerNode.usage.runTime?.minimum ?? Infinity
+              )
             )
           ),
-          maximum: Math.max(
-            ...this.workerNodes.map(
-              workerNode => workerNode.usage.runTime?.maximum ?? -Infinity
+          maximum: round(
+            Math.max(
+              ...this.workerNodes.map(
+                workerNode => workerNode.usage.runTime?.maximum ?? -Infinity
+              )
             )
-          )
+          ),
+          average: round(
+            this.workerNodes.reduce(
+              (accumulator, workerNode) =>
+                accumulator + (workerNode.usage.runTime?.aggregate ?? 0),
+              0
+            ) /
+              this.workerNodes.reduce(
+                (accumulator, workerNode) =>
+                  accumulator + (workerNode.usage.tasks?.executed ?? 0),
+                0
+              )
+          ),
+          ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+            .runTime.median && {
+            median: round(
+              median(
+                this.workerNodes.map(
+                  workerNode => workerNode.usage.runTime?.median ?? 0
+                )
+              )
+            )
+          })
         }
       }),
       ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
         .waitTime.aggregate && {
         waitTime: {
-          minimum: Math.min(
-            ...this.workerNodes.map(
-              workerNode => workerNode.usage.waitTime?.minimum ?? Infinity
+          minimum: round(
+            Math.min(
+              ...this.workerNodes.map(
+                workerNode => workerNode.usage.waitTime?.minimum ?? Infinity
+              )
             )
           ),
-          maximum: Math.max(
-            ...this.workerNodes.map(
-              workerNode => workerNode.usage.waitTime?.maximum ?? -Infinity
+          maximum: round(
+            Math.max(
+              ...this.workerNodes.map(
+                workerNode => workerNode.usage.waitTime?.maximum ?? -Infinity
+              )
             )
-          )
+          ),
+          average: round(
+            this.workerNodes.reduce(
+              (accumulator, workerNode) =>
+                accumulator + (workerNode.usage.waitTime?.aggregate ?? 0),
+              0
+            ) /
+              this.workerNodes.reduce(
+                (accumulator, workerNode) =>
+                  accumulator + (workerNode.usage.tasks?.executed ?? 0),
+                0
+              )
+          ),
+          ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
+            .waitTime.median && {
+            median: round(
+              median(
+                this.workerNodes.map(
+                  workerNode => workerNode.usage.waitTime?.median ?? 0
+                )
+              )
+            )
+          })
         }
       })
     }
@@ -602,8 +654,9 @@ export abstract class AbstractPool<
   ): void {
     const workerTaskStatistics = workerUsage.tasks
     --workerTaskStatistics.executing
-    ++workerTaskStatistics.executed
-    if (message.taskError != null) {
+    if (message.taskError == null) {
+      ++workerTaskStatistics.executed
+    } else {
       ++workerTaskStatistics.failed
     }
   }
@@ -633,8 +686,7 @@ export abstract class AbstractPool<
         workerUsage.tasks.executed !== 0
       ) {
         workerUsage.runTime.average =
-          workerUsage.runTime.aggregate /
-          (workerUsage.tasks.executed - workerUsage.tasks.failed)
+          workerUsage.runTime.aggregate / workerUsage.tasks.executed
       }
       if (
         this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
@@ -673,8 +725,7 @@ export abstract class AbstractPool<
         workerUsage.tasks.executed !== 0
       ) {
         workerUsage.waitTime.average =
-          workerUsage.waitTime.aggregate /
-          (workerUsage.tasks.executed - workerUsage.tasks.failed)
+          workerUsage.waitTime.aggregate / workerUsage.tasks.executed
       }
       if (
         this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
@@ -730,12 +781,10 @@ export abstract class AbstractPool<
             .average &&
           workerUsage.tasks.executed !== 0
         ) {
-          const executedTasks =
-            workerUsage.tasks.executed - workerUsage.tasks.failed
           workerUsage.elu.idle.average =
-            workerUsage.elu.idle.aggregate / executedTasks
+            workerUsage.elu.idle.aggregate / workerUsage.tasks.executed
           workerUsage.elu.active.average =
-            workerUsage.elu.active.aggregate / executedTasks
+            workerUsage.elu.active.aggregate / workerUsage.tasks.executed
         }
         if (
           this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu
index 15654803d64f2a3e1d6924c5120d47c4f56a2283..5be4f95a9e62ceb643c49a98faffd0f0dc29395e 100644 (file)
@@ -89,10 +89,14 @@ export interface PoolInfo {
   runTime?: {
     minimum: number
     maximum: number
+    average: number
+    median?: number
   }
   waitTime?: {
     minimum: number
     maximum: number
+    average: number
+    median?: number
   }
 }
 
index cbe00a4780792fdcf189d217b29ddcbe2787c853..54d0e7cb054de064d8f9dfbfe074df5f1ba5bbf6 100644 (file)
@@ -60,7 +60,7 @@ export class LeastEluWorkerChoiceStrategy<
     let minWorkerElu = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerUsage = workerNode.usage
-      const workerElu = workerUsage.elu?.active.aggregate ?? 0
+      const workerElu = workerUsage.elu?.active?.aggregate ?? 0
       if (workerElu === 0) {
         this.nextWorkerNodeId = workerNodeKey
         break