X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FStatistics.ts;h=a7dfaaa7e4e2e0cd612bd643a3d7101345564a2e;hb=a410f7c20845e369236b9a10d29df8d5680cd8b6;hp=f2bfee4ffc53208a9cf531ab7bb3be892403b01b;hpb=e118beaab2fe63c663d5a969085c682ca05b6554;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Statistics.ts b/src/utils/Statistics.ts index f2bfee4f..a7dfaaa7 100644 --- a/src/utils/Statistics.ts +++ b/src/utils/Statistics.ts @@ -1,4 +1,6 @@ -import CommandStatisticsData from '../types/CommandStatisticsData'; +import CommandStatistics, { CommandStatisticsData, PerfEntry } from '../types/CommandStatistics'; + +import CircularArray from './CircularArray'; import Configuration from './Configuration'; import Constants from './Constants'; import { PerformanceEntry } from 'perf_hooks'; @@ -8,12 +10,10 @@ import logger from './Logger'; export default class Statistics { private static instance: Statistics; private _objName: string; - private _commandsStatistics: { - [command: string]: CommandStatisticsData - }; + private _commandsStatistics: CommandStatistics; private constructor() { - this._commandsStatistics = {}; + this._commandsStatistics = {} as CommandStatistics; } set objName(objName: string) { @@ -67,48 +67,68 @@ export default class Statistics { } } - addPerformanceTimer(command: string, duration: number): void { - // Map to proper command name - const MAPCOMMAND = { - sendMeterValues: 'MeterValues', - startTransaction: 'StartTransaction', - stopTransaction: 'StopTransaction', - }; - if (MAPCOMMAND[command]) { - command = MAPCOMMAND[command] as string; - } - // Initialize command statistics - if (!this._commandsStatistics[command]) { - this._commandsStatistics[command] = {} as CommandStatisticsData; - } - // Update current statistics timers - this._commandsStatistics[command].countTime = this._commandsStatistics[command].countTime ? this._commandsStatistics[command].countTime + 1 : 1; - this._commandsStatistics[command].minTime = this._commandsStatistics[command].minTime ? (this._commandsStatistics[command].minTime > duration ? duration : this._commandsStatistics[command].minTime) : duration; - this._commandsStatistics[command].maxTime = this._commandsStatistics[command].maxTime ? (this._commandsStatistics[command].maxTime < duration ? duration : this._commandsStatistics[command].maxTime) : duration; - this._commandsStatistics[command].totalTime = this._commandsStatistics[command].totalTime ? this._commandsStatistics[command].totalTime + duration : duration; - this._commandsStatistics[command].avgTime = this._commandsStatistics[command].totalTime / this._commandsStatistics[command].countTime; - } - logPerformance(entry: PerformanceEntry, className: string): void { this.addPerformanceTimer(entry.name, entry.duration); - logger.info(`${this._logPrefix()} class->${className}, method->${entry.name}, duration->${entry.duration}`); + const perfEntry: PerfEntry = {} as PerfEntry; + perfEntry.name = entry.name; + perfEntry.entryType = entry.entryType; + perfEntry.startTime = entry.startTime; + perfEntry.duration = entry.duration; + logger.info(`${this._logPrefix()} object ${className} method(s) performance entry: %j`, perfEntry); + } + + start(): void { + this._displayInterval(); } - _display(): void { + private _display(): void { logger.info(this._logPrefix() + ' %j', this._commandsStatistics); } - _displayInterval(): void { + private _displayInterval(): void { if (Configuration.getStatisticsDisplayInterval() > 0) { setInterval(() => { this._display(); }, Configuration.getStatisticsDisplayInterval() * 1000); - logger.info(this._logPrefix() + ' displayed every ' + Configuration.getStatisticsDisplayInterval().toString() + 's'); + logger.info(this._logPrefix() + ' displayed every ' + Utils.secondsToHHMMSS(Configuration.getStatisticsDisplayInterval())); } } - start(): void { - this._displayInterval(); + private median(dataSet: number[]): number { + if (Array.isArray(dataSet) && dataSet.length === 1) { + return dataSet[0]; + } + const sortedDataSet = dataSet.slice().sort(); + const middleIndex = Math.floor(sortedDataSet.length / 2); + if (sortedDataSet.length % 2) { + return sortedDataSet[middleIndex / 2]; + } + return (sortedDataSet[(middleIndex - 1)] + sortedDataSet[middleIndex]) / 2; + } + + private addPerformanceTimer(command: string, duration: number): void { + // Map to proper command name + const MAPCOMMAND = { + sendMeterValues: 'MeterValues', + startTransaction: 'StartTransaction', + stopTransaction: 'StopTransaction', + }; + if (MAPCOMMAND[command]) { + command = MAPCOMMAND[command] as string; + } + // Initialize command statistics + if (!this._commandsStatistics[command]) { + this._commandsStatistics[command] = {} as CommandStatisticsData; + } + // Update current statistics timers + this._commandsStatistics[command].countTimeMeasurement = this._commandsStatistics[command].countTimeMeasurement ? this._commandsStatistics[command].countTimeMeasurement + 1 : 1; + this._commandsStatistics[command].currentTimeMeasurement = duration; + this._commandsStatistics[command].minTimeMeasurement = this._commandsStatistics[command].minTimeMeasurement ? (this._commandsStatistics[command].minTimeMeasurement > duration ? duration : this._commandsStatistics[command].minTimeMeasurement) : duration; + this._commandsStatistics[command].maxTimeMeasurement = this._commandsStatistics[command].maxTimeMeasurement ? (this._commandsStatistics[command].maxTimeMeasurement < duration ? duration : this._commandsStatistics[command].maxTimeMeasurement) : duration; + this._commandsStatistics[command].totalTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement ? this._commandsStatistics[command].totalTimeMeasurement + duration : duration; + this._commandsStatistics[command].avgTimeMeasurement = this._commandsStatistics[command].totalTimeMeasurement / this._commandsStatistics[command].countTimeMeasurement; + Array.isArray(this._commandsStatistics[command].timeMeasurementSeries) ? this._commandsStatistics[command].timeMeasurementSeries.push(duration) : this._commandsStatistics[command].timeMeasurementSeries = [duration] as CircularArray; + this._commandsStatistics[command].medTimeMeasurement = this.median(this._commandsStatistics[command].timeMeasurementSeries); } private _logPrefix(): string {