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