From: Jérôme Benoit Date: Fri, 30 Apr 2021 18:54:37 +0000 (+0200) Subject: Make the worker start delay a tunable. X-Git-Tag: v1.0.1-0~46 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=322c9192eaa7142da1bf475cc2c6588ca72d922c;p=e-mobility-charging-stations-simulator.git Make the worker start delay a tunable. Signed-off-by: Jérôme Benoit --- diff --git a/package-lock.json b/package-lock.json index 95ce88e6..3706f3c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -298,6 +298,12 @@ "kuler": "^2.0.0" } }, + "@es-joy/jsdoccomment": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@es-joy/jsdoccomment/-/jsdoccomment-0.1.1.tgz", + "integrity": "sha512-6lIx5Pjc50D7VJU9lfRZ1twfIrIwQk+aeT9Ink2C07IUu/y9pxkIpDqmhY/VN3jAW42dA5z6ioOdyhOZZU1isw==", + "dev": true + }, "@eslint/eslintrc": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-0.4.0.tgz", @@ -4480,11 +4486,12 @@ } }, "eslint-plugin-jsdoc": { - "version": "32.3.3", - "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-32.3.3.tgz", - "integrity": "sha512-WxXohbMYlZvCt3r7MepwT++nTLsO4CPegWcm5toM4IGq3MBmYkG+Uf5yDa+n1MwPXLg+KbJqAsI19hmkVD7MPg==", + "version": "33.0.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-33.0.0.tgz", + "integrity": "sha512-bkopnnuDdT04abKWPfDdD6XcAp2yX6UDpDViyvIdYmxbZYbpHXCRzQzLqCTo+SzWSTS0KFWz/V3shmmMr+x4EA==", "dev": true, "requires": { + "@es-joy/jsdoccomment": "^0.1.1", "comment-parser": "1.1.5", "debug": "^4.3.1", "jsdoctypeparser": "^9.0.0", diff --git a/package.json b/package.json index e7c480ee..adca5292 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "cross-env": "^7.0.3", "eslint": "^7.25.0", "eslint-plugin-import": "^2.22.1", - "eslint-plugin-jsdoc": "^32.3.3", + "eslint-plugin-jsdoc": "^33.0.0", "mbt": "^1.1.1", "npm-check": "^5.9.2", "rollup": "^2.46.0", diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index ddf21f48..bb101ca7 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -78,11 +78,13 @@ export default class Bootstrap { private getWorkerImplementationInstance(): WorkerAbstract { if (!this.workerImplementationInstance) { - this.workerImplementationInstance = WorkerFactory.getWorkerImplementation(this.workerScript, Configuration.getWorkerProcess(), { - poolMaxSize: Configuration.getWorkerPoolMaxSize(), - poolMinSize: Configuration.getWorkerPoolMinSize(), - elementsPerWorker: Configuration.getChargingStationsPerWorker() - }); + this.workerImplementationInstance = WorkerFactory.getWorkerImplementation(this.workerScript, Configuration.getWorkerProcess(), + { + startDelay: Configuration.getWorkerStartDelay(), + poolMaxSize: Configuration.getWorkerPoolMaxSize(), + poolMinSize: Configuration.getWorkerPoolMinSize(), + elementsPerWorker: Configuration.getChargingStationsPerWorker() + }); } return this.workerImplementationInstance; } diff --git a/src/types/ConfigurationData.ts b/src/types/ConfigurationData.ts index 125ecef6..9c7c098e 100644 --- a/src/types/ConfigurationData.ts +++ b/src/types/ConfigurationData.ts @@ -13,6 +13,7 @@ export default interface ConfigurationData { autoReconnectMaxRetries?: number; distributeStationsToTenantsEqually?: boolean; workerProcess?: WorkerProcessType; + workerStartDelay?: number; workerPoolMinSize?: number; workerPoolMaxSize?: number; chargingStationsPerWorker?: number; diff --git a/src/types/Worker.ts b/src/types/Worker.ts index bc297268..dde976ae 100644 --- a/src/types/Worker.ts +++ b/src/types/Worker.ts @@ -7,12 +7,14 @@ export enum WorkerProcessType { } export interface WorkerOptions { + startDelay?: number; poolMaxSize?: number; poolMinSize?: number; elementsPerWorker?: number; } -export interface WorkerData { } +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface WorkerData {} export interface StationWorkerData extends WorkerData { index: number; diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 552fab21..4bc1853d 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,5 +1,6 @@ import ConfigurationData, { StationTemplateURL } from '../types/ConfigurationData'; +import Constants from './Constants'; import { WorkerProcessType } from '../types/Worker'; import fs from 'fs'; import path from 'path'; @@ -51,6 +52,10 @@ export default class Configuration { return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') ? Configuration.getConfig().workerProcess : WorkerProcessType.WORKER_SET; } + static getWorkerStartDelay(): number { + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay') ? Configuration.getConfig().workerStartDelay : Constants.WORKER_START_DELAY; + } + static getWorkerPoolMinSize(): number { return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize') ? Configuration.getConfig().workerPoolMinSize : 4; } diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index 1959ed93..6a1a7cfe 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -31,6 +31,6 @@ export default class Constants { static readonly TRANSACTION_DEFAULT_TAGID = '00000000'; - static readonly START_WORKER_DELAY = 500; + static readonly WORKER_START_DELAY = 500; static readonly WORKER_POOL_MAX_INACTIVE_TIME = 60000; } diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 9f16af73..772bf1b4 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -1,7 +1,9 @@ +import Constants from '../utils/Constants'; import { WorkerData } from '../types/Worker'; export default abstract class WorkerAbstract { - protected workerScript: string; + protected readonly workerScript: string; + protected readonly workerStartDelay: number; public abstract size: number; public abstract maxElementsPerWorker: number; @@ -9,9 +11,11 @@ export default abstract class WorkerAbstract { * `WorkerAbstract` constructor. * * @param {string} workerScript + * @param {number} workerStartDelay */ - constructor(workerScript: string) { + constructor(workerScript: string, workerStartDelay: number = Constants.WORKER_START_DELAY) { this.workerScript = workerScript; + this.workerStartDelay = workerStartDelay; } public abstract start(): Promise; diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index a9aa56f0..cf22c0a8 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -1,6 +1,5 @@ import { DynamicThreadPool, PoolOptions } from 'poolifier'; -import Constants from '../utils/Constants'; import Utils from '../utils/Utils'; import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; @@ -15,9 +14,10 @@ export default class WorkerDynamicPool extends WorkerAbstract { * @param {string} workerScript * @param {number} min * @param {number} max + * @param {number} workerStartDelay */ - constructor(workerScript: string, min: number, max: number,) { - super(workerScript); + constructor(workerScript: string, min: number, max: number, workerStartDelay?: number) { + super(workerScript, workerStartDelay); this.pool = DynamicPool.getInstance(min, max, this.workerScript); } @@ -56,7 +56,7 @@ export default class WorkerDynamicPool extends WorkerAbstract { public async addElement(elementData: T): Promise { await this.pool.execute(elementData); // Start worker sequentially to optimize memory at startup - await Utils.sleep(Constants.START_WORKER_DELAY); + await Utils.sleep(this.workerStartDelay); } } diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 242121fb..34369e18 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -1,6 +1,6 @@ import { WorkerOptions, WorkerProcessType } from '../types/Worker'; -import Utils from '../utils/Utils'; +import Constants from '../utils/Constants'; import WorkerAbstract from './WorkerAbstract'; import WorkerDynamicPool from './WorkerDynamicPool'; import WorkerSet from './WorkerSet'; @@ -12,20 +12,19 @@ export default class WorkerFactory { if (!isMainThread) { throw new Error('Trying to get a worker implementation outside the main thread'); } - if (Utils.isUndefined(options)) { - options = {} as WorkerOptions; - } + options = options ?? {} as WorkerOptions; + options.startDelay = options.startDelay ?? Constants.WORKER_START_DELAY; switch (workerProcessType) { case WorkerProcessType.WORKER_SET: options.elementsPerWorker = options.elementsPerWorker ?? 1; - return new WorkerSet(workerScript, options.elementsPerWorker); + return new WorkerSet(workerScript, options.elementsPerWorker, options.startDelay); case WorkerProcessType.STATIC_POOL: options.poolMaxSize = options.poolMaxSize ?? 16; - return new WorkerStaticPool(workerScript, options.poolMaxSize); + return new WorkerStaticPool(workerScript, options.poolMaxSize, options.startDelay); case WorkerProcessType.DYNAMIC_POOL: options.poolMinSize = options.poolMinSize ?? 4; options.poolMaxSize = options.poolMaxSize ?? 16; - return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize); + return new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, options.startDelay); default: return null; } diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index bf8b7021..0e6855f1 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -1,6 +1,5 @@ import { WorkerEvents, WorkerSetElement } from '../types/Worker'; -import Constants from '../utils/Constants'; import Utils from '../utils/Utils'; import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; @@ -14,9 +13,10 @@ export default class WorkerSet extends WorkerAbstract { * * @param {string} workerScript * @param {number} maxElementsPerWorker + * @param {number} workerStartDelay */ - constructor(workerScript: string, maxElementsPerWorker = 1) { - super(workerScript); + constructor(workerScript: string, maxElementsPerWorker = 1, workerStartDelay?: number) { + super(workerScript, workerStartDelay); this.workerSet = new Set(); this.maxElementsPerWorker = maxElementsPerWorker; } @@ -38,7 +38,7 @@ export default class WorkerSet extends WorkerAbstract { if (this.getLastWorkerSetElement().numberOfWorkerElements >= this.maxElementsPerWorker) { this.startWorker(); // Start worker sequentially to optimize memory at startup - await Utils.sleep(Constants.START_WORKER_DELAY); + await Utils.sleep(this.workerStartDelay); } this.getLastWorker().postMessage({ id: WorkerEvents.START_WORKER_ELEMENT, workerData: elementData }); this.getLastWorkerSetElement().numberOfWorkerElements++; @@ -52,7 +52,7 @@ export default class WorkerSet extends WorkerAbstract { public async start(): Promise { this.startWorker(); // Start worker sequentially to optimize memory at startup - await Utils.sleep(Constants.START_WORKER_DELAY); + await Utils.sleep(this.workerStartDelay); } /** diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 64217629..c78768e0 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -1,6 +1,5 @@ import { FixedThreadPool, PoolOptions } from 'poolifier'; -import Constants from '../utils/Constants'; import Utils from '../utils/Utils'; import { Worker } from 'worker_threads'; import WorkerAbstract from './WorkerAbstract'; @@ -14,9 +13,10 @@ export default class WorkerStaticPool extends WorkerAbstract { * * @param {string} workerScript * @param {number} numberOfThreads + * @param {number} startWorkerDelay */ - constructor(workerScript: string, numberOfThreads: number) { - super(workerScript); + constructor(workerScript: string, numberOfThreads: number, startWorkerDelay?: number) { + super(workerScript, startWorkerDelay); this.pool = StaticPool.getInstance(numberOfThreads, this.workerScript); } @@ -54,7 +54,7 @@ export default class WorkerStaticPool extends WorkerAbstract { public async addElement(elementData: T): Promise { await this.pool.execute(elementData); // Start worker sequentially to optimize memory at startup - await Utils.sleep(Constants.START_WORKER_DELAY); + await Utils.sleep(this.workerStartDelay); } }