Make the performance measurement time series actually a real time series
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 22 Nov 2021 17:00:17 +0000 (18:00 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 22 Nov 2021 17:00:17 +0000 (18:00 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/performance/PerformanceStatistics.ts
src/types/Statistics.ts

index 9f3b94822033e98e956f72561b59ad51ea4aa04c..f9be1922f3ef87e98c1af83a39565933b2a5d698 100644 (file)
@@ -3,7 +3,7 @@
 import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from '../utils/CircularArray';
 import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
 import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
-import Statistics, { StatisticsData } from '../types/Statistics';
+import Statistics, { StatisticsData, TimeSeries } from '../types/Statistics';
 
 import { ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker';
 import Configuration from '../utils/Configuration';
@@ -176,15 +176,21 @@ export default class PerformanceStatistics {
     this.statistics.statisticsData.get(entryName).maxTimeMeasurement = this.statistics.statisticsData.get(entryName)?.maxTimeMeasurement ? (this.statistics.statisticsData.get(entryName).maxTimeMeasurement < entry.duration ? entry.duration : this.statistics.statisticsData.get(entryName).maxTimeMeasurement) : entry.duration;
     this.statistics.statisticsData.get(entryName).totalTimeMeasurement = this.statistics.statisticsData.get(entryName)?.totalTimeMeasurement ? this.statistics.statisticsData.get(entryName).totalTimeMeasurement + entry.duration : entry.duration;
     this.statistics.statisticsData.get(entryName).avgTimeMeasurement = this.statistics.statisticsData.get(entryName).totalTimeMeasurement / this.statistics.statisticsData.get(entryName).countTimeMeasurement;
-    Array.isArray(this.statistics.statisticsData.get(entryName).timeMeasurementSeries) ? this.statistics.statisticsData.get(entryName).timeMeasurementSeries.push(entry.duration) : this.statistics.statisticsData.get(entryName).timeMeasurementSeries = new CircularArray<number>(DEFAULT_CIRCULAR_ARRAY_SIZE, entry.duration);
-    this.statistics.statisticsData.get(entryName).medTimeMeasurement = this.median(this.statistics.statisticsData.get(entryName).timeMeasurementSeries);
-    this.statistics.statisticsData.get(entryName).ninetyFiveThPercentileTimeMeasurement = this.percentile(this.statistics.statisticsData.get(entryName).timeMeasurementSeries, 95);
-    this.statistics.statisticsData.get(entryName).stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData.get(entryName).timeMeasurementSeries);
+    Array.isArray(this.statistics.statisticsData.get(entryName).timeMeasurementSeries)
+      ? this.statistics.statisticsData.get(entryName).timeMeasurementSeries.push({ timestamp: entry.startTime, value: entry.duration })
+      : this.statistics.statisticsData.get(entryName).timeMeasurementSeries = new CircularArray<TimeSeries>(DEFAULT_CIRCULAR_ARRAY_SIZE, { timestamp: entry.startTime, value: entry.duration });
+    this.statistics.statisticsData.get(entryName).medTimeMeasurement = this.median(this.extractTimeSeriesValues(this.statistics.statisticsData.get(entryName).timeMeasurementSeries));
+    this.statistics.statisticsData.get(entryName).ninetyFiveThPercentileTimeMeasurement = this.percentile(this.extractTimeSeriesValues(this.statistics.statisticsData.get(entryName).timeMeasurementSeries), 95);
+    this.statistics.statisticsData.get(entryName).stdDevTimeMeasurement = this.stdDeviation(this.extractTimeSeriesValues(this.statistics.statisticsData.get(entryName).timeMeasurementSeries));
     if (Configuration.getPerformanceStorage().enabled) {
       parentPort.postMessage({ id: ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS, data: this.statistics });
     }
   }
 
+  private extractTimeSeriesValues(timeSeries: CircularArray<TimeSeries>): number[] {
+    return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
+  }
+
   private logPrefix(): string {
     return Utils.logPrefix(` ${this.objId} | Performance statistics`);
   }
index 00c73052f98d132de8bfc2dbb3a2bc81c444f00e..5a6db6d5b78dedfdd18a2115220b591b3ac62a28 100644 (file)
@@ -1,11 +1,16 @@
 import { CircularArray } from '../utils/CircularArray';
 
+export interface TimeSeries {
+  timestamp: number;
+  value: number;
+}
+
 export interface StatisticsData {
   countRequest: number;
   countResponse: number;
   countError: number;
   countTimeMeasurement: number;
-  timeMeasurementSeries: CircularArray<number>;
+  timeMeasurementSeries: CircularArray<TimeSeries>;
   currentTimeMeasurement: number;
   minTimeMeasurement: number;
   maxTimeMeasurement: number;