README.md: refine performanceStorage configuration documentation
[e-mobility-charging-stations-simulator.git] / src / utils / PerformanceStatistics.ts
index c39295affc108fb81373878e2d49ad65b7a4e1cb..4049197a0693b3e4eb74575b06f057f4add57ae2 100644 (file)
@@ -1,3 +1,5 @@
+// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+
 import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from './CircularArray';
 import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
 import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
@@ -6,7 +8,9 @@ import Statistics, { StatisticsData } from '../types/Statistics';
 import Configuration from './Configuration';
 import { MessageType } from '../types/ocpp/MessageType';
 import Utils from './Utils';
+import { WorkerEvents } from '../types/Worker';
 import logger from './Logger';
+import { parentPort } from 'worker_threads';
 
 export default class PerformanceStatistics {
   private objId: string;
@@ -17,7 +21,7 @@ export default class PerformanceStatistics {
   public constructor(objId: string) {
     this.objId = objId;
     this.initializePerformanceObserver();
-    this.statistics = { id: this.objId ?? 'Object id not specified', statisticsData: {} };
+    this.statistics = { id: this.objId ?? 'Object id not specified', createdAt: new Date(), statisticsData: {} };
   }
 
   public static beginMeasure(id: string): string {
@@ -72,7 +76,10 @@ export default class PerformanceStatistics {
   }
 
   public start(): void {
-    this.startDisplayInterval();
+    this.startLogStatisticsInterval();
+    if (Configuration.getPerformanceStorage().enabled) {
+      logger.info(`${this.logPrefix()} storage enabled: type ${Configuration.getPerformanceStorage().type}, URI: ${Configuration.getPerformanceStorage().URI}`);
+    }
   }
 
   public stop(): void {
@@ -100,14 +107,14 @@ export default class PerformanceStatistics {
     logger.info(this.logPrefix() + ' %j', this.statistics);
   }
 
-  private startDisplayInterval(): void {
-    if (Configuration.getStatisticsDisplayInterval() > 0) {
+  private startLogStatisticsInterval(): void {
+    if (Configuration.getLogStatisticsInterval() > 0) {
       this.displayInterval = setInterval(() => {
         this.logStatistics();
-      }, Configuration.getStatisticsDisplayInterval() * 1000);
-      logger.info(this.logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval()));
+      }, Configuration.getLogStatisticsInterval() * 1000);
+      logger.info(this.logPrefix() + ' logged every ' + Utils.secondsToHHMMSS(Configuration.getLogStatisticsInterval()));
     } else {
-      logger.info(this.logPrefix() + ' display interval is set to ' + Configuration.getStatisticsDisplayInterval().toString() + '. Not displaying statistics');
+      logger.info(this.logPrefix() + ' log interval is set to ' + Configuration.getLogStatisticsInterval().toString() + '. Not logging statistics');
     }
   }
 
@@ -115,7 +122,7 @@ export default class PerformanceStatistics {
     if (Array.isArray(dataSet) && dataSet.length === 1) {
       return dataSet[0];
     }
-    const sortedDataSet = dataSet.slice().sort();
+    const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
     const middleIndex = Math.floor(sortedDataSet.length / 2);
     if (sortedDataSet.length % 2) {
       return sortedDataSet[middleIndex / 2];
@@ -131,7 +138,7 @@ export default class PerformanceStatistics {
     if (Utils.isEmptyArray(dataSet)) {
       return 0;
     }
-    const sortedDataSet = dataSet.slice().sort();
+    const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
     if (percentile === 0) {
       return sortedDataSet[0];
     }
@@ -171,6 +178,7 @@ export default class PerformanceStatistics {
       this.statistics.statisticsData[entryName] = {} as StatisticsData;
     }
     // Update current statistics
+    this.statistics.lastUpdatedAt = new Date();
     this.statistics.statisticsData[entryName].countTimeMeasurement = this.statistics.statisticsData[entryName].countTimeMeasurement ? this.statistics.statisticsData[entryName].countTimeMeasurement + 1 : 1;
     this.statistics.statisticsData[entryName].currentTimeMeasurement = entry.duration;
     this.statistics.statisticsData[entryName].minTimeMeasurement = this.statistics.statisticsData[entryName].minTimeMeasurement ? (this.statistics.statisticsData[entryName].minTimeMeasurement > entry.duration ? entry.duration : this.statistics.statisticsData[entryName].minTimeMeasurement) : entry.duration;
@@ -181,6 +189,9 @@ export default class PerformanceStatistics {
     this.statistics.statisticsData[entryName].medTimeMeasurement = this.median(this.statistics.statisticsData[entryName].timeMeasurementSeries);
     this.statistics.statisticsData[entryName].ninetyFiveThPercentileTimeMeasurement = this.percentile(this.statistics.statisticsData[entryName].timeMeasurementSeries, 95);
     this.statistics.statisticsData[entryName].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[entryName].timeMeasurementSeries);
+    if (Configuration.getPerformanceStorage().enabled) {
+      parentPort.postMessage({ id: WorkerEvents.PERFORMANCE_STATISTICS, data: this.statistics });
+    }
   }
 
   private logPrefix(): string {