Storage: use worker threads message passing to store performance records from
[e-mobility-charging-stations-simulator.git] / src / utils / PerformanceStatistics.ts
1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
2
3 import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from './CircularArray';
4 import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests';
5 import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks';
6 import Statistics, { StatisticsData } from '../types/Statistics';
7
8 import Configuration from './Configuration';
9 import { MessageType } from '../types/ocpp/MessageType';
10 import Utils from './Utils';
11 import { WorkerEvents } from '../types/Worker';
12 import logger from './Logger';
13 import { parentPort } from 'worker_threads';
14
15 export default class PerformanceStatistics {
16 private objId: string;
17 private performanceObserver: PerformanceObserver;
18 private statistics: Statistics;
19 private displayInterval: NodeJS.Timeout;
20
21 public constructor(objId: string) {
22 this.objId = objId;
23 this.initializePerformanceObserver();
24 this.statistics = { id: this.objId ?? 'Object id not specified', createdAt: new Date(), statisticsData: {} };
25 }
26
27 public static beginMeasure(id: string): string {
28 const beginId = 'begin' + id.charAt(0).toUpperCase() + id.slice(1);
29 performance.mark(beginId);
30 return beginId;
31 }
32
33 public static endMeasure(name: string, beginId: string): void {
34 performance.measure(name, beginId);
35 performance.clearMarks(beginId);
36 }
37
38 public addRequestStatistic(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void {
39 switch (messageType) {
40 case MessageType.CALL_MESSAGE:
41 if (this.statistics.statisticsData[command] && this.statistics.statisticsData[command].countRequest) {
42 this.statistics.statisticsData[command].countRequest++;
43 } else {
44 this.statistics.statisticsData[command] = {} as StatisticsData;
45 this.statistics.statisticsData[command].countRequest = 1;
46 }
47 break;
48 case MessageType.CALL_RESULT_MESSAGE:
49 if (this.statistics.statisticsData[command]) {
50 if (this.statistics.statisticsData[command].countResponse) {
51 this.statistics.statisticsData[command].countResponse++;
52 } else {
53 this.statistics.statisticsData[command].countResponse = 1;
54 }
55 } else {
56 this.statistics.statisticsData[command] = {} as StatisticsData;
57 this.statistics.statisticsData[command].countResponse = 1;
58 }
59 break;
60 case MessageType.CALL_ERROR_MESSAGE:
61 if (this.statistics.statisticsData[command]) {
62 if (this.statistics.statisticsData[command].countError) {
63 this.statistics.statisticsData[command].countError++;
64 } else {
65 this.statistics.statisticsData[command].countError = 1;
66 }
67 } else {
68 this.statistics.statisticsData[command] = {} as StatisticsData;
69 this.statistics.statisticsData[command].countError = 1;
70 }
71 break;
72 default:
73 logger.error(`${this.logPrefix()} wrong message type ${messageType}`);
74 break;
75 }
76 }
77
78 public start(): void {
79 this.startLogStatisticsInterval();
80 if (Configuration.getPerformanceStorage().enabled) {
81 logger.info(`${this.logPrefix()} storage enabled: type ${Configuration.getPerformanceStorage().type}, URI: ${Configuration.getPerformanceStorage().URI}`);
82 }
83 }
84
85 public stop(): void {
86 if (this.displayInterval) {
87 clearInterval(this.displayInterval);
88 }
89 performance.clearMarks();
90 this.performanceObserver?.disconnect();
91 }
92
93 public restart(): void {
94 this.stop();
95 this.start();
96 }
97
98 private initializePerformanceObserver(): void {
99 this.performanceObserver = new PerformanceObserver((list) => {
100 this.addPerformanceEntryToStatistics(list.getEntries()[0]);
101 logger.debug(`${this.logPrefix()} '${list.getEntries()[0].name}' performance entry: %j`, list.getEntries()[0]);
102 });
103 this.performanceObserver.observe({ entryTypes: ['measure'] });
104 }
105
106 private logStatistics(): void {
107 logger.info(this.logPrefix() + ' %j', this.statistics);
108 }
109
110 private startLogStatisticsInterval(): void {
111 if (Configuration.getLogStatisticsInterval() > 0) {
112 this.displayInterval = setInterval(() => {
113 this.logStatistics();
114 }, Configuration.getLogStatisticsInterval() * 1000);
115 logger.info(this.logPrefix() + ' logged every ' + Utils.secondsToHHMMSS(Configuration.getLogStatisticsInterval()));
116 } else {
117 logger.info(this.logPrefix() + ' log interval is set to ' + Configuration.getLogStatisticsInterval().toString() + '. Not logging statistics');
118 }
119 }
120
121 private median(dataSet: number[]): number {
122 if (Array.isArray(dataSet) && dataSet.length === 1) {
123 return dataSet[0];
124 }
125 const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
126 const middleIndex = Math.floor(sortedDataSet.length / 2);
127 if (sortedDataSet.length % 2) {
128 return sortedDataSet[middleIndex / 2];
129 }
130 return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2;
131 }
132
133 // TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree
134 private percentile(dataSet: number[], percentile: number): number {
135 if (percentile < 0 && percentile > 100) {
136 throw new RangeError('Percentile is not between 0 and 100');
137 }
138 if (Utils.isEmptyArray(dataSet)) {
139 return 0;
140 }
141 const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
142 if (percentile === 0) {
143 return sortedDataSet[0];
144 }
145 if (percentile === 100) {
146 return sortedDataSet[sortedDataSet.length - 1];
147 }
148 const percentileIndex = ((percentile / 100) * sortedDataSet.length) - 1;
149 if (Number.isInteger(percentileIndex)) {
150 return (sortedDataSet[percentileIndex] + sortedDataSet[percentileIndex + 1]) / 2;
151 }
152 return sortedDataSet[Math.round(percentileIndex)];
153 }
154
155 private stdDeviation(dataSet: number[]): number {
156 let totalDataSet = 0;
157 for (const data of dataSet) {
158 totalDataSet += data;
159 }
160 const dataSetMean = totalDataSet / dataSet.length;
161 let totalGeometricDeviation = 0;
162 for (const data of dataSet) {
163 const deviation = data - dataSetMean;
164 totalGeometricDeviation += deviation * deviation;
165 }
166 return Math.sqrt(totalGeometricDeviation / dataSet.length);
167 }
168
169 private addPerformanceEntryToStatistics(entry: PerformanceEntry): void {
170 let entryName = entry.name;
171 // Rename entry name
172 const MAP_NAME: Record<string, string> = {};
173 if (MAP_NAME[entryName]) {
174 entryName = MAP_NAME[entryName];
175 }
176 // Initialize command statistics
177 if (!this.statistics.statisticsData[entryName]) {
178 this.statistics.statisticsData[entryName] = {} as StatisticsData;
179 }
180 // Update current statistics
181 this.statistics.lastUpdatedAt = new Date();
182 this.statistics.statisticsData[entryName].countTimeMeasurement = this.statistics.statisticsData[entryName].countTimeMeasurement ? this.statistics.statisticsData[entryName].countTimeMeasurement + 1 : 1;
183 this.statistics.statisticsData[entryName].currentTimeMeasurement = entry.duration;
184 this.statistics.statisticsData[entryName].minTimeMeasurement = this.statistics.statisticsData[entryName].minTimeMeasurement ? (this.statistics.statisticsData[entryName].minTimeMeasurement > entry.duration ? entry.duration : this.statistics.statisticsData[entryName].minTimeMeasurement) : entry.duration;
185 this.statistics.statisticsData[entryName].maxTimeMeasurement = this.statistics.statisticsData[entryName].maxTimeMeasurement ? (this.statistics.statisticsData[entryName].maxTimeMeasurement < entry.duration ? entry.duration : this.statistics.statisticsData[entryName].maxTimeMeasurement) : entry.duration;
186 this.statistics.statisticsData[entryName].totalTimeMeasurement = this.statistics.statisticsData[entryName].totalTimeMeasurement ? this.statistics.statisticsData[entryName].totalTimeMeasurement + entry.duration : entry.duration;
187 this.statistics.statisticsData[entryName].avgTimeMeasurement = this.statistics.statisticsData[entryName].totalTimeMeasurement / this.statistics.statisticsData[entryName].countTimeMeasurement;
188 Array.isArray(this.statistics.statisticsData[entryName].timeMeasurementSeries) ? this.statistics.statisticsData[entryName].timeMeasurementSeries.push(entry.duration) : this.statistics.statisticsData[entryName].timeMeasurementSeries = new CircularArray<number>(DEFAULT_CIRCULAR_ARRAY_SIZE, entry.duration);
189 this.statistics.statisticsData[entryName].medTimeMeasurement = this.median(this.statistics.statisticsData[entryName].timeMeasurementSeries);
190 this.statistics.statisticsData[entryName].ninetyFiveThPercentileTimeMeasurement = this.percentile(this.statistics.statisticsData[entryName].timeMeasurementSeries, 95);
191 this.statistics.statisticsData[entryName].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[entryName].timeMeasurementSeries);
192 if (Configuration.getPerformanceStorage().enabled) {
193 parentPort.postMessage({ id: WorkerEvents.PERFORMANCE_STATISTICS, data: this.statistics });
194 }
195 }
196
197 private logPrefix(): string {
198 return Utils.logPrefix(` ${this.objId} | Performance statistics`);
199 }
200 }