Flag some attributes readonly
[e-mobility-charging-stations-simulator.git] / src / performance / PerformanceStatistics.ts
1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
2
3 import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from '../utils/CircularArray';
4 import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
5 import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
6 import Statistics, { StatisticsData } from '../types/Statistics';
7
8 import { ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker';
9 import Configuration from '../utils/Configuration';
10 import { MessageType } from '../types/ocpp/MessageType';
11 import { URL } from 'url';
12 import Utils from '../utils/Utils';
13 import logger from '../utils/Logger';
14 import { parentPort } from 'worker_threads';
15
16 export default class PerformanceStatistics {
17 private readonly objId: string;
18 private performanceObserver: PerformanceObserver;
19 private readonly statistics: Statistics;
20 private displayInterval: NodeJS.Timeout;
21
22 public constructor(objId: string, URI: URL) {
23 this.objId = objId;
24 this.initializePerformanceObserver();
25 this.statistics = { id: this.objId ?? 'Object id not specified', URI: URI.toString(), createdAt: new Date(), statisticsData: {} };
26 }
27
28 public static beginMeasure(id: string): string {
29 const markId = `${id.charAt(0).toUpperCase() + id.slice(1)}~${Utils.generateUUID()}`;
30 performance.mark(markId);
31 return markId;
32 }
33
34 public static endMeasure(name: string, markId: string): void {
35 performance.measure(name, markId);
36 performance.clearMarks(markId);
37 }
38
39 public addRequestStatistic(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
40 switch (messageType) {
41 case MessageType.CALL_MESSAGE:
42 if (this.statistics.statisticsData[command] && this.statistics.statisticsData[command].countRequest) {
43 this.statistics.statisticsData[command].countRequest++;
44 } else {
45 this.statistics.statisticsData[command] = {} as StatisticsData;
46 this.statistics.statisticsData[command].countRequest = 1;
47 }
48 break;
49 case MessageType.CALL_RESULT_MESSAGE:
50 if (this.statistics.statisticsData[command]) {
51 if (this.statistics.statisticsData[command].countResponse) {
52 this.statistics.statisticsData[command].countResponse++;
53 } else {
54 this.statistics.statisticsData[command].countResponse = 1;
55 }
56 } else {
57 this.statistics.statisticsData[command] = {} as StatisticsData;
58 this.statistics.statisticsData[command].countResponse = 1;
59 }
60 break;
61 case MessageType.CALL_ERROR_MESSAGE:
62 if (this.statistics.statisticsData[command]) {
63 if (this.statistics.statisticsData[command].countError) {
64 this.statistics.statisticsData[command].countError++;
65 } else {
66 this.statistics.statisticsData[command].countError = 1;
67 }
68 } else {
69 this.statistics.statisticsData[command] = {} as StatisticsData;
70 this.statistics.statisticsData[command].countError = 1;
71 }
72 break;
73 default:
74 logger.error(`${this.logPrefix()} wrong message type ${messageType}`);
75 break;
76 }
77 }
78
79 public start(): void {
80 this.startLogStatisticsInterval();
81 if (Configuration.getPerformanceStorage().enabled) {
82 logger.info(`${this.logPrefix()} storage enabled: type ${Configuration.getPerformanceStorage().type}, URI: ${Configuration.getPerformanceStorage().URI}`);
83 }
84 }
85
86 public stop(): void {
87 if (this.displayInterval) {
88 clearInterval(this.displayInterval);
89 }
90 performance.clearMarks();
91 this.performanceObserver?.disconnect();
92 }
93
94 public restart(): void {
95 this.stop();
96 this.start();
97 }
98
99 private initializePerformanceObserver(): void {
100 this.performanceObserver = new PerformanceObserver((list) => {
101 const lastPerformanceEntry = list.getEntries()[0];
102 this.addPerformanceEntryToStatistics(lastPerformanceEntry);
103 logger.debug(`${this.logPrefix()} '${lastPerformanceEntry.name}' performance entry: %j`, lastPerformanceEntry);
104 });
105 this.performanceObserver.observe({ entryTypes: ['measure'] });
106 }
107
108 private logStatistics(): void {
109 logger.info(this.logPrefix() + ' %j', this.statistics);
110 }
111
112 private startLogStatisticsInterval(): void {
113 if (Configuration.getLogStatisticsInterval() > 0) {
114 this.displayInterval = setInterval(() => {
115 this.logStatistics();
116 }, Configuration.getLogStatisticsInterval() * 1000);
117 logger.info(this.logPrefix() + ' logged every ' + Utils.formatDurationSeconds(Configuration.getLogStatisticsInterval()));
118 } else {
119 logger.info(this.logPrefix() + ' log interval is set to ' + Configuration.getLogStatisticsInterval().toString() + '. Not logging statistics');
120 }
121 }
122
123 private median(dataSet: number[]): number {
124 if (Array.isArray(dataSet) && dataSet.length === 1) {
125 return dataSet[0];
126 }
127 const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
128 const middleIndex = Math.floor(sortedDataSet.length / 2);
129 if (sortedDataSet.length % 2) {
130 return sortedDataSet[middleIndex / 2];
131 }
132 return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2;
133 }
134
135 // TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree
136 private percentile(dataSet: number[], percentile: number): number {
137 if (percentile < 0 && percentile > 100) {
138 throw new RangeError('Percentile is not between 0 and 100');
139 }
140 if (Utils.isEmptyArray(dataSet)) {
141 return 0;
142 }
143 const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
144 if (percentile === 0) {
145 return sortedDataSet[0];
146 }
147 if (percentile === 100) {
148 return sortedDataSet[sortedDataSet.length - 1];
149 }
150 const percentileIndex = ((percentile / 100) * sortedDataSet.length) - 1;
151 if (Number.isInteger(percentileIndex)) {
152 return (sortedDataSet[percentileIndex] + sortedDataSet[percentileIndex + 1]) / 2;
153 }
154 return sortedDataSet[Math.round(percentileIndex)];
155 }
156
157 private stdDeviation(dataSet: number[]): number {
158 let totalDataSet = 0;
159 for (const data of dataSet) {
160 totalDataSet += data;
161 }
162 const dataSetMean = totalDataSet / dataSet.length;
163 let totalGeometricDeviation = 0;
164 for (const data of dataSet) {
165 const deviation = data - dataSetMean;
166 totalGeometricDeviation += deviation * deviation;
167 }
168 return Math.sqrt(totalGeometricDeviation / dataSet.length);
169 }
170
171 private addPerformanceEntryToStatistics(entry: PerformanceEntry): void {
172 let entryName = entry.name;
173 // Rename entry name
174 const MAP_NAME: Record<string, string> = {};
175 if (MAP_NAME[entryName]) {
176 entryName = MAP_NAME[entryName];
177 }
178 // Initialize command statistics
179 if (!this.statistics.statisticsData[entryName]) {
180 this.statistics.statisticsData[entryName] = {} as StatisticsData;
181 }
182 // Update current statistics
183 this.statistics.updatedAt = new Date();
184 this.statistics.statisticsData[entryName].countTimeMeasurement = this.statistics.statisticsData[entryName].countTimeMeasurement ? this.statistics.statisticsData[entryName].countTimeMeasurement + 1 : 1;
185 this.statistics.statisticsData[entryName].currentTimeMeasurement = entry.duration;
186 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;
187 this.statistics.statisticsData[entryName].maxTimeMeasurement = this.statistics.statisticsData[entryName].maxTimeMeasurement ? (this.statistics.statisticsData[entryName].maxTimeMeasurement < entry.duration ? entry.duration : this.statistics.statisticsData[entryName].maxTimeMeasurement) : entry.duration;
188 this.statistics.statisticsData[entryName].totalTimeMeasurement = this.statistics.statisticsData[entryName].totalTimeMeasurement ? this.statistics.statisticsData[entryName].totalTimeMeasurement + entry.duration : entry.duration;
189 this.statistics.statisticsData[entryName].avgTimeMeasurement = this.statistics.statisticsData[entryName].totalTimeMeasurement / this.statistics.statisticsData[entryName].countTimeMeasurement;
190 Array.isArray(this.statistics.statisticsData[entryName].timeMeasurementSeries) ? this.statistics.statisticsData[entryName].timeMeasurementSeries.push(entry.duration) : this.statistics.statisticsData[entryName].timeMeasurementSeries = new CircularArray<number>(DEFAULT_CIRCULAR_ARRAY_SIZE, entry.duration);
191 this.statistics.statisticsData[entryName].medTimeMeasurement = this.median(this.statistics.statisticsData[entryName].timeMeasurementSeries);
192 this.statistics.statisticsData[entryName].ninetyFiveThPercentileTimeMeasurement = this.percentile(this.statistics.statisticsData[entryName].timeMeasurementSeries, 95);
193 this.statistics.statisticsData[entryName].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[entryName].timeMeasurementSeries);
194 if (Configuration.getPerformanceStorage().enabled) {
195 parentPort.postMessage({ id: ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS, data: this.statistics });
196 }
197 }
198
199 private logPrefix(): string {
200 return Utils.logPrefix(` ${this.objId} | Performance statistics`);
201 }
202 }