From: Jérôme Benoit Date: Fri, 20 Nov 2020 00:25:55 +0000 (+0100) Subject: Use a circular array to store time measurements. X-Git-Tag: v1.0.1-0~191 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=edfb206c5a6a309cbe5a8e6bf3c3f52ab57d96fc;p=e-mobility-charging-stations-simulator.git Use a circular array to store time measurements. Signed-off-by: Jérôme Benoit --- diff --git a/src/types/CommandStatistics.ts b/src/types/CommandStatistics.ts index 9c1896f4..b8740039 100644 --- a/src/types/CommandStatistics.ts +++ b/src/types/CommandStatistics.ts @@ -1,3 +1,4 @@ +import CircularArray from '../utils/CircularArray'; import { EntryType } from 'perf_hooks'; export interface PerfEntry { @@ -12,7 +13,7 @@ export interface CommandStatisticsData { countResponse: number; countError: number; countTimeMeasurement: number; - timeMeasurementSeries: number[]; + timeMeasurementSeries: CircularArray; currentTimeMeasurement: number; minTimeMeasurement: number; maxTimeMeasurement: number; diff --git a/src/utils/CircularArray.ts b/src/utils/CircularArray.ts new file mode 100644 index 00000000..5dab1e2c --- /dev/null +++ b/src/utils/CircularArray.ts @@ -0,0 +1,24 @@ +import Constants from './Constants'; + +export default class CircularArray extends Array { + public size: number; + + constructor(size: number = Constants.MAXIMUM_MEASUREMENTS_NUMBER) { + super(); + this.size = size; + } + + push(...items: T[]): number { + while (this.length > this.size) { + this.shift(); + } + return super.push(...items); + } + + unshift(...items: T[]): number { + while (this.length > this.size) { + this.pop(); + } + return super.unshift(...items); + } +} diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index eab54ea5..cf5d9bfa 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -50,5 +50,7 @@ export default class Constants { static readonly CHARGING_STATION_DEFAULT_RESET_TIME = 60000; // Ms static readonly CHARGING_STATION_ATG_WAIT_TIME = 2000; // Ms + static readonly MAXIMUM_MEASUREMENTS_NUMBER = 5000; + static readonly TRANSACTION_DEFAULT_IDTAG = '00000000'; } diff --git a/src/utils/Statistics.ts b/src/utils/Statistics.ts index 3fa4c783..d687368c 100644 --- a/src/utils/Statistics.ts +++ b/src/utils/Statistics.ts @@ -1,5 +1,6 @@ import CommandStatistics, { CommandStatisticsData, PerfEntry } from '../types/CommandStatistics'; +import CircularArray from './CircularArray'; import Configuration from './Configuration'; import Constants from './Constants'; import { PerformanceEntry } from 'perf_hooks'; @@ -126,7 +127,7 @@ export default class Statistics { 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]; + 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); }