refactor: cleanup measurement statistics computation code
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 10:05:41 +0000 (12:05 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 10:05:41 +0000 (12:05 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/utils.ts

index 6467f4eb120bad8655daa076e7f910ca3da1f42a..5481099f202c19f5ef57833d4f454a18757dce3c 100644 (file)
@@ -36,6 +36,7 @@ import type {
   WorkerUsage
 } from './worker'
 import {
+  type MeasurementStatisticsRequirements,
   Measurements,
   WorkerChoiceStrategies,
   type WorkerChoiceStrategy,
@@ -774,22 +775,21 @@ export abstract class AbstractPool<
     workerUsage: WorkerUsage,
     message: MessageValue<Response>
   ): void {
+    const eluTaskStatisticsRequirements: MeasurementStatisticsRequirements =
+      this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu
     updateMeasurementStatistics(
       workerUsage.elu.active,
-      this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu,
+      eluTaskStatisticsRequirements,
       message.taskPerformance?.elu?.active ?? 0,
       workerUsage.tasks.executed
     )
     updateMeasurementStatistics(
       workerUsage.elu.idle,
-      this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu,
+      eluTaskStatisticsRequirements,
       message.taskPerformance?.elu?.idle ?? 0,
       workerUsage.tasks.executed
     )
-    if (
-      this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu
-        .aggregate
-    ) {
+    if (eluTaskStatisticsRequirements.aggregate) {
       if (message.taskPerformance?.elu != null) {
         if (workerUsage.elu.utilization != null) {
           workerUsage.elu.utilization =
index cc71491a2fbe9396a9f0411ad32a9438cf6f7d35..45e4fd2393d0533a5256754eec9075666a7f7ea1 100644 (file)
@@ -136,13 +136,13 @@ export const isAsyncFunction = (
  * @param measurementStatistics - The measurement statistics to update.
  * @param measurementRequirements - The measurement statistics requirements.
  * @param measurementValue - The measurement value.
- * @param tasksExecuted - The number of tasks executed.
+ * @param numberOfMeasurements - The number of measurements.
  */
 export const updateMeasurementStatistics = (
   measurementStatistics: MeasurementStatistics,
   measurementRequirements: MeasurementStatisticsRequirements,
   measurementValue: number,
-  tasksExecuted: number
+  numberOfMeasurements: number
 ): void => {
   if (measurementRequirements.aggregate) {
     measurementStatistics.aggregate =
@@ -155,9 +155,9 @@ export const updateMeasurementStatistics = (
       measurementValue,
       measurementStatistics.maximum ?? -Infinity
     )
-    if (measurementRequirements.average && tasksExecuted !== 0) {
+    if (measurementRequirements.average && numberOfMeasurements !== 0) {
       measurementStatistics.average =
-        measurementStatistics.aggregate / tasksExecuted
+        measurementStatistics.aggregate / numberOfMeasurements
     }
     if (measurementRequirements.median && measurementValue != null) {
       measurementStatistics.history.push(measurementValue)