Vue UI + UI server
[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';
8114d10e
JB
4import { URL } from 'url';
5import { parentPort } from 'worker_threads';
63b48f77 6
32de5a57 7import { MessageChannelUtils } from '../charging-station/MessageChannelUtils';
d2a64eb5 8import { MessageType } from '../types/ocpp/MessageType';
8114d10e
JB
9import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
10import Statistics, { StatisticsData, TimeSeries } from '../types/Statistics';
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
JB
174 private median(dataSet: number[]): number {
175 if (Array.isArray(dataSet) && dataSet.length === 1) {
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
JB
222 private addPerformanceEntryToStatistics(entry: PerformanceEntry): void {
223 let entryName = entry.name;
e9017bfc 224 // Rename entry name
b49422c6
JB
225 const MAP_NAME: Record<string, string> = {};
226 if (MAP_NAME[entryName]) {
227 entryName = MAP_NAME[entryName];
7ec46a9a
JB
228 }
229 // Initialize command statistics
ff4b895e
JB
230 if (!this.statistics.statisticsData.has(entryName)) {
231 this.statistics.statisticsData.set(entryName, {});
7ec46a9a 232 }
b49422c6 233 // Update current statistics
a6b3c6c3 234 this.statistics.updatedAt = new Date();
e7aeea18
JB
235 this.statistics.statisticsData.get(entryName).countTimeMeasurement =
236 this.statistics.statisticsData.get(entryName)?.countTimeMeasurement
237 ? this.statistics.statisticsData.get(entryName).countTimeMeasurement + 1
238 : 1;
ff4b895e 239 this.statistics.statisticsData.get(entryName).currentTimeMeasurement = entry.duration;
e7aeea18
JB
240 this.statistics.statisticsData.get(entryName).minTimeMeasurement =
241 this.statistics.statisticsData.get(entryName)?.minTimeMeasurement
242 ? this.statistics.statisticsData.get(entryName).minTimeMeasurement > entry.duration
243 ? entry.duration
244 : this.statistics.statisticsData.get(entryName).minTimeMeasurement
245 : entry.duration;
246 this.statistics.statisticsData.get(entryName).maxTimeMeasurement =
247 this.statistics.statisticsData.get(entryName)?.maxTimeMeasurement
248 ? this.statistics.statisticsData.get(entryName).maxTimeMeasurement < entry.duration
249 ? entry.duration
250 : this.statistics.statisticsData.get(entryName).maxTimeMeasurement
251 : entry.duration;
252 this.statistics.statisticsData.get(entryName).totalTimeMeasurement =
253 this.statistics.statisticsData.get(entryName)?.totalTimeMeasurement
254 ? this.statistics.statisticsData.get(entryName).totalTimeMeasurement + entry.duration
255 : entry.duration;
256 this.statistics.statisticsData.get(entryName).avgTimeMeasurement =
257 this.statistics.statisticsData.get(entryName).totalTimeMeasurement /
258 this.statistics.statisticsData.get(entryName).countTimeMeasurement;
0c142310 259 Array.isArray(this.statistics.statisticsData.get(entryName).timeMeasurementSeries)
e7aeea18
JB
260 ? this.statistics.statisticsData
261 .get(entryName)
262 .timeMeasurementSeries.push({ timestamp: entry.startTime, value: entry.duration })
263 : (this.statistics.statisticsData.get(entryName).timeMeasurementSeries =
264 new CircularArray<TimeSeries>(DEFAULT_CIRCULAR_ARRAY_SIZE, {
265 timestamp: entry.startTime,
266 value: entry.duration,
267 }));
268 this.statistics.statisticsData.get(entryName).medTimeMeasurement = this.median(
269 this.extractTimeSeriesValues(
270 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
271 )
272 );
273 this.statistics.statisticsData.get(entryName).ninetyFiveThPercentileTimeMeasurement =
274 this.percentile(
275 this.extractTimeSeriesValues(
276 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
277 ),
278 95
279 );
280 this.statistics.statisticsData.get(entryName).stdDevTimeMeasurement = this.stdDeviation(
281 this.extractTimeSeriesValues(
282 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
283 )
284 );
72f041bd 285 if (Configuration.getPerformanceStorage().enabled) {
32de5a57
LM
286 parentPort.postMessage(
287 MessageChannelUtils.buildPerformanceStatisticsMessage(this.statistics)
288 );
72f041bd 289 }
7ec46a9a
JB
290 }
291
0c142310
JB
292 private extractTimeSeriesValues(timeSeries: CircularArray<TimeSeries>): number[] {
293 return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
294 }
295
c0560973 296 private logPrefix(): string {
9f2e3130 297 return Utils.logPrefix(` ${this.objName} | Performance statistics`);
6af9012e 298 }
7dde0b73 299}