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