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