Version 1.1.10
[e-mobility-charging-stations-simulator.git] / src / performance / PerformanceStatistics.ts
index bae6fa48e37a8f89bd18fdf7e617a36d76b9271d..70f9bafdbaedcc704c37eb5ae9d8447c68d489f8 100644 (file)
@@ -5,11 +5,11 @@ import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
 import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
 import Statistics, { StatisticsData } from '../types/Statistics';
 
+import { ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker';
 import Configuration from '../utils/Configuration';
 import { MessageType } from '../types/ocpp/MessageType';
 import { URL } from 'url';
 import Utils from '../utils/Utils';
-import { WorkerMessageEvents } from '../types/Worker';
 import logger from '../utils/Logger';
 import { parentPort } from 'worker_threads';
 
@@ -26,14 +26,14 @@ export default class PerformanceStatistics {
   }
 
   public static beginMeasure(id: string): string {
-    const beginId = 'begin' + id.charAt(0).toUpperCase() + id.slice(1);
-    performance.mark(beginId);
-    return beginId;
+    const markId = `${id.charAt(0).toUpperCase() + id.slice(1)}~${Utils.generateUUID()}`;
+    performance.mark(markId);
+    return markId;
   }
 
-  public static endMeasure(name: string, beginId: string): void {
-    performance.measure(name, beginId);
-    performance.clearMarks(beginId);
+  public static endMeasure(name: string, markId: string): void {
+    performance.measure(name, markId);
+    performance.clearMarks(markId);
   }
 
   public addRequestStatistic(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
@@ -98,8 +98,9 @@ export default class PerformanceStatistics {
 
   private initializePerformanceObserver(): void {
     this.performanceObserver = new PerformanceObserver((list) => {
-      this.addPerformanceEntryToStatistics(list.getEntries()[0]);
-      logger.debug(`${this.logPrefix()} '${list.getEntries()[0].name}' performance entry: %j`, list.getEntries()[0]);
+      const lastPerformanceEntry = list.getEntries()[0];
+      this.addPerformanceEntryToStatistics(lastPerformanceEntry);
+      logger.debug(`${this.logPrefix()} '${lastPerformanceEntry.name}' performance entry: %j`, lastPerformanceEntry);
     });
     this.performanceObserver.observe({ entryTypes: ['measure'] });
   }
@@ -113,7 +114,7 @@ export default class PerformanceStatistics {
       this.displayInterval = setInterval(() => {
         this.logStatistics();
       }, Configuration.getLogStatisticsInterval() * 1000);
-      logger.info(this.logPrefix() + ' logged every ' + Utils.secondsToHHMMSS(Configuration.getLogStatisticsInterval()));
+      logger.info(this.logPrefix() + ' logged every ' + Utils.formatDurationSeconds(Configuration.getLogStatisticsInterval()));
     } else {
       logger.info(this.logPrefix() + ' log interval is set to ' + Configuration.getLogStatisticsInterval().toString() + '. Not logging statistics');
     }
@@ -191,7 +192,7 @@ export default class PerformanceStatistics {
     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: WorkerMessageEvents.PERFORMANCE_STATISTICS, data: this.statistics });
+      parentPort.postMessage({ id: ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS, data: this.statistics });
     }
   }