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