From edfb206c5a6a309cbe5a8e6bf3c3f52ab57d96fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 20 Nov 2020 01:25:55 +0100 Subject: [PATCH] Use a circular array to store time measurements. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/types/CommandStatistics.ts | 3 ++- src/utils/CircularArray.ts | 24 ++++++++++++++++++++++++ src/utils/Constants.ts | 2 ++ src/utils/Statistics.ts | 3 ++- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/utils/CircularArray.ts 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); } -- 2.34.1