Use a circular array to store time measurements.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 20 Nov 2020 00:25:55 +0000 (01:25 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 20 Nov 2020 00:25:55 +0000 (01:25 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/types/CommandStatistics.ts
src/utils/CircularArray.ts [new file with mode: 0644]
src/utils/Constants.ts
src/utils/Statistics.ts

index 9c1896f43fa4ebffb250864bb458971a7b0c2c5f..b8740039cdfd2d193253580f2570fbbd37d8944e 100644 (file)
@@ -1,3 +1,4 @@
+import CircularArray from '../utils/CircularArray';
 import { EntryType } from 'perf_hooks';
 
 export interface PerfEntry {
@@ -12,7 +13,7 @@ export interface CommandStatisticsData {
   countResponse: number;
   countError: number;
   countTimeMeasurement: number;
-  timeMeasurementSeries: number[];
+  timeMeasurementSeries: CircularArray<number>;
   currentTimeMeasurement: number;
   minTimeMeasurement: number;
   maxTimeMeasurement: number;
diff --git a/src/utils/CircularArray.ts b/src/utils/CircularArray.ts
new file mode 100644 (file)
index 0000000..5dab1e2
--- /dev/null
@@ -0,0 +1,24 @@
+import Constants from './Constants';
+
+export default class CircularArray<T> extends Array<T> {
+  public size: number;
+
+  constructor(size: number = Constants.MAXIMUM_MEASUREMENTS_NUMBER) {
+    super();
+    this.size = size;
+  }
+
+  push(...items: T[]): number {
+    while (this.length > this.size) {
+      this.shift();
+    }
+    return super.push(...items);
+  }
+
+  unshift(...items: T[]): number {
+    while (this.length > this.size) {
+      this.pop();
+    }
+    return super.unshift(...items);
+  }
+}
index eab54ea5fe962be57997184ff6997d96d96780c5..cf5d9bfaa38d303f2f512e5c348be6bd30dab6f0 100644 (file)
@@ -50,5 +50,7 @@ export default class Constants {
   static readonly CHARGING_STATION_DEFAULT_RESET_TIME = 60000; // Ms
   static readonly CHARGING_STATION_ATG_WAIT_TIME = 2000; // Ms
 
+  static readonly MAXIMUM_MEASUREMENTS_NUMBER = 5000;
+
   static readonly TRANSACTION_DEFAULT_IDTAG = '00000000';
 }
index 3fa4c78321ddafe219ba750aa11b78c971f83b63..d687368c008b8043d80ec9638b13d00b0d7365d9 100644 (file)
@@ -1,5 +1,6 @@
 import CommandStatistics, { CommandStatisticsData, PerfEntry } from '../types/CommandStatistics';
 
+import CircularArray from './CircularArray';
 import Configuration from './Configuration';
 import Constants from './Constants';
 import { PerformanceEntry } from 'perf_hooks';
@@ -126,7 +127,7 @@ export default class Statistics {
     this._commandsStatistics[command].maxTimeMeasurement = this._commandsStatistics[command].maxTimeMeasurement ? (this._commandsStatistics[command].maxTimeMeasurement < duration ? duration : this._commandsStatistics[command].maxTimeMeasurement) : duration;
     this._commandsStatistics[command].totalTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement ? this._commandsStatistics[command].totalTimeMeasurement + duration : duration;
     this._commandsStatistics[command].avgTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement / this._commandsStatistics[command].countTimeMeasurement;
-    Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration];
+    Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration] as CircularArray<number>;
     this._commandsStatistics[command].medTimeMeasurement = this.median(this._commandsStatistics[command].timeMeasurementSeries);
   }