build(simulator): cleanup TS configuration
[e-mobility-charging-stations-simulator.git] / src / performance / PerformanceStatistics.ts
CommitLineData
edd13439 1// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
c8eeb62b 2
01f4001e 3import { type PerformanceEntry, PerformanceObserver, performance } from 'node:perf_hooks';
130783a7 4import type { URL } from 'node:url';
01f4001e 5import { parentPort } from 'node:worker_threads';
63b48f77 6
268a74bb
JB
7import {
8 type IncomingRequestCommand,
9 MessageType,
10 type RequestCommand,
11 type Statistics,
12 type TimeSeries,
13} from '../types';
7671fa0b
JB
14import {
15 CircularArray,
16 Configuration,
17 Constants,
7671fa0b 18 Utils,
c8faabc8 19 buildPerformanceStatisticsMessage,
7671fa0b 20 logger,
4884b8d3
JB
21 median,
22 nthPercentile,
23 stdDeviation,
7671fa0b 24} from '../utils';
7dde0b73 25
268a74bb 26export class PerformanceStatistics {
e7aeea18
JB
27 private static readonly instances: Map<string, PerformanceStatistics> = new Map<
28 string,
29 PerformanceStatistics
30 >();
10068088 31
9e23580d 32 private readonly objId: string;
9f2e3130 33 private readonly objName: string;
1895299d 34 private performanceObserver!: PerformanceObserver;
9e23580d 35 private readonly statistics: Statistics;
1895299d 36 private displayInterval!: NodeJS.Timeout;
560bcf5b 37
9f2e3130 38 private constructor(objId: string, objName: string, uri: URL) {
c0560973 39 this.objId = objId;
9f2e3130 40 this.objName = objName;
aef1b33a 41 this.initializePerformanceObserver();
e7aeea18
JB
42 this.statistics = {
43 id: this.objId ?? 'Object id not specified',
44 name: this.objName ?? 'Object name not specified',
45 uri: uri.toString(),
46 createdAt: new Date(),
1e4b0e4b 47 statisticsData: new Map(),
e7aeea18 48 };
9f2e3130
JB
49 }
50
844e496b
JB
51 public static getInstance(
52 objId: string,
53 objName: string,
54 uri: URL
55 ): PerformanceStatistics | undefined {
9f2e3130
JB
56 if (!PerformanceStatistics.instances.has(objId)) {
57 PerformanceStatistics.instances.set(objId, new PerformanceStatistics(objId, objName, uri));
58 }
59 return PerformanceStatistics.instances.get(objId);
560bcf5b
JB
60 }
61
aef1b33a 62 public static beginMeasure(id: string): string {
14ecae6a 63 const markId = `${id.charAt(0).toUpperCase()}${id.slice(1)}~${Utils.generateUUID()}`;
c63c21bc
JB
64 performance.mark(markId);
65 return markId;
57939a9d
JB
66 }
67
c63c21bc
JB
68 public static endMeasure(name: string, markId: string): void {
69 performance.measure(name, markId);
70 performance.clearMarks(markId);
c60af6ca 71 performance.clearMeasures(name);
aef1b33a
JB
72 }
73
e7aeea18
JB
74 public addRequestStatistic(
75 command: RequestCommand | IncomingRequestCommand,
76 messageType: MessageType
77 ): void {
7f134aca 78 switch (messageType) {
d2a64eb5 79 case MessageType.CALL_MESSAGE:
e7aeea18
JB
80 if (
81 this.statistics.statisticsData.has(command) &&
82 this.statistics.statisticsData.get(command)?.countRequest
83 ) {
1fe0632a 84 ++this.statistics.statisticsData.get(command).countRequest;
7dde0b73 85 } else {
71910904
JB
86 this.statistics.statisticsData.set(command, {
87 ...this.statistics.statisticsData.get(command),
88 countRequest: 1,
89 });
7f134aca
JB
90 }
91 break;
d2a64eb5 92 case MessageType.CALL_RESULT_MESSAGE:
e7aeea18
JB
93 if (
94 this.statistics.statisticsData.has(command) &&
95 this.statistics.statisticsData.get(command)?.countResponse
96 ) {
1fe0632a 97 ++this.statistics.statisticsData.get(command).countResponse;
7f134aca 98 } else {
71910904
JB
99 this.statistics.statisticsData.set(command, {
100 ...this.statistics.statisticsData.get(command),
101 countResponse: 1,
102 });
7dde0b73 103 }
7f134aca 104 break;
d2a64eb5 105 case MessageType.CALL_ERROR_MESSAGE:
e7aeea18
JB
106 if (
107 this.statistics.statisticsData.has(command) &&
108 this.statistics.statisticsData.get(command)?.countError
109 ) {
1fe0632a 110 ++this.statistics.statisticsData.get(command).countError;
7f134aca 111 } else {
71910904
JB
112 this.statistics.statisticsData.set(command, {
113 ...this.statistics.statisticsData.get(command),
114 countError: 1,
115 });
7f134aca
JB
116 }
117 break;
118 default:
9534e74e 119 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
9f2e3130 120 logger.error(`${this.logPrefix()} wrong message type ${messageType}`);
7f134aca 121 break;
7dde0b73
JB
122 }
123 }
124
aef1b33a 125 public start(): void {
72f041bd
JB
126 this.startLogStatisticsInterval();
127 if (Configuration.getPerformanceStorage().enabled) {
e7aeea18
JB
128 logger.info(
129 `${this.logPrefix()} storage enabled: type ${
130 Configuration.getPerformanceStorage().type
131 }, uri: ${Configuration.getPerformanceStorage().uri}`
132 );
72f041bd 133 }
7dde0b73
JB
134 }
135
aef1b33a 136 public stop(): void {
8f953431 137 this.stopLogStatisticsInterval();
aef1b33a 138 performance.clearMarks();
c60af6ca 139 performance.clearMeasures();
087a502d
JB
140 this.performanceObserver?.disconnect();
141 }
142
143 public restart(): void {
144 this.stop();
145 this.start();
136c90ba
JB
146 }
147
aef1b33a 148 private initializePerformanceObserver(): void {
72092cfc 149 this.performanceObserver = new PerformanceObserver((performanceObserverList) => {
c60af6ca 150 const lastPerformanceEntry = performanceObserverList.getEntries()[0];
9d1dc4b1
JB
151 // logger.debug(
152 // `${this.logPrefix()} '${lastPerformanceEntry.name}' performance entry: %j`,
153 // lastPerformanceEntry
154 // );
eb835fa8 155 this.addPerformanceEntryToStatistics(lastPerformanceEntry);
a0ba4ced 156 });
aef1b33a
JB
157 this.performanceObserver.observe({ entryTypes: ['measure'] });
158 }
159
aef1b33a 160 private logStatistics(): void {
c60af6ca
JB
161 logger.info(`${this.logPrefix()}`, {
162 ...this.statistics,
163 statisticsData: Utils.JSONStringifyWithMapSupport(this.statistics.statisticsData),
164 });
7dde0b73
JB
165 }
166
72f041bd 167 private startLogStatisticsInterval(): void {
3d48c1c1
JB
168 const logStatisticsInterval = Configuration.getLog().enabled
169 ? Configuration.getLog().statisticsInterval
170 : 0;
8f953431 171 if (logStatisticsInterval > 0 && !this.displayInterval) {
aef1b33a
JB
172 this.displayInterval = setInterval(() => {
173 this.logStatistics();
8f953431 174 }, logStatisticsInterval * 1000);
e7aeea18 175 logger.info(
8f953431 176 `${this.logPrefix()} logged every ${Utils.formatDurationSeconds(logStatisticsInterval)}`
e7aeea18 177 );
dfe81c8f
JB
178 } else if (this.displayInterval) {
179 logger.info(
180 `${this.logPrefix()} already logged every ${Utils.formatDurationSeconds(
8f953431 181 logStatisticsInterval
dfe81c8f
JB
182 )}`
183 );
3d48c1c1 184 } else if (Configuration.getLog().enabled) {
e7aeea18 185 logger.info(
8f953431 186 `${this.logPrefix()} log interval is set to ${logStatisticsInterval?.toString()}. Not logging statistics`
e7aeea18 187 );
7dde0b73
JB
188 }
189 }
190
8f953431
JB
191 private stopLogStatisticsInterval(): void {
192 if (this.displayInterval) {
193 clearInterval(this.displayInterval);
194 delete this.displayInterval;
195 }
196 }
197
b49422c6 198 private addPerformanceEntryToStatistics(entry: PerformanceEntry): void {
976d11ec 199 const entryName = entry.name;
7ec46a9a 200 // Initialize command statistics
ff4b895e 201 if (!this.statistics.statisticsData.has(entryName)) {
abe9e9dd 202 this.statistics.statisticsData.set(entryName, {});
7ec46a9a 203 }
b49422c6 204 // Update current statistics
a6b3c6c3 205 this.statistics.updatedAt = new Date();
e7aeea18 206 this.statistics.statisticsData.get(entryName).countTimeMeasurement =
a8735ef9 207 (this.statistics.statisticsData.get(entryName)?.countTimeMeasurement ?? 0) + 1;
ff4b895e 208 this.statistics.statisticsData.get(entryName).currentTimeMeasurement = entry.duration;
a8735ef9
JB
209 this.statistics.statisticsData.get(entryName).minTimeMeasurement = Math.min(
210 entry.duration,
211 this.statistics.statisticsData.get(entryName)?.minTimeMeasurement ?? Infinity
212 );
213 this.statistics.statisticsData.get(entryName).maxTimeMeasurement = Math.max(
214 entry.duration,
215 this.statistics.statisticsData.get(entryName)?.maxTimeMeasurement ?? -Infinity
216 );
e7aeea18 217 this.statistics.statisticsData.get(entryName).totalTimeMeasurement =
a8735ef9 218 (this.statistics.statisticsData.get(entryName)?.totalTimeMeasurement ?? 0) + entry.duration;
e7aeea18
JB
219 this.statistics.statisticsData.get(entryName).avgTimeMeasurement =
220 this.statistics.statisticsData.get(entryName).totalTimeMeasurement /
221 this.statistics.statisticsData.get(entryName).countTimeMeasurement;
9a15316c 222 this.statistics.statisticsData.get(entryName)?.timeMeasurementSeries instanceof CircularArray
e7aeea18
JB
223 ? this.statistics.statisticsData
224 .get(entryName)
72092cfc 225 ?.timeMeasurementSeries?.push({ timestamp: entry.startTime, value: entry.duration })
e7aeea18 226 : (this.statistics.statisticsData.get(entryName).timeMeasurementSeries =
9a15316c 227 new CircularArray<TimeSeries>(Constants.DEFAULT_CIRCULAR_BUFFER_CAPACITY, {
e7aeea18
JB
228 timestamp: entry.startTime,
229 value: entry.duration,
230 }));
4884b8d3 231 this.statistics.statisticsData.get(entryName).medTimeMeasurement = median(
e7aeea18
JB
232 this.extractTimeSeriesValues(
233 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
234 )
235 );
236 this.statistics.statisticsData.get(entryName).ninetyFiveThPercentileTimeMeasurement =
4884b8d3 237 nthPercentile(
e7aeea18
JB
238 this.extractTimeSeriesValues(
239 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
240 ),
241 95
242 );
4884b8d3 243 this.statistics.statisticsData.get(entryName).stdDevTimeMeasurement = stdDeviation(
e7aeea18
JB
244 this.extractTimeSeriesValues(
245 this.statistics.statisticsData.get(entryName).timeMeasurementSeries
246 )
247 );
72f041bd 248 if (Configuration.getPerformanceStorage().enabled) {
c8faabc8 249 parentPort?.postMessage(buildPerformanceStatisticsMessage(this.statistics));
72f041bd 250 }
7ec46a9a
JB
251 }
252
0c142310 253 private extractTimeSeriesValues(timeSeries: CircularArray<TimeSeries>): number[] {
72092cfc 254 return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
0c142310
JB
255 }
256
8b7072dc 257 private logPrefix = (): string => {
9f2e3130 258 return Utils.logPrefix(` ${this.objName} | Performance statistics`);
8b7072dc 259 };
7dde0b73 260}