import Configuration from './Configuration';
import { MessageType } from '../types/ocpp/MessageType';
import Utils from './Utils';
+import { WorkerEvents } from '../types/Worker';
import logger from './Logger';
+import { parentPort } from 'worker_threads';
export default class PerformanceStatistics {
private objId: string;
public constructor(objId: string) {
this.objId = objId;
this.initializePerformanceObserver();
- this.statistics = { id: this.objId ?? 'Object id not specified', statisticsData: {} };
+ this.statistics = { id: this.objId ?? 'Object id not specified', createdAt: new Date(), statisticsData: {} };
}
public static beginMeasure(id: string): string {
}
public start(): void {
- this.startDisplayInterval();
+ this.startLogStatisticsInterval();
+ if (Configuration.getPerformanceStorage().enabled) {
+ logger.info(`${this.logPrefix()} storage enabled: type ${Configuration.getPerformanceStorage().type}, URI: ${Configuration.getPerformanceStorage().URI}`);
+ }
}
public stop(): void {
logger.info(this.logPrefix() + ' %j', this.statistics);
}
- private startDisplayInterval(): void {
- if (Configuration.getStatisticsDisplayInterval() > 0) {
+ private startLogStatisticsInterval(): void {
+ if (Configuration.getLogStatisticsInterval() > 0) {
this.displayInterval = setInterval(() => {
this.logStatistics();
- }, Configuration.getStatisticsDisplayInterval() * 1000);
- logger.info(this.logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval()));
+ }, Configuration.getLogStatisticsInterval() * 1000);
+ logger.info(this.logPrefix() + ' logged every ' + Utils.secondsToHHMMSS(Configuration.getLogStatisticsInterval()));
} else {
- logger.info(this.logPrefix() + ' display interval is set to ' + Configuration.getStatisticsDisplayInterval().toString() + '. Not displaying statistics');
+ logger.info(this.logPrefix() + ' log interval is set to ' + Configuration.getLogStatisticsInterval().toString() + '. Not logging statistics');
}
}
if (Array.isArray(dataSet) && dataSet.length === 1) {
return dataSet[0];
}
- const sortedDataSet = dataSet.slice().sort();
+ const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
const middleIndex = Math.floor(sortedDataSet.length / 2);
if (sortedDataSet.length % 2) {
return sortedDataSet[middleIndex / 2];
if (Utils.isEmptyArray(dataSet)) {
return 0;
}
- const sortedDataSet = dataSet.slice().sort();
+ const sortedDataSet = dataSet.slice().sort((a, b) => (a - b));
if (percentile === 0) {
return sortedDataSet[0];
}
this.statistics.statisticsData[entryName] = {} as StatisticsData;
}
// Update current statistics
+ this.statistics.lastUpdatedAt = new Date();
this.statistics.statisticsData[entryName].countTimeMeasurement = this.statistics.statisticsData[entryName].countTimeMeasurement ? this.statistics.statisticsData[entryName].countTimeMeasurement + 1 : 1;
this.statistics.statisticsData[entryName].currentTimeMeasurement = entry.duration;
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;
this.statistics.statisticsData[entryName].medTimeMeasurement = this.median(this.statistics.statisticsData[entryName].timeMeasurementSeries);
this.statistics.statisticsData[entryName].ninetyFiveThPercentileTimeMeasurement = this.percentile(this.statistics.statisticsData[entryName].timeMeasurementSeries, 95);
this.statistics.statisticsData[entryName].stdDevTimeMeasurement = this.stdDeviation(this.statistics.statisticsData[entryName].timeMeasurementSeries);
+ if (Configuration.getPerformanceStorage().enabled) {
+ parentPort.postMessage({ id: WorkerEvents.PERFORMANCE_STATISTICS, data: this.statistics });
+ }
}
private logPrefix(): string {