1 import { CircularArray
, DEFAULT_CIRCULAR_ARRAY_SIZE
} from
'./CircularArray';
2 import CommandStatistics
, { CommandStatisticsData
, PerfEntry
} from
'../types/CommandStatistics';
3 import { IncomingRequestCommand
, RequestCommand
} from
'../types/ocpp/Requests';
5 import Configuration from
'./Configuration';
6 import { MessageType
} from
'../types/ocpp/MessageType';
7 import { PerformanceEntry
} from
'perf_hooks';
8 import Utils from
'./Utils';
9 import logger from
'./Logger';
11 export default class PerformanceStatistics
{
12 private objId
: string;
13 private commandsStatistics
: CommandStatistics
;
15 public constructor(objId
: string) {
17 this.commandsStatistics
= { id
: this.objId
? this.objId
: 'Object id not specified', commandsStatisticsData
: {} };
20 public addMessage(command
: RequestCommand
| IncomingRequestCommand
, messageType
: MessageType
): void {
21 switch (messageType
) {
22 case MessageType
.CALL_MESSAGE
:
23 if (this.commandsStatistics
.commandsStatisticsData
[command
] && this.commandsStatistics
.commandsStatisticsData
[command
].countRequest
) {
24 this.commandsStatistics
.commandsStatisticsData
[command
].countRequest
++;
26 this.commandsStatistics
.commandsStatisticsData
[command
] = {} as CommandStatisticsData
;
27 this.commandsStatistics
.commandsStatisticsData
[command
].countRequest
= 1;
30 case MessageType
.CALL_RESULT_MESSAGE
:
31 if (this.commandsStatistics
.commandsStatisticsData
[command
]) {
32 if (this.commandsStatistics
.commandsStatisticsData
[command
].countResponse
) {
33 this.commandsStatistics
.commandsStatisticsData
[command
].countResponse
++;
35 this.commandsStatistics
.commandsStatisticsData
[command
].countResponse
= 1;
38 this.commandsStatistics
.commandsStatisticsData
[command
] = {} as CommandStatisticsData
;
39 this.commandsStatistics
.commandsStatisticsData
[command
].countResponse
= 1;
42 case MessageType
.CALL_ERROR_MESSAGE
:
43 if (this.commandsStatistics
.commandsStatisticsData
[command
]) {
44 if (this.commandsStatistics
.commandsStatisticsData
[command
].countError
) {
45 this.commandsStatistics
.commandsStatisticsData
[command
].countError
++;
47 this.commandsStatistics
.commandsStatisticsData
[command
].countError
= 1;
50 this.commandsStatistics
.commandsStatisticsData
[command
] = {} as CommandStatisticsData
;
51 this.commandsStatistics
.commandsStatisticsData
[command
].countError
= 1;
55 logger
.error(`${this.logPrefix()} wrong message type ${messageType}`);
60 public logPerformance(entry
: PerformanceEntry
, className
: string): void {
61 this.addPerformanceTimer(entry
.name
as RequestCommand
| IncomingRequestCommand
, entry
.duration
);
62 const perfEntry
: PerfEntry
= {
64 entryType
: entry
.entryType
,
65 startTime
: entry
.startTime
,
66 duration
: entry
.duration
68 logger
.info(`${this.logPrefix()} ${className} method(s) entry: %j`, perfEntry
);
71 public start(): void {
72 this.displayInterval();
75 private display(): void {
76 logger
.info(this.logPrefix() + ' %j', this.commandsStatistics
);
79 private displayInterval(): void {
80 if (Configuration
.getStatisticsDisplayInterval() > 0) {
83 }, Configuration
.getStatisticsDisplayInterval() * 1000);
84 logger
.info(this.logPrefix() + ' displayed every ' + Utils
.secondsToHHMMSS(Configuration
.getStatisticsDisplayInterval()));
88 private median(dataSet
: number[]): number {
89 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
92 const sortedDataSet
= dataSet
.slice().sort();
93 const middleIndex
= Math.floor(sortedDataSet
.length
/ 2);
94 if (sortedDataSet
.length
% 2) {
95 return sortedDataSet
[middleIndex
/ 2];
97 return (sortedDataSet
[(middleIndex
- 1)] + sortedDataSet
[middleIndex
]) / 2;
100 private stdDeviation(dataSet
: number[]): number {
101 let totalDataSet
= 0;
102 for (const data
of dataSet
) {
103 totalDataSet
+= data
;
105 const dataSetMean
= totalDataSet
/ dataSet
.length
;
106 let totalGeometricDeviation
= 0;
107 for (const data
of dataSet
) {
108 const deviation
= data
- dataSetMean
;
109 totalGeometricDeviation
+= deviation
* deviation
;
111 return Math.sqrt(totalGeometricDeviation
/ dataSet
.length
);
114 private addPerformanceTimer(command
: RequestCommand
| IncomingRequestCommand
, duration
: number): void {
115 // Map to proper command name
116 const MAP_COMMAND
= {
117 sendMeterValues
: RequestCommand
.METER_VALUES
,
118 startTransaction
: RequestCommand
.START_TRANSACTION
,
119 stopTransaction
: RequestCommand
.STOP_TRANSACTION
,
121 if (MAP_COMMAND
[command
]) {
122 command
= MAP_COMMAND
[command
] as RequestCommand
| IncomingRequestCommand
;
124 // Initialize command statistics
125 if (!this.commandsStatistics
.commandsStatisticsData
[command
]) {
126 this.commandsStatistics
.commandsStatisticsData
[command
] = {} as CommandStatisticsData
;
128 // Update current statistics timers
129 this.commandsStatistics
.commandsStatisticsData
[command
].countTimeMeasurement
= this.commandsStatistics
.commandsStatisticsData
[command
].countTimeMeasurement
? this.commandsStatistics
.commandsStatisticsData
[command
].countTimeMeasurement
+ 1 : 1;
130 this.commandsStatistics
.commandsStatisticsData
[command
].currentTimeMeasurement
= duration
;
131 this.commandsStatistics
.commandsStatisticsData
[command
].minTimeMeasurement
= this.commandsStatistics
.commandsStatisticsData
[command
].minTimeMeasurement
? (this.commandsStatistics
.commandsStatisticsData
[command
].minTimeMeasurement
> duration
? duration
: this.commandsStatistics
.commandsStatisticsData
[command
].minTimeMeasurement
) : duration
;
132 this.commandsStatistics
.commandsStatisticsData
[command
].maxTimeMeasurement
= this.commandsStatistics
.commandsStatisticsData
[command
].maxTimeMeasurement
? (this.commandsStatistics
.commandsStatisticsData
[command
].maxTimeMeasurement
< duration
? duration
: this.commandsStatistics
.commandsStatisticsData
[command
].maxTimeMeasurement
) : duration
;
133 this.commandsStatistics
.commandsStatisticsData
[command
].totalTimeMeasurement
= this.commandsStatistics
.commandsStatisticsData
[command
].totalTimeMeasurement
? this.commandsStatistics
.commandsStatisticsData
[command
].totalTimeMeasurement
+ duration
: duration
;
134 this.commandsStatistics
.commandsStatisticsData
[command
].avgTimeMeasurement
= this.commandsStatistics
.commandsStatisticsData
[command
].totalTimeMeasurement
/ this.commandsStatistics
.commandsStatisticsData
[command
].countTimeMeasurement
;
135 Array.isArray(this.commandsStatistics
.commandsStatisticsData
[command
].timeMeasurementSeries
) ? this.commandsStatistics
.commandsStatisticsData
[command
].timeMeasurementSeries
.push(duration
) : this.commandsStatistics
.commandsStatisticsData
[command
].timeMeasurementSeries
= new CircularArray
<number>(DEFAULT_CIRCULAR_ARRAY_SIZE
, duration
);
136 this.commandsStatistics
.commandsStatisticsData
[command
].medTimeMeasurement
= this.median(this.commandsStatistics
.commandsStatisticsData
[command
].timeMeasurementSeries
);
137 this.commandsStatistics
.commandsStatisticsData
[command
].stdDevTimeMeasurement
= this.stdDeviation(this.commandsStatistics
.commandsStatisticsData
[command
].timeMeasurementSeries
);
140 private logPrefix(): string {
141 return Utils
.logPrefix(` ${this.objId} | Performance statistics`);