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