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