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) => { | |
087a502d | 93 | this.addPerformanceEntry(list.getEntries()[0]); |
a0ba4ced | 94 | }); |
aef1b33a JB |
95 | this.performanceObserver.observe({ entryTypes: ['measure'] }); |
96 | } | |
97 | ||
087a502d | 98 | private addPerformanceEntry(entry: PerformanceEntry): void { |
aef1b33a JB |
99 | this.addPerformanceStatistic(entry.name, entry.duration); |
100 | logger.debug(`${this.logPrefix()} '${entry.name}' performance entry: %j`, entry); | |
a0ba4ced JB |
101 | } |
102 | ||
aef1b33a JB |
103 | private logStatistics(): void { |
104 | logger.info(this.logPrefix() + ' %j', this.statistics); | |
7dde0b73 JB |
105 | } |
106 | ||
aef1b33a | 107 | private startDisplayInterval(): void { |
c6b89400 | 108 | if (Configuration.getStatisticsDisplayInterval() > 0) { |
aef1b33a JB |
109 | this.displayInterval = setInterval(() => { |
110 | this.logStatistics(); | |
7dde0b73 | 111 | }, Configuration.getStatisticsDisplayInterval() * 1000); |
c0560973 | 112 | logger.info(this.logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval())); |
aef1b33a JB |
113 | } else { |
114 | logger.info(this.logPrefix() + ' display interval is set to ' + Configuration.getStatisticsDisplayInterval().toString() + '. Not displaying statistics'); | |
7dde0b73 JB |
115 | } |
116 | } | |
117 | ||
6bf6769e JB |
118 | private median(dataSet: number[]): number { |
119 | if (Array.isArray(dataSet) && dataSet.length === 1) { | |
120 | return dataSet[0]; | |
121 | } | |
122 | const sortedDataSet = dataSet.slice().sort(); | |
123 | const middleIndex = Math.floor(sortedDataSet.length / 2); | |
124 | if (sortedDataSet.length % 2) { | |
125 | return sortedDataSet[middleIndex / 2]; | |
126 | } | |
127 | return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2; | |
128 | } | |
129 | ||
aeada1fa JB |
130 | private stdDeviation(dataSet: number[]): number { |
131 | let totalDataSet = 0; | |
132 | for (const data of dataSet) { | |
133 | totalDataSet += data; | |
134 | } | |
135 | const dataSetMean = totalDataSet / dataSet.length; | |
136 | let totalGeometricDeviation = 0; | |
137 | for (const data of dataSet) { | |
138 | const deviation = data - dataSetMean; | |
139 | totalGeometricDeviation += deviation * deviation; | |
140 | } | |
141 | return Math.sqrt(totalGeometricDeviation / dataSet.length); | |
142 | } | |
143 | ||
aef1b33a | 144 | private addPerformanceStatistic(name: string, duration: number): void { |
e9017bfc | 145 | // Rename entry name |
aef1b33a | 146 | const MAP_NAME = {}; |
e9017bfc JB |
147 | if (MAP_NAME[name]) { |
148 | name = MAP_NAME[name] as string; | |
7ec46a9a JB |
149 | } |
150 | // Initialize command statistics | |
aef1b33a JB |
151 | if (!this.statistics.statisticsData[name]) { |
152 | this.statistics.statisticsData[name] = {} as StatisticsData; | |
7ec46a9a JB |
153 | } |
154 | // Update current statistics timers | |
aef1b33a JB |
155 | this.statistics.statisticsData[name].countTimeMeasurement = this.statistics.statisticsData[name].countTimeMeasurement ? this.statistics.statisticsData[name].countTimeMeasurement + 1 : 1; |
156 | this.statistics.statisticsData[name].currentTimeMeasurement = duration; | |
157 | this.statistics.statisticsData[name].minTimeMeasurement = this.statistics.statisticsData[name].minTimeMeasurement ? (this.statistics.statisticsData[name].minTimeMeasurement > duration ? duration : this.statistics.statisticsData[name].minTimeMeasurement) : duration; | |
158 | this.statistics.statisticsData[name].maxTimeMeasurement = this.statistics.statisticsData[name].maxTimeMeasurement ? (this.statistics.statisticsData[name].maxTimeMeasurement < duration ? duration : this.statistics.statisticsData[name].maxTimeMeasurement) : duration; | |
159 | this.statistics.statisticsData[name].totalTimeMeasurement = this.statistics.statisticsData[name].totalTimeMeasurement ? this.statistics.statisticsData[name].totalTimeMeasurement + duration : duration; | |
160 | this.statistics.statisticsData[name].avgTimeMeasurement = this.statistics.statisticsData[name].totalTimeMeasurement / this.statistics.statisticsData[name].countTimeMeasurement; | |
161 | Array.isArray(this.statistics.statisticsData[name].timeMeasurementSeries) ? this.statistics.statisticsData[name].timeMeasurementSeries.push(duration) : this.statistics.statisticsData[name].timeMeasurementSeries = new CircularArray<number>(DEFAULT_CIRCULAR_ARRAY_SIZE, duration); | |
162 | this.statistics.statisticsData[name].medTimeMeasurement = this.median(this.statistics.statisticsData[name].timeMeasurementSeries); | |
163 | this.statistics.statisticsData[name].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[name].timeMeasurementSeries); | |
7ec46a9a JB |
164 | } |
165 | ||
c0560973 | 166 | private logPrefix(): string { |
54b1efe0 | 167 | return Utils.logPrefix(` ${this.objId} | Performance statistics`); |
6af9012e | 168 | } |
7dde0b73 | 169 | } |