From: Jérôme Benoit Date: Sun, 3 Sep 2023 14:02:10 +0000 (+0200) Subject: perf: optimize min/max implementation X-Git-Tag: v1.2.21~35 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5adf6ca45d6644c0969ca1d60ac1b9dc0b7810f6;p=e-mobility-charging-stations-simulator.git perf: optimize min/max implementation Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index a8da873b..f8415392 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -144,6 +144,7 @@ import { isUndefined, logPrefix, logger, + min, once, roundTo, secureRandom, @@ -380,7 +381,7 @@ export class ChargingStation { const connectorMaximumPower = this.getMaximumPower() / this.powerDivider; const connectorChargingProfilesPowerLimit = getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId); - return Math.min( + return min( isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower, isNaN(connectorAmperageLimitationPowerLimit!) ? Infinity diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index b4a7e324..7c459809 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -40,6 +40,7 @@ import { isNotEmptyString, logPrefix, logger, + min, } from '../../utils'; export class OCPPServiceUtils { @@ -421,7 +422,7 @@ export class OCPPServiceUtils { const parsedInt = parseInt(value); const numberValue = isNaN(parsedInt) ? Infinity : parsedInt; return options?.limitationEnabled - ? Math.min(numberValue * options.unitMultiplier!, limit) + ? min(numberValue * options.unitMultiplier!, limit) : numberValue * options.unitMultiplier!; } diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 64216db2..7ff27723 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -28,7 +28,9 @@ import { generateUUID, logPrefix, logger, + max, median, + min, nthPercentile, stdDeviation, } from '../utils'; @@ -220,11 +222,11 @@ export class PerformanceStatistics { this.statistics.statisticsData.get(entryName)!.timeMeasurementCount = (this.statistics.statisticsData.get(entryName)?.timeMeasurementCount ?? 0) + 1; this.statistics.statisticsData.get(entryName)!.currentTimeMeasurement = entry.duration; - this.statistics.statisticsData.get(entryName)!.minTimeMeasurement = Math.min( + this.statistics.statisticsData.get(entryName)!.minTimeMeasurement = min( entry.duration, this.statistics.statisticsData.get(entryName)?.minTimeMeasurement ?? Infinity, ); - this.statistics.statisticsData.get(entryName)!.maxTimeMeasurement = Math.max( + this.statistics.statisticsData.get(entryName)!.maxTimeMeasurement = max( entry.duration, this.statistics.statisticsData.get(entryName)?.maxTimeMeasurement ?? -Infinity, ); diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 58e9094c..a2cb233a 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -397,3 +397,9 @@ export const once = ( return result; }; }; + +export const min = (...args: number[]): number => + args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity); + +export const max = (...args: number[]): number => + args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity); diff --git a/src/utils/index.ts b/src/utils/index.ts index f8fbe0e1..f3feeb66 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -49,6 +49,8 @@ export { isUndefined, isValidTime, logPrefix, + max, + min, once, promiseWithTimeout, roundTo, diff --git a/test/utils/Utils.test.ts b/test/utils/Utils.test.ts index 4a982775..ab07ec81 100644 --- a/test/utils/Utils.test.ts +++ b/test/utils/Utils.test.ts @@ -26,6 +26,8 @@ import { isObject, isUndefined, isValidTime, + max, + min, once, roundTo, secureRandom, @@ -189,11 +191,11 @@ describe('Utils test suite', () => { randomInteger = getRandomInteger(2, 1); expect(randomInteger).toBeGreaterThanOrEqual(1); expect(randomInteger).toBeLessThanOrEqual(2); - const max = 2.2, - min = 1.1; - randomInteger = getRandomInteger(max, min); - expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(min)); - expect(randomInteger).toBeLessThanOrEqual(Math.floor(max)); + const maximum = 2.2, + minimum = 1.1; + randomInteger = getRandomInteger(maximum, minimum); + expect(randomInteger).toBeLessThanOrEqual(Math.floor(maximum)); + expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(minimum)); }); it('Verify roundTo()', () => { @@ -447,4 +449,18 @@ describe('Utils test suite', () => { expect(called).toBe(1); expect(result3).toBe(1); }); + + it('Verify min()', () => { + expect(min(0, 1)).toBe(0); + expect(min(1, 0)).toBe(0); + expect(min(0, -1)).toBe(-1); + expect(min(-1, 0)).toBe(-1); + }); + + it('Verify max()', () => { + expect(max(0, 1)).toBe(1); + expect(max(1, 0)).toBe(1); + expect(max(0, -1)).toBe(0); + expect(max(-1, 0)).toBe(0); + }); });