From d1c99c59787e98b3954760fbc208a5dfbc3d8f57 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 2 Feb 2023 20:17:03 +0100 Subject: [PATCH] fix(simulator): calculate LRU cache capacity dynamically at startup to avoid heavy evictions load MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Properly calculate the number of charging stations and templates in use Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 49 ++++++++++++++------------ src/charging-station/SharedLRUCache.ts | 6 +++- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 88b4b4c4..66a42419 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -38,11 +38,11 @@ enum exitCodes { export class Bootstrap { private static instance: Bootstrap | null = null; + public numberOfChargingStations!: number; + public numberOfChargingStationTemplates!: number; private workerImplementation: WorkerAbstract | null; private readonly uiServer!: AbstractUIServer | null; private readonly storage!: Storage; - private numberOfChargingStationTemplates!: number | undefined; - private numberOfChargingStations!: number; private numberOfStartedChargingStations!: number; private readonly version: string = version; private started: boolean; @@ -86,29 +86,21 @@ export class Bootstrap { await this.workerImplementation?.start(); this.uiServer?.start(); const stationTemplateUrls = Configuration.getStationTemplateUrls(); - this.numberOfChargingStationTemplates = stationTemplateUrls?.length; // Start ChargingStation object in worker thread - if (!Utils.isEmptyArray(stationTemplateUrls)) { - for (const stationTemplateUrl of stationTemplateUrls) { - try { - const nbStations = stationTemplateUrl.numberOfStations ?? 0; - for (let index = 1; index <= nbStations; index++) { - await this.startChargingStation(index, stationTemplateUrl); - } - } catch (error) { - console.error( - chalk.red( - `Error at starting charging station with template file ${stationTemplateUrl.file}: ` - ), - error - ); + for (const stationTemplateUrl of stationTemplateUrls) { + try { + const nbStations = stationTemplateUrl.numberOfStations ?? 0; + for (let index = 1; index <= nbStations; index++) { + await this.startChargingStation(index, stationTemplateUrl); } + } catch (error) { + console.error( + chalk.red( + `Error at starting charging station with template file ${stationTemplateUrl.file}: ` + ), + error + ); } - } else { - console.warn( - chalk.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting") - ); - process.exit(exitCodes.missingChargingStationsConfiguration); } if (this.numberOfChargingStations === 0) { console.warn( @@ -256,6 +248,18 @@ export class Bootstrap { private initialize() { this.numberOfChargingStationTemplates = 0; this.numberOfChargingStations = 0; + const stationTemplateUrls = Configuration.getStationTemplateUrls(); + if (!Utils.isEmptyArray(stationTemplateUrls)) { + this.numberOfChargingStationTemplates = stationTemplateUrls?.length; + stationTemplateUrls.forEach((stationTemplateUrl) => { + this.numberOfChargingStations += stationTemplateUrl.numberOfStations ?? 0; + }); + } else { + console.warn( + chalk.yellow("'stationTemplateUrls' not defined or empty in configuration, exiting") + ); + process.exit(exitCodes.missingChargingStationsConfiguration); + } this.numberOfStartedChargingStations = 0; this.initializeWorkerImplementation(); } @@ -286,7 +290,6 @@ export class Bootstrap { ), }; await this.workerImplementation?.addElement(workerData); - ++this.numberOfChargingStations; } private logPrefix = (): string => { diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index e235c51d..db5269f5 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,5 +1,6 @@ import LRUCache from 'mnemonist/lru-map-with-delete'; +import { Bootstrap } from '../internal'; import type { ChargingStationConfiguration } from '../types/ChargingStationConfiguration'; import type { ChargingStationTemplate } from '../types/ChargingStationTemplate'; import Utils from '../utils/Utils'; @@ -16,7 +17,10 @@ export default class SharedLRUCache { private readonly lruCache: LRUCache; private constructor() { - this.lruCache = new LRUCache(1000); + this.lruCache = new LRUCache( + Bootstrap.getInstance().numberOfChargingStations + + Bootstrap.getInstance().numberOfChargingStationTemplates + ); } public static getInstance(): SharedLRUCache { -- 2.34.1