Apply dependencies update
[e-mobility-charging-stations-simulator.git] / src / performance / PerformanceStatistics.ts
CommitLineData
c8eeb62b
JB
1// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
2
57939a9d 3import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
6c1761d4 4import type { URL } from 'url';
8114d10e 5import { parentPort } from 'worker_threads';
63b48f77 6
32de5a57 7import { MessageChannelUtils } from '../charging-station/MessageChannelUtils';
d2a64eb5 8import { MessageType } from '../types/ocpp/MessageType';
6c1761d4 9import type { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
8a36b1eb 10import type { Statistics, StatisticsData, TimeSeries } from '../types/Statistics';
8114d10e
JB
11import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from '../utils/CircularArray';
12import Configuration from '../utils/Configuration';
9f2e3130 13import logger from '../utils/Logger';
8114d10e 14import Utils from '../utils/Utils';
7dde0b73 15
54b1efe0 16export default class PerformanceStatistics {
e7aeea18
JB
17 private static readonly instances: Map<string, PerformanceStatistics> = new Map<
18 string,
19 PerformanceStatistics
20 >();
10068088 21
9e23580d 22 private readonly objId: string;
9f2e3130 23 private readonly objName: string;
aef1b33a 24 private performanceObserver: PerformanceObserver;
9e23580d 25 private readonly statistics: Statistics;
aef1b33a 26 private displayInterval: NodeJS.Timeout;
560bcf5b 27
9f2e3130 28 private constructor(objId: string, objName: string, uri: URL) {
c0560973 29 this.objId = objId;
9f2e3130 30 this.objName = objName;
aef1b33a 31 this.initializePerformanceObserver();
e7aeea18
JB
32 this.statistics = {
33 id: this.objId ?? 'Object id not specified',
34 name: this.objName ?? 'Object name not specified',
35 uri: uri.toString(),
36 createdAt: new Date(),
37 statisticsData: new Map<string, Partial<StatisticsData>>(),
38 };
9f2e3130
JB
39 }
40
844e496b
JB
41 public static getInstance(
42 objId: string,
43 objName: string,
44 uri: URL
45 ): PerformanceStatistics | undefined {
9f2e3130
JB
46 if (!PerformanceStatistics.instances.has(objId)) {
47 PerformanceStatistics.instances.set(objId, new PerformanceStatistics(objId, objName, uri));
48 }
49 return PerformanceStatistics.instances.get(objId);
560bcf5b
JB
50 }
51
aef1b33a 52 public static beginMeasure(id: string): string {
c63c21bc
JB
53 const markId = `${id.charAt(0).toUpperCase() + id.slice(1)}~${Utils.generateUUID()}`;
54 performance.mark(markId);
55 return markId;
57939a9d
JB
56 }
57
c63c21bc
JB
58 public static endMeasure(name: string, markId: string): void {
59 performance.measure(name, markId);
60 performance.clearMarks(markId);
aef1b33a
JB
61 }
62
e7aeea18
JB
63 public addRequestStatistic(
64 command: RequestCommand | IncomingRequestCommand,
65 messageType: MessageType
66 ): void {
7f134aca 67 switch (messageType) {
d2a64eb5 68 case MessageType.CALL_MESSAGE:
e7aeea18
JB
69 if (
70 this.statistics.statisticsData.has(command) &&
71 this.statistics.statisticsData.get(command)?.countRequest
72 ) {
ff4b895e 73 this.statistics.statisticsData.get(command).countRequest++;
7dde0b73 74 } else {
e7aeea18
JB
75 this.statistics.statisticsData.set(
76 command,
77 Object.assign({ countRequest: 1 }, this.statistics.statisticsData.get(command))
78 );
7f134aca
JB
79 }
80 break;
d2a64eb5 81 case MessageType.CALL_RESULT_MESSAGE:
e7aeea18
JB
82 if (
83 this.statistics.statisticsData.has(command) &&
84 this.statistics.statisticsData.get(command)?.countResponse
85 ) {
ff4b895e 86 this.statistics.statisticsData.get(command).countResponse++;
7f134aca 87 } else {
e7aeea18
JB
88 this.statistics.statisticsData.set(
89 command,
90 Object.assign({ countResponse: 1 }, this.statistics.statisticsData.get(command))
91 );
7dde0b73 92 }
7f134aca 93 break;
d2a64eb5 94 case MessageType.CALL_ERROR_MESSAGE:
e7aeea18
JB
95 if (
96 this.statistics.statisticsData.has(command) &&
97 this.statistics.statisticsData.get(command)?.countError
98 ) {
ff4b895e 99 this.statistics.statisticsData.get(command).countError++;
7f134aca 100 } else {
e7aeea18
JB
101 this.statistics.statisticsData.set(
102 command,
103 Object.assign({ countError: 1 }, this.statistics.statisticsData.get(command))
104 );
7f134aca
JB
105 }
106 break;
107 default:
9534e74e 108 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
9f2e3130 109 logger.error(`${this.logPrefix()} wrong message type ${messageType}`);
7f134aca 110 break;
7dde0b73
JB
111 }
112 }
113
aef1b33a 114 public start(): void {
72f041bd
JB
115 this.startLogStatisticsInterval();
116 if (Configuration.getPerformanceStorage().enabled) {
e7aeea18
JB
117 logger.info(
118 `${this.logPrefix()} storage enabled: type ${
119 Configuration.getPerformanceStorage().type
120 }, uri: ${Configuration.getPerformanceStorage().uri}`
121 );
72f041bd 122 }
7dde0b73
JB
123 }
124
aef1b33a 125 public stop(): void {
7874b0b1
JB
126 if (this.displayInterval) {
127 clearInterval(this.displayInterval);
128 }
aef1b33a 129 performance.clearMarks();
087a502d
JB
130 this.performanceObserver?.disconnect();
131 }
132
133 public restart(): void {
134 this.stop();
135 this.start();
136c90ba
JB
136 }
137
aef1b33a
JB
138 private initializePerformanceObserver(): void {
139 this.performanceObserver = new PerformanceObserver((list) => {
eb835fa8
JB
140 const lastPerformanceEntry = list.getEntries()[0];
141 this.addPerformanceEntryToStatistics(lastPerformanceEntry);
e7aeea18
JB
142 logger.debug(
143 `${this.logPrefix()} '${lastPerformanceEntry.name}' performance entry: %j`,
144 lastPerformanceEntry
145 );
a0ba4ced 146 });
aef1b33a
JB
147 this.performanceObserver.observe({ entryTypes: ['measure'] });
148 }
149
aef1b33a 150 private logStatistics(): void {
9f2e3130 151 logger.info(this.logPrefix() + ' %j', this.statistics);
7dde0b73
JB
152 }
153
72f041bd
JB
154 private startLogStatisticsInterval(): void {
155 if (Configuration.getLogStatisticsInterval() > 0) {
aef1b33a
JB
156 this.displayInterval = setInterval(() => {
157 this.logStatistics();
72f041bd 158 }, Configuration.getLogStatisticsInterval() * 1000);
e7aeea18
JB
159 logger.info(
160 this.logPrefix() +
161 ' logged every ' +
162 Utils.formatDurationSeconds(Configuration.getLogStatisticsInterval())
163 );
aef1b33a 164 } else {
e7aeea18
JB
165 logger.info(
166 this.logPrefix() +
167 ' log interval is set to ' +
168 Configuration.getLogStatisticsInterval().toString() +
169 '. Not logging statistics'
170 );
7dde0b73
JB
171 }
172 }
173
6bf6769e 174 private median(dataSet: number[]): number {
5f7e72c1 175 if (Array.isArray(dataSet) === true && dataSet.length === 1) {
6bf6769e
JB
176 return dataSet[0];
177 }
e7aeea18 178 const sortedDataSet = dataSet.slice().sort((a, b) => a - b);
6bf6769e
JB
179 const middleIndex = Math.floor(sortedDataSet.length / 2);
180 if (sortedDataSet.length % 2) {
181 return sortedDataSet[middleIndex / 2];
182 }
e7aeea18 183 return (sortedDataSet[middleIndex - 1] + sortedDataSet[middleIndex]) / 2;
6bf6769e
JB
184 }
185
b49422c6
JB
186 // TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree
187 private percentile(dataSet: number[], percentile: number): number {
188 if (percentile < 0 && percentile > 100) {
189 throw new RangeError('Percentile is not between 0 and 100');
190 }
191 if (Utils.isEmptyArray(dataSet)) {
192 return 0;
193 }
e7aeea18 194 const sortedDataSet = dataSet.slice().sort((a, b) => a - b);
b49422c6
JB
195 if (percentile === 0) {
196 return sortedDataSet[0];
197 }
198 if (percentile === 100) {
199 return sortedDataSet[sortedDataSet.length - 1];
200 }
e7aeea18 201 const percentileIndex = (percentile / 100) * sortedDataSet.length - 1;
b49422c6
JB
202 if (Number.isInteger(percentileIndex)) {
203 return (sortedDataSet[percentileIndex] + sortedDataSet[percentileIndex + 1]) / 2;
204 }
205 return sortedDataSet[Math.round(percentileIndex)];
206 }
207
aeada1fa
JB
208 private stdDeviation(dataSet: number[]): number {
209 let totalDataSet = 0;
210 for (const data of dataSet) {
211 totalDataSet += data;
212 }
213 const dataSetMean = totalDataSet / dataSet.length;
214 let totalGeometricDeviation = 0;
215 for (const data of dataSet) {
216 const deviation = data - dataSetMean;
217 totalGeometricDeviation += deviation * deviation;
218 }
219 return Math.sqrt(totalGeometricDeviation / dataSet.length);
220 }
221
b49422c6 222 private addPerformanceEntryToStatistics(entry: PerformanceEntry): void {
976d11ec 223 const entryName = entry.name;
7ec46a9a 224 // Initialize command statistics
ff4b895e
JB
225 if (!this.statistics.statisticsData.has(entryName)) {
226 this.statistics.statisticsData.set(entryName, {});
7ec46a9a 227 }
b49422c6 228 // Update current statistics
a6b3c6c3 229 this.statistics.updatedAt = new Date();
e7aeea18
JB
230 this.statistics.statisticsData.get(entryName).countTimeMeasurement =
231 this.statistics.statisticsData.get(entryName)?.countTimeMeasurement
232 ? this.statistics.statisticsData.get(entryName).countTimeMeasurement + 1
233 : 1;
ff4b895e 234 this.statistics.statisticsData.get(entryName).currentTimeMeasurement = entry.duration;
e7aeea18
JB
235 this.statistics.statisticsData.get(entryName).minTimeMeasurement =
236 this.statistics.statisticsData.get(entryName)?.minTimeMeasurement
237 ? this.statistics.statisticsData.get(entryName).minTimeMeasurement > entry.duration
238 ? entry.duration
239 : this.statistics.statisticsData.get(entryName).minTimeMeasurement
240 : entry.duration;
241 this.statistics.statisticsData.get(entryName).maxTimeMeasurement =
242 this.statistics.statisticsData.get(entryName)?.maxTimeMeasurement
243 ? this.statistics.statisticsData.get(entryName).maxTimeMeasurement < entry.duration
244 ? entry.duration
245 : this.statistics.statisticsData.get(entryName).maxTimeMeasurement
246 : entry.duration;
247 this.statistics.statisticsData.get(entryName).totalTimeMeasurement =
248 this.statistics.statisticsData.get(entryName)?.totalTimeMeasurement
249 ? this.statistics.statisticsData.get(entryName).totalTimeMeasurement + entry.duration
250 : entry.duration;
251 this.statistics.statisticsData.get(entryName).avgTimeMeasurement =
252 this.statistics.statisticsData.get(entryName).totalTimeMeasurement /
253 this.statistics.statisticsData.get(entryName).countTimeMeasurement;
5f7e72c1 254 Array.isArray(this.statistics.statisticsData.get(entryName).timeMeasurementSeries) === true
e7aeea18
JB
255 ? this.statistics.statisticsData
256 .get(entryName)
257 .timeMeasurementSeries.push({ timestamp: entry.startTime, value: entry.duration })
258 : (this.statistics.statisticsData.get(entryName).timeMeasurementSeries =
259 new CircularArray<TimeSeries>(DEFAULT_CIRCULAR_ARRAY_SIZE, {
260 timestamp: entry.startTime,
261 value: entry.duration,
262 }));
263 this.statistics.statisticsData.get(entryName).medTimeMeasurement = this.median(
264 this.extractTimeSeriesValues(
265 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
266 )
267 );
268 this.statistics.statisticsData.get(entryName).ninetyFiveThPercentileTimeMeasurement =
269 this.percentile(
270 this.extractTimeSeriesValues(
271 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
272 ),
273 95
274 );
275 this.statistics.statisticsData.get(entryName).stdDevTimeMeasurement = this.stdDeviation(
276 this.extractTimeSeriesValues(
277 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
278 )
279 );
72f041bd 280 if (Configuration.getPerformanceStorage().enabled) {
32de5a57
LM
281 parentPort.postMessage(
282 MessageChannelUtils.buildPerformanceStatisticsMessage(this.statistics)
283 );
72f041bd 284 }
7ec46a9a
JB
285 }
286
0c142310
JB
287 private extractTimeSeriesValues(timeSeries: CircularArray<TimeSeries>): number[] {
288 return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
289 }
290
c0560973 291 private logPrefix(): string {
9f2e3130 292 return Utils.logPrefix(` ${this.objName} | Performance statistics`);
6af9012e 293 }
7dde0b73 294}