X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2FPerformanceStatistics.ts;h=ef9d393ab41b051d18266bc16f5ddefe090e7a60;hb=44eb6026079c8dc2c77b10a96a42d0c0b2da7c8f;hp=fdcabb33f6f89adcaf3adde29029b22493cf0771;hpb=9534e74eac653e8cdf02b17a33b273d421251a1c;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index fdcabb33..ef9d393a 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -1,17 +1,17 @@ -// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from '../utils/CircularArray'; -import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests'; import { PerformanceEntry, PerformanceObserver, performance } from 'perf_hooks'; -import Statistics, { StatisticsData, TimeSeries } from '../types/Statistics'; +import type { URL } from 'url'; +import { parentPort } from 'worker_threads'; -import { ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; -import Configuration from '../utils/Configuration'; +import { MessageChannelUtils } from '../charging-station/MessageChannelUtils'; import { MessageType } from '../types/ocpp/MessageType'; -import { URL } from 'url'; -import Utils from '../utils/Utils'; +import type { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests'; +import type { Statistics, TimeSeries } from '../types/Statistics'; +import { CircularArray, DEFAULT_CIRCULAR_ARRAY_SIZE } from '../utils/CircularArray'; +import Configuration from '../utils/Configuration'; import logger from '../utils/Logger'; -import { parentPort } from 'worker_threads'; +import Utils from '../utils/Utils'; export default class PerformanceStatistics { private static readonly instances: Map = new Map< @@ -34,11 +34,15 @@ export default class PerformanceStatistics { name: this.objName ?? 'Object name not specified', uri: uri.toString(), createdAt: new Date(), - statisticsData: new Map>(), + statisticsData: new Map(), }; } - public static getInstance(objId: string, objName: string, uri: URL): PerformanceStatistics { + public static getInstance( + objId: string, + objName: string, + uri: URL + ): PerformanceStatistics | undefined { if (!PerformanceStatistics.instances.has(objId)) { PerformanceStatistics.instances.set(objId, new PerformanceStatistics(objId, objName, uri)); } @@ -54,6 +58,7 @@ export default class PerformanceStatistics { public static endMeasure(name: string, markId: string): void { performance.measure(name, markId); performance.clearMarks(markId); + performance.clearMeasures(name); } public addRequestStatistic( @@ -123,6 +128,7 @@ export default class PerformanceStatistics { clearInterval(this.displayInterval); } performance.clearMarks(); + performance.clearMeasures(); this.performanceObserver?.disconnect(); } @@ -132,19 +138,22 @@ export default class PerformanceStatistics { } private initializePerformanceObserver(): void { - this.performanceObserver = new PerformanceObserver((list) => { - const lastPerformanceEntry = list.getEntries()[0]; + this.performanceObserver = new PerformanceObserver((performanceObserverList) => { + const lastPerformanceEntry = performanceObserverList.getEntries()[0]; + // logger.debug( + // `${this.logPrefix()} '${lastPerformanceEntry.name}' performance entry: %j`, + // lastPerformanceEntry + // ); this.addPerformanceEntryToStatistics(lastPerformanceEntry); - logger.debug( - `${this.logPrefix()} '${lastPerformanceEntry.name}' performance entry: %j`, - lastPerformanceEntry - ); }); this.performanceObserver.observe({ entryTypes: ['measure'] }); } private logStatistics(): void { - logger.info(this.logPrefix() + ' %j', this.statistics); + logger.info(`${this.logPrefix()}`, { + ...this.statistics, + statisticsData: Utils.JSONStringifyWithMapSupport(this.statistics.statisticsData), + }); } private startLogStatisticsInterval(): void { @@ -153,22 +162,19 @@ export default class PerformanceStatistics { this.logStatistics(); }, Configuration.getLogStatisticsInterval() * 1000); logger.info( - this.logPrefix() + - ' logged every ' + - Utils.formatDurationSeconds(Configuration.getLogStatisticsInterval()) + `${this.logPrefix()} logged every ${Utils.formatDurationSeconds( + Configuration.getLogStatisticsInterval() + )}` ); } else { logger.info( - this.logPrefix() + - ' log interval is set to ' + - Configuration.getLogStatisticsInterval().toString() + - '. Not logging statistics' + `${this.logPrefix()} log interval is set to ${Configuration.getLogStatisticsInterval().toString()}. Not logging statistics` ); } } private median(dataSet: number[]): number { - if (Array.isArray(dataSet) && dataSet.length === 1) { + if (Array.isArray(dataSet) === true && dataSet.length === 1) { return dataSet[0]; } const sortedDataSet = dataSet.slice().sort((a, b) => a - b); @@ -216,12 +222,7 @@ export default class PerformanceStatistics { } private addPerformanceEntryToStatistics(entry: PerformanceEntry): void { - let entryName = entry.name; - // Rename entry name - const MAP_NAME: Record = {}; - if (MAP_NAME[entryName]) { - entryName = MAP_NAME[entryName]; - } + const entryName = entry.name; // Initialize command statistics if (!this.statistics.statisticsData.has(entryName)) { this.statistics.statisticsData.set(entryName, {}); @@ -252,7 +253,7 @@ export default class PerformanceStatistics { this.statistics.statisticsData.get(entryName).avgTimeMeasurement = this.statistics.statisticsData.get(entryName).totalTimeMeasurement / this.statistics.statisticsData.get(entryName).countTimeMeasurement; - Array.isArray(this.statistics.statisticsData.get(entryName).timeMeasurementSeries) + Array.isArray(this.statistics.statisticsData.get(entryName).timeMeasurementSeries) === true ? this.statistics.statisticsData .get(entryName) .timeMeasurementSeries.push({ timestamp: entry.startTime, value: entry.duration }) @@ -279,10 +280,9 @@ export default class PerformanceStatistics { ) ); if (Configuration.getPerformanceStorage().enabled) { - parentPort.postMessage({ - id: ChargingStationWorkerMessageEvents.PERFORMANCE_STATISTICS, - data: this.statistics, - }); + parentPort.postMessage( + MessageChannelUtils.buildPerformanceStatisticsMessage(this.statistics) + ); } }