Commit | Line | Data |
---|---|---|
ef72d3f5 | 1 | import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from './CircularArray'; |
c0560973 | 2 | import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests'; |
57939a9d | 3 | import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks'; |
aef1b33a | 4 | import Statistics, { StatisticsData } from '../types/Statistics'; |
63b48f77 | 5 | |
6af9012e | 6 | import Configuration from './Configuration'; |
d2a64eb5 | 7 | import { MessageType } from '../types/ocpp/MessageType'; |
6af9012e JB |
8 | import Utils from './Utils'; |
9 | import logger from './Logger'; | |
7dde0b73 | 10 | |
54b1efe0 | 11 | export default class PerformanceStatistics { |
418106c8 | 12 | private objId: string; |
aef1b33a JB |
13 | private performanceObserver: PerformanceObserver; |
14 | private statistics: Statistics; | |
15 | private displayInterval: NodeJS.Timeout; | |
560bcf5b | 16 | |
c0560973 JB |
17 | public constructor(objId: string) { |
18 | this.objId = objId; | |
aef1b33a JB |
19 | this.initializePerformanceObserver(); |
20 | this.statistics = { id: this.objId ?? 'Object id not specified', statisticsData: {} }; | |
560bcf5b JB |
21 | } |
22 | ||
aef1b33a JB |
23 | public static beginMeasure(id: string): string { |
24 | const beginId = 'begin' + id.charAt(0).toUpperCase() + id.slice(1); | |
25 | performance.mark(beginId); | |
26 | return beginId; | |
57939a9d JB |
27 | } |
28 | ||
aef1b33a JB |
29 | public static endMeasure(name: string, beginId: string): void { |
30 | performance.measure(name, beginId); | |
333eb100 | 31 | performance.clearMarks(beginId); |
aef1b33a JB |
32 | } |
33 | ||
34 | public addRequestStatistic(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void { | |
7f134aca | 35 | switch (messageType) { |
d2a64eb5 | 36 | case MessageType.CALL_MESSAGE: |
aef1b33a JB |
37 | if (this.statistics.statisticsData[command] && this.statistics.statisticsData[command].countRequest) { |
38 | this.statistics.statisticsData[command].countRequest++; | |
7dde0b73 | 39 | } else { |
aef1b33a JB |
40 | this.statistics.statisticsData[command] = {} as StatisticsData; |
41 | this.statistics.statisticsData[command].countRequest = 1; | |
7f134aca JB |
42 | } |
43 | break; | |
d2a64eb5 | 44 | case MessageType.CALL_RESULT_MESSAGE: |
aef1b33a JB |
45 | if (this.statistics.statisticsData[command]) { |
46 | if (this.statistics.statisticsData[command].countResponse) { | |
47 | this.statistics.statisticsData[command].countResponse++; | |
7f134aca | 48 | } else { |
aef1b33a | 49 | this.statistics.statisticsData[command].countResponse = 1; |
7f134aca JB |
50 | } |
51 | } else { | |
aef1b33a JB |
52 | this.statistics.statisticsData[command] = {} as StatisticsData; |
53 | this.statistics.statisticsData[command].countResponse = 1; | |
7dde0b73 | 54 | } |
7f134aca | 55 | break; |
d2a64eb5 | 56 | case MessageType.CALL_ERROR_MESSAGE: |
aef1b33a JB |
57 | if (this.statistics.statisticsData[command]) { |
58 | if (this.statistics.statisticsData[command].countError) { | |
59 | this.statistics.statisticsData[command].countError++; | |
7f134aca | 60 | } else { |
aef1b33a | 61 | this.statistics.statisticsData[command].countError = 1; |
7f134aca JB |
62 | } |
63 | } else { | |
aef1b33a JB |
64 | this.statistics.statisticsData[command] = {} as StatisticsData; |
65 | this.statistics.statisticsData[command].countError = 1; | |
7f134aca JB |
66 | } |
67 | break; | |
68 | default: | |
54b1efe0 | 69 | logger.error(`${this.logPrefix()} wrong message type ${messageType}`); |
7f134aca | 70 | break; |
7dde0b73 JB |
71 | } |
72 | } | |
73 | ||
aef1b33a JB |
74 | public start(): void { |
75 | this.startDisplayInterval(); | |
7dde0b73 JB |
76 | } |
77 | ||
aef1b33a | 78 | public stop(): void { |
7874b0b1 JB |
79 | if (this.displayInterval) { |
80 | clearInterval(this.displayInterval); | |
81 | } | |
aef1b33a | 82 | performance.clearMarks(); |
087a502d JB |
83 | this.performanceObserver?.disconnect(); |
84 | } | |
85 | ||
86 | public restart(): void { | |
87 | this.stop(); | |
88 | this.start(); | |
136c90ba JB |
89 | } |
90 | ||
aef1b33a JB |
91 | private initializePerformanceObserver(): void { |
92 | this.performanceObserver = new PerformanceObserver((list) => { | |
b49422c6 JB |
93 | this.addPerformanceEntryToStatistics(list.getEntries()[0]); |
94 | logger.debug(`${this.logPrefix()} '${list.getEntries()[0].name}' performance entry: %j`, list.getEntries()[0]); | |
a0ba4ced | 95 | }); |
aef1b33a JB |
96 | this.performanceObserver.observe({ entryTypes: ['measure'] }); |
97 | } | |
98 | ||
aef1b33a JB |
99 | private logStatistics(): void { |
100 | logger.info(this.logPrefix() + ' %j', this.statistics); | |
7dde0b73 JB |
101 | } |
102 | ||
aef1b33a | 103 | private startDisplayInterval(): void { |
c6b89400 | 104 | if (Configuration.getStatisticsDisplayInterval() > 0) { |
aef1b33a JB |
105 | this.displayInterval = setInterval(() => { |
106 | this.logStatistics(); | |
7dde0b73 | 107 | }, Configuration.getStatisticsDisplayInterval() * 1000); |
c0560973 | 108 | logger.info(this.logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval())); |
aef1b33a JB |
109 | } else { |
110 | logger.info(this.logPrefix() + ' display interval is set to ' + Configuration.getStatisticsDisplayInterval().toString() + '. Not displaying statistics'); | |
7dde0b73 JB |
111 | } |
112 | } | |
113 | ||
6bf6769e JB |
114 | private median(dataSet: number[]): number { |
115 | if (Array.isArray(dataSet) && dataSet.length === 1) { | |
116 | return dataSet[0]; | |
117 | } | |
118 | const sortedDataSet = dataSet.slice().sort(); | |
119 | const middleIndex = Math.floor(sortedDataSet.length / 2); | |
120 | if (sortedDataSet.length % 2) { | |
121 | return sortedDataSet[middleIndex / 2]; | |
122 | } | |
123 | return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2; | |
124 | } | |
125 | ||
b49422c6 JB |
126 | // TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree |
127 | private percentile(dataSet: number[], percentile: number): number { | |
128 | if (percentile < 0 && percentile > 100) { | |
129 | throw new RangeError('Percentile is not between 0 and 100'); | |
130 | } | |
131 | if (Utils.isEmptyArray(dataSet)) { | |
132 | return 0; | |
133 | } | |
134 | const sortedDataSet = dataSet.slice().sort(); | |
135 | if (percentile === 0) { | |
136 | return sortedDataSet[0]; | |
137 | } | |
138 | if (percentile === 100) { | |
139 | return sortedDataSet[sortedDataSet.length - 1]; | |
140 | } | |
141 | const percentileIndex = ((percentile / 100) * sortedDataSet.length) - 1; | |
142 | if (Number.isInteger(percentileIndex)) { | |
143 | return (sortedDataSet[percentileIndex] + sortedDataSet[percentileIndex + 1]) / 2; | |
144 | } | |
145 | return sortedDataSet[Math.round(percentileIndex)]; | |
146 | } | |
147 | ||
aeada1fa JB |
148 | private stdDeviation(dataSet: number[]): number { |
149 | let totalDataSet = 0; | |
150 | for (const data of dataSet) { | |
151 | totalDataSet += data; | |
152 | } | |
153 | const dataSetMean = totalDataSet / dataSet.length; | |
154 | let totalGeometricDeviation = 0; | |
155 | for (const data of dataSet) { | |
156 | const deviation = data - dataSetMean; | |
157 | totalGeometricDeviation += deviation * deviation; | |
158 | } | |
159 | return Math.sqrt(totalGeometricDeviation / dataSet.length); | |
160 | } | |
161 | ||
b49422c6 JB |
162 | private addPerformanceEntryToStatistics(entry: PerformanceEntry): void { |
163 | let entryName = entry.name; | |
e9017bfc | 164 | // Rename entry name |
b49422c6 JB |
165 | const MAP_NAME: Record<string, string> = {}; |
166 | if (MAP_NAME[entryName]) { | |
167 | entryName = MAP_NAME[entryName]; | |
7ec46a9a JB |
168 | } |
169 | // Initialize command statistics | |
b49422c6 JB |
170 | if (!this.statistics.statisticsData[entryName]) { |
171 | this.statistics.statisticsData[entryName] = {} as StatisticsData; | |
7ec46a9a | 172 | } |
b49422c6 JB |
173 | // Update current statistics |
174 | this.statistics.statisticsData[entryName].countTimeMeasurement = this.statistics.statisticsData[entryName].countTimeMeasurement ? this.statistics.statisticsData[entryName].countTimeMeasurement + 1 : 1; | |
175 | this.statistics.statisticsData[entryName].currentTimeMeasurement = entry.duration; | |
176 | 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; | |
177 | 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; | |
178 | this.statistics.statisticsData[entryName].totalTimeMeasurement = this.statistics.statisticsData[entryName].totalTimeMeasurement ? this.statistics.statisticsData[entryName].totalTimeMeasurement + entry.duration : entry.duration; | |
179 | this.statistics.statisticsData[entryName].avgTimeMeasurement = this.statistics.statisticsData[entryName].totalTimeMeasurement / this.statistics.statisticsData[entryName].countTimeMeasurement; | |
180 | 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); | |
181 | this.statistics.statisticsData[entryName].medTimeMeasurement = this.median(this.statistics.statisticsData[entryName].timeMeasurementSeries); | |
182 | this.statistics.statisticsData[entryName].ninetyFiveThPercentileTimeMeasurement = this.percentile(this.statistics.statisticsData[entryName].timeMeasurementSeries, 95); | |
183 | this.statistics.statisticsData[entryName].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[entryName].timeMeasurementSeries); | |
7ec46a9a JB |
184 | } |
185 | ||
c0560973 | 186 | private logPrefix(): string { |
54b1efe0 | 187 | return Utils.logPrefix(` ${this.objId} | Performance statistics`); |
6af9012e | 188 | } |
7dde0b73 | 189 | } |