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); | |
31 | } | |
32 | ||
33 | public addRequestStatistic(command: RequestCommand | IncomingRequestCommand, messageType: MessageType): void { | |
7f134aca | 34 | switch (messageType) { |
d2a64eb5 | 35 | case MessageType.CALL_MESSAGE: |
aef1b33a JB |
36 | if (this.statistics.statisticsData[command] && this.statistics.statisticsData[command].countRequest) { |
37 | this.statistics.statisticsData[command].countRequest++; | |
7dde0b73 | 38 | } else { |
aef1b33a JB |
39 | this.statistics.statisticsData[command] = {} as StatisticsData; |
40 | this.statistics.statisticsData[command].countRequest = 1; | |
7f134aca JB |
41 | } |
42 | break; | |
d2a64eb5 | 43 | case MessageType.CALL_RESULT_MESSAGE: |
aef1b33a JB |
44 | if (this.statistics.statisticsData[command]) { |
45 | if (this.statistics.statisticsData[command].countResponse) { | |
46 | this.statistics.statisticsData[command].countResponse++; | |
7f134aca | 47 | } else { |
aef1b33a | 48 | this.statistics.statisticsData[command].countResponse = 1; |
7f134aca JB |
49 | } |
50 | } else { | |
aef1b33a JB |
51 | this.statistics.statisticsData[command] = {} as StatisticsData; |
52 | this.statistics.statisticsData[command].countResponse = 1; | |
7dde0b73 | 53 | } |
7f134aca | 54 | break; |
d2a64eb5 | 55 | case MessageType.CALL_ERROR_MESSAGE: |
aef1b33a JB |
56 | if (this.statistics.statisticsData[command]) { |
57 | if (this.statistics.statisticsData[command].countError) { | |
58 | this.statistics.statisticsData[command].countError++; | |
7f134aca | 59 | } else { |
aef1b33a | 60 | this.statistics.statisticsData[command].countError = 1; |
7f134aca JB |
61 | } |
62 | } else { | |
aef1b33a JB |
63 | this.statistics.statisticsData[command] = {} as StatisticsData; |
64 | this.statistics.statisticsData[command].countError = 1; | |
7f134aca JB |
65 | } |
66 | break; | |
67 | default: | |
54b1efe0 | 68 | logger.error(`${this.logPrefix()} wrong message type ${messageType}`); |
7f134aca | 69 | break; |
7dde0b73 JB |
70 | } |
71 | } | |
72 | ||
aef1b33a JB |
73 | public start(): void { |
74 | this.startDisplayInterval(); | |
7dde0b73 JB |
75 | } |
76 | ||
aef1b33a | 77 | public stop(): void { |
7874b0b1 JB |
78 | if (this.displayInterval) { |
79 | clearInterval(this.displayInterval); | |
80 | } | |
aef1b33a | 81 | performance.clearMarks(); |
087a502d JB |
82 | this.performanceObserver?.disconnect(); |
83 | } | |
84 | ||
85 | public restart(): void { | |
86 | this.stop(); | |
87 | this.start(); | |
136c90ba JB |
88 | } |
89 | ||
aef1b33a JB |
90 | private initializePerformanceObserver(): void { |
91 | this.performanceObserver = new PerformanceObserver((list) => { | |
087a502d | 92 | this.addPerformanceEntry(list.getEntries()[0]); |
a0ba4ced | 93 | }); |
aef1b33a JB |
94 | this.performanceObserver.observe({ entryTypes: ['measure'] }); |
95 | } | |
96 | ||
087a502d | 97 | private addPerformanceEntry(entry: PerformanceEntry): void { |
aef1b33a JB |
98 | this.addPerformanceStatistic(entry.name, entry.duration); |
99 | logger.debug(`${this.logPrefix()} '${entry.name}' performance entry: %j`, entry); | |
a0ba4ced JB |
100 | } |
101 | ||
aef1b33a JB |
102 | private logStatistics(): void { |
103 | logger.info(this.logPrefix() + ' %j', this.statistics); | |
7dde0b73 JB |
104 | } |
105 | ||
aef1b33a | 106 | private startDisplayInterval(): void { |
c6b89400 | 107 | if (Configuration.getStatisticsDisplayInterval() > 0) { |
aef1b33a JB |
108 | this.displayInterval = setInterval(() => { |
109 | this.logStatistics(); | |
7dde0b73 | 110 | }, Configuration.getStatisticsDisplayInterval() * 1000); |
c0560973 | 111 | logger.info(this.logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval())); |
aef1b33a JB |
112 | } else { |
113 | logger.info(this.logPrefix() + ' display interval is set to ' + Configuration.getStatisticsDisplayInterval().toString() + '. Not displaying statistics'); | |
7dde0b73 JB |
114 | } |
115 | } | |
116 | ||
6bf6769e JB |
117 | private median(dataSet: number[]): number { |
118 | if (Array.isArray(dataSet) && dataSet.length === 1) { | |
119 | return dataSet[0]; | |
120 | } | |
121 | const sortedDataSet = dataSet.slice().sort(); | |
122 | const middleIndex = Math.floor(sortedDataSet.length / 2); | |
123 | if (sortedDataSet.length % 2) { | |
124 | return sortedDataSet[middleIndex / 2]; | |
125 | } | |
126 | return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2; | |
127 | } | |
128 | ||
aeada1fa JB |
129 | private stdDeviation(dataSet: number[]): number { |
130 | let totalDataSet = 0; | |
131 | for (const data of dataSet) { | |
132 | totalDataSet += data; | |
133 | } | |
134 | const dataSetMean = totalDataSet / dataSet.length; | |
135 | let totalGeometricDeviation = 0; | |
136 | for (const data of dataSet) { | |
137 | const deviation = data - dataSetMean; | |
138 | totalGeometricDeviation += deviation * deviation; | |
139 | } | |
140 | return Math.sqrt(totalGeometricDeviation / dataSet.length); | |
141 | } | |
142 | ||
aef1b33a | 143 | private addPerformanceStatistic(name: string, duration: number): void { |
e9017bfc | 144 | // Rename entry name |
aef1b33a | 145 | const MAP_NAME = {}; |
e9017bfc JB |
146 | if (MAP_NAME[name]) { |
147 | name = MAP_NAME[name] as string; | |
7ec46a9a JB |
148 | } |
149 | // Initialize command statistics | |
aef1b33a JB |
150 | if (!this.statistics.statisticsData[name]) { |
151 | this.statistics.statisticsData[name] = {} as StatisticsData; | |
7ec46a9a JB |
152 | } |
153 | // Update current statistics timers | |
aef1b33a JB |
154 | this.statistics.statisticsData[name].countTimeMeasurement = this.statistics.statisticsData[name].countTimeMeasurement ? this.statistics.statisticsData[name].countTimeMeasurement + 1 : 1; |
155 | this.statistics.statisticsData[name].currentTimeMeasurement = duration; | |
156 | this.statistics.statisticsData[name].minTimeMeasurement = this.statistics.statisticsData[name].minTimeMeasurement ? (this.statistics.statisticsData[name].minTimeMeasurement > duration ? duration : this.statistics.statisticsData[name].minTimeMeasurement) : duration; | |
157 | this.statistics.statisticsData[name].maxTimeMeasurement = this.statistics.statisticsData[name].maxTimeMeasurement ? (this.statistics.statisticsData[name].maxTimeMeasurement < duration ? duration : this.statistics.statisticsData[name].maxTimeMeasurement) : duration; | |
158 | this.statistics.statisticsData[name].totalTimeMeasurement = this.statistics.statisticsData[name].totalTimeMeasurement ? this.statistics.statisticsData[name].totalTimeMeasurement + duration : duration; | |
159 | this.statistics.statisticsData[name].avgTimeMeasurement = this.statistics.statisticsData[name].totalTimeMeasurement / this.statistics.statisticsData[name].countTimeMeasurement; | |
160 | 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); | |
161 | this.statistics.statisticsData[name].medTimeMeasurement = this.median(this.statistics.statisticsData[name].timeMeasurementSeries); | |
162 | this.statistics.statisticsData[name].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[name].timeMeasurementSeries); | |
7ec46a9a JB |
163 | } |
164 | ||
c0560973 | 165 | private logPrefix(): string { |
54b1efe0 | 166 | return Utils.logPrefix(` ${this.objId} | Performance statistics`); |
6af9012e | 167 | } |
7dde0b73 | 168 | } |