From 8603c1caae4c616b0c1f8292c7f70bf3b0124175 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 12 Jul 2023 15:00:28 +0200 Subject: [PATCH] feat: add elementsPerWorker automatic calculation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- README.md | 116 +++++++++++++++--------------- src/charging-station/Bootstrap.ts | 12 +++- src/types/ConfigurationData.ts | 2 +- src/utils/Configuration.ts | 2 +- 4 files changed, 70 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 62110317..97146c2c 100644 --- a/README.md +++ b/README.md @@ -99,11 +99,11 @@ But the modifications to test have to be done to the files in the build target d **src/assets/config.json**: | Key | Value(s) | Default Value | Value type | Description | -| -------------------------- | -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| -------------------------- | -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | supervisionUrls | | [] | string \| string[] | string or strings array containing global connection URIs to OCPP-J servers | | supervisionUrlDistribution | round-robin/random/charging-station-affinity | charging-station-affinity | string | supervision urls distribution policy to simulated charging stations | | log | | {
"enabled": true,
"file": "logs/combined.log",
"errorFile": "logs/error.log",
"statisticsInterval": 60,
"level": "info",
"console": false,
"format": "simple",
"rotate": true
} | {
enabled: boolean;
file: string;
errorFile: string;
statisticsInterval: number;
level: string;
console: boolean;
format: string;
rotate: boolean;
maxFiles: string \| number;
maxSize: string \| number;
} | Log configuration section:
- _enabled_: enable logging
- _file_: log file relative path
- _errorFile_: error log file relative path
- _statisticsInterval_: seconds between charging stations statistics output in the logs
- _level_: emerg/alert/crit/error/warning/notice/info/debug [winston](https://github.com/winstonjs/winston) logging level
- _console_: output logs on the console
- _format_: [winston](https://github.com/winstonjs/winston) log format
- _rotate_: enable daily log files rotation
- _maxFiles_: maximum number of log files: https://github.com/winstonjs/winston-daily-rotate-file#options
- _maxSize_: maximum size of log files in bytes, or units of kb, mb, and gb: https://github.com/winstonjs/winston-daily-rotate-file#options | -| worker | | {
"processType": "workerSet",
"startDelay": 500,
"elementStartDelay": 0,
"elementsPerWorker": 1,
"poolMinSize": 4,
"poolMaxSize": 16,
"poolStrategy": "ROUND_ROBIN"
} | {
processType: WorkerProcessType;
startDelay: number;
elementStartDelay: number;
elementsPerWorker: number;
poolMinSize: number;
poolMaxSize: number;
poolStrategy: WorkerChoiceStrategy;
} | Worker configuration section:
- _processType_: worker threads process type (`workerSet`/`staticPool`/`dynamicPool`)
- _startDelay_: milliseconds to wait at worker threads startup (only for `workerSet` worker threads process type)
- _elementStartDelay_: milliseconds to wait at charging station startup
- _elementsPerWorker_: number of charging stations per worker threads for the `workerSet` process type
- _poolMinSize_: worker threads pool minimum number of threads
- _poolMaxSize_: worker threads pool maximum number of threads
- _poolStrategy_: worker threads pool [poolifier](https://github.com/poolifier/poolifier) worker choice strategy | +| worker | | {
"processType": "workerSet",
"startDelay": 500,
"elementStartDelay": 0,
"elementsPerWorker": 'auto',
"poolMinSize": 4,
"poolMaxSize": 16,
"poolStrategy": "ROUND_ROBIN"
} | {
processType: WorkerProcessType;
startDelay: number;
elementStartDelay: number;
elementsPerWorker: number | string;
poolMinSize: number;
poolMaxSize: number;
poolStrategy: WorkerChoiceStrategy;
} | Worker configuration section:
- _processType_: worker threads process type (`workerSet`/`staticPool`/`dynamicPool`)
- _startDelay_: milliseconds to wait at worker threads startup (only for `workerSet` worker threads process type)
- _elementStartDelay_: milliseconds to wait at charging station startup
- _elementsPerWorker_: number of charging stations per worker threads for the `workerSet` process type (`auto` means (number of stations) / (number of CPUs) if (number of stations) > (number of CPUs), otherwise 1)
- _poolMinSize_: worker threads pool minimum number of threads
- _poolMaxSize_: worker threads pool maximum number of threads
- _poolStrategy_: worker threads pool [poolifier](https://github.com/poolifier/poolifier) worker choice strategy | | uiServer | | {
"enabled": false,
"type": "ws",
"options": {
"host": "localhost",
"port": 8080
}
} | {
enabled: boolean;
type: ApplicationProtocol;
options: ServerOptions;
authentication: {
enabled: boolean;
type: AuthenticationType;
username: string;
password: string;
}
} | UI server configuration section | | performanceStorage | | {
"enabled": false,
"type": "jsonfile",
"file:///performanceRecords.json"
} | {
enabled: boolean;
type: string;
URI: string;
}
where type can be 'jsonfile' or 'mongodb' | performance storage configuration section | | stationTemplateUrls | | {}[] | {
file: string;
numberOfStations: number;
}[] | array of charging station configuration templates URIs configuration section (charging station configuration template file name and number of stations) | @@ -123,62 +123,62 @@ But the modifications to test have to be done to the files in the build target d **src/assets/station-templates/\.json**: -| Key | Value(s) | Default Value | Value type | Description | -| ---------------------------------------------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| supervisionUrls | | [] | string \| string[] | string or strings array containing connection URIs to OCPP-J servers | -| supervisionUser | | undefined | string | basic HTTP authentication user to OCPP-J server | -| supervisionPassword | | undefined | string | basic HTTP authentication password to OCPP-J server | -| supervisionUrlOcppConfiguration | true/false | false | boolean | allow supervision URL configuration via a vendor OCPP parameter key | -| supervisionUrlOcppKey | | 'ConnectionUrl' | string | the vendor string that will be used as a vendor OCPP parameter key to set the supervision URL | -| ocppVersion | 1.6/2.0/2.0.1 | 1.6 | string | OCPP version | -| ocppProtocol | json | json | string | OCPP protocol | -| ocppStrictCompliance | true/false | true | boolean | strict adherence to the OCPP version and protocol specifications. OCPP commands PDU validation against [OCA](https://www.openchargealliance.org/) JSON schemas | -| ocppPersistentConfiguration | true/false | true | boolean | enable persistent OCPP parameters storage by charging stations 'hashId'. The persistency is ensured by the charging stations configuration files in [dist/assets/configurations](dist/assets/configurations) | -| stationInfoPersistentConfiguration | true/false | true | boolean | enable persistent station information and specifications storage by charging stations 'hashId'. The persistency is ensured by the charging stations configuration files in [dist/assets/configurations](dist/assets/configurations) | -| automaticTransactionGeneratorPersistentConfiguration | true/false | true | boolean | enable persistent automatic transaction generator configuration storage by charging stations 'hashId'. The persistency is ensured by the charging stations configuration files in [dist/assets/configurations](dist/assets/configurations) | -| wsOptions | | {} | ClientOptions & ClientRequestArgs | [ws](https://github.com/websockets/ws) and node.js [http](https://nodejs.org/api/http.html) clients options intersection | -| idTagsFile | | undefined | string | RFID tags list file relative to [src/assets](src/assets) path | -| baseName | | undefined | string | base name to build charging stations id | -| nameSuffix | | undefined | string | name suffix to build charging stations id | -| fixedName | true/false | false | boolean | use the baseName as the charging stations unique name | -| chargePointModel | | undefined | string | charging stations model | -| chargePointVendor | | undefined | string | charging stations vendor | -| chargePointSerialNumberPrefix | | undefined | string | charge point serial number prefix | -| chargeBoxSerialNumberPrefix | | undefined | string | charge box serial number prefix (deprecated in OCPP 1.6) | -| firmwareVersionPattern | | Semantic versionning regular expression: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | string | charging stations firmware version pattern | -| firmwareVersion | | undefined | string | charging stations firmware version | -| power | | | float \| float[] | charging stations maximum power value(s) | -| powerSharedByConnectors | true/false | false | boolean | charging stations power shared by its connectors | -| powerUnit | W/kW | W | string | charging stations power unit | -| currentOutType | AC/DC | AC | string | charging stations current out type | -| voltageOut | | AC:230/DC:400 | integer | charging stations voltage out | -| numberOfPhases | 0/1/3 | AC:3/DC:0 | integer | charging stations number of phase(s) | -| numberOfConnectors | | | integer \| integer[] | charging stations number of connector(s) | -| useConnectorId0 | true/false | true | boolean | use connector id 0 definition from the charging station configuration template | -| randomConnectors | true/false | false | boolean | randomize runtime connector id affectation from the connector id definition in charging station configuration template | -| resetTime | | 60 | integer | seconds to wait before the charging stations come back at reset | -| autoRegister | true/false | false | boolean | set charging stations as registered at boot notification for testing purpose | -| autoReconnectMaxRetries | | -1 (unlimited) | integer | connection retries to the OCPP-J server | -| reconnectExponentialDelay | true/false | false | boolean | connection delay retry to the OCPP-J server | -| registrationMaxRetries | | -1 (unlimited) | integer | charging stations boot notification retries | -| amperageLimitationOcppKey | | undefined | string | charging stations OCPP parameter key used to set the amperage limit, per phase for each connector on AC and global for DC | -| amperageLimitationUnit | A/cA/dA/mA | A | string | charging stations amperage limit unit | -| enableStatistics | true/false | false | boolean | enable charging stations statistics | -| mustAuthorizeAtRemoteStart | true/false | true | boolean | always send authorize at remote start transaction when AuthorizeRemoteTxRequests is enabled | -| beginEndMeterValues | true/false | false | boolean | enable Transaction.{Begin,End} MeterValues | -| outOfOrderEndMeterValues | true/false | false | boolean | send Transaction.End MeterValues out of order. Need to relax OCPP specifications strict compliance ('ocppStrictCompliance' parameter) | -| meteringPerTransaction | true/false | true | boolean | enable metering history on a per transaction basis | -| transactionDataMeterValues | true/false | false | boolean | enable transaction data MeterValues at stop transaction | -| mainVoltageMeterValues | true/false | true | boolean | include charging stations main voltage MeterValues on three phased charging stations | -| phaseLineToLineVoltageMeterValues | true/false | false | boolean | include charging stations line to line voltage MeterValues on three phased charging stations | -| customValueLimitationMeterValues | true/false | true | boolean | enable limitation on custom fluctuated value in MeterValues | -| firmwareUpgrade | | {
"versionUpgrade": {
"step": 1
},
"reset": true
} | {
versionUpgrade: {
patternGroup: number;
step: number;
};
reset: boolean;
failureStatus: 'DownloadFailed' \| 'InstallationFailed';
} | Configuration section for simulating firmware upgrade support. | -| commandsSupport | | {
"incomingCommands": {},
"outgoingCommands": {}
} | {
incomingCommands: Record;
outgoingCommands?: Record;
} | Configuration section for OCPP commands support. Empty section or subsections means all implemented OCPP commands are supported | -| messageTriggerSupport | | {} | Record | Configuration section for OCPP commands trigger support. Empty section means all implemented OCPP trigger commands are supported | -| Configuration | | | ChargingStationOcppConfiguration | charging stations OCPP parameters configuration section | -| AutomaticTransactionGenerator | | | AutomaticTransactionGeneratorConfiguration | charging stations ATG configuration section | -| Connectors | | | Record | charging stations connectors configuration section | -| Evses | | | Record | charging stations EVSEs configuration section | +| Key | Value(s) | Default Value | Value type | Description | +| ---------------------------------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| supervisionUrls | | [] | string \| string[] | string or strings array containing connection URIs to OCPP-J servers | +| supervisionUser | | undefined | string | basic HTTP authentication user to OCPP-J server | +| supervisionPassword | | undefined | string | basic HTTP authentication password to OCPP-J server | +| supervisionUrlOcppConfiguration | true/false | false | boolean | allow supervision URL configuration via a vendor OCPP parameter key | +| supervisionUrlOcppKey | | 'ConnectionUrl' | string | the vendor string that will be used as a vendor OCPP parameter key to set the supervision URL | +| ocppVersion | 1.6/2.0/2.0.1 | 1.6 | string | OCPP version | +| ocppProtocol | json | json | string | OCPP protocol | +| ocppStrictCompliance | true/false | true | boolean | strict adherence to the OCPP version and protocol specifications. OCPP commands PDU validation against [OCA](https://www.openchargealliance.org/) JSON schemas | +| ocppPersistentConfiguration | true/false | true | boolean | enable persistent OCPP parameters storage by charging stations 'hashId'. The persistency is ensured by the charging stations configuration files in [dist/assets/configurations](dist/assets/configurations) | +| stationInfoPersistentConfiguration | true/false | true | boolean | enable persistent station information and specifications storage by charging stations 'hashId'. The persistency is ensured by the charging stations configuration files in [dist/assets/configurations](dist/assets/configurations) | +| automaticTransactionGeneratorPersistentConfiguration | true/false | true | boolean | enable persistent automatic transaction generator configuration storage by charging stations 'hashId'. The persistency is ensured by the charging stations configuration files in [dist/assets/configurations](dist/assets/configurations) | +| wsOptions | | {} | ClientOptions & ClientRequestArgs | [ws](https://github.com/websockets/ws) and node.js [http](https://nodejs.org/api/http.html) clients options intersection | +| idTagsFile | | undefined | string | RFID tags list file relative to [src/assets](src/assets) path | +| baseName | | undefined | string | base name to build charging stations id | +| nameSuffix | | undefined | string | name suffix to build charging stations id | +| fixedName | true/false | false | boolean | use the baseName as the charging stations unique name | +| chargePointModel | | undefined | string | charging stations model | +| chargePointVendor | | undefined | string | charging stations vendor | +| chargePointSerialNumberPrefix | | undefined | string | charge point serial number prefix | +| chargeBoxSerialNumberPrefix | | undefined | string | charge box serial number prefix (deprecated in OCPP 1.6) | +| firmwareVersionPattern | | Semantic versioning regular expression: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | string | charging stations firmware version pattern | +| firmwareVersion | | undefined | string | charging stations firmware version | +| power | | | float \| float[] | charging stations maximum power value(s) | +| powerSharedByConnectors | true/false | false | boolean | charging stations power shared by its connectors | +| powerUnit | W/kW | W | string | charging stations power unit | +| currentOutType | AC/DC | AC | string | charging stations current out type | +| voltageOut | | AC:230/DC:400 | integer | charging stations voltage out | +| numberOfPhases | 0/1/3 | AC:3/DC:0 | integer | charging stations number of phase(s) | +| numberOfConnectors | | | integer \| integer[] | charging stations number of connector(s) | +| useConnectorId0 | true/false | true | boolean | use connector id 0 definition from the charging station configuration template | +| randomConnectors | true/false | false | boolean | randomize runtime connector id affectation from the connector id definition in charging station configuration template | +| resetTime | | 60 | integer | seconds to wait before the charging stations come back at reset | +| autoRegister | true/false | false | boolean | set charging stations as registered at boot notification for testing purpose | +| autoReconnectMaxRetries | | -1 (unlimited) | integer | connection retries to the OCPP-J server | +| reconnectExponentialDelay | true/false | false | boolean | connection delay retry to the OCPP-J server | +| registrationMaxRetries | | -1 (unlimited) | integer | charging stations boot notification retries | +| amperageLimitationOcppKey | | undefined | string | charging stations OCPP parameter key used to set the amperage limit, per phase for each connector on AC and global for DC | +| amperageLimitationUnit | A/cA/dA/mA | A | string | charging stations amperage limit unit | +| enableStatistics | true/false | false | boolean | enable charging stations statistics | +| mustAuthorizeAtRemoteStart | true/false | true | boolean | always send authorize at remote start transaction when AuthorizeRemoteTxRequests is enabled | +| beginEndMeterValues | true/false | false | boolean | enable Transaction.{Begin,End} MeterValues | +| outOfOrderEndMeterValues | true/false | false | boolean | send Transaction.End MeterValues out of order. Need to relax OCPP specifications strict compliance ('ocppStrictCompliance' parameter) | +| meteringPerTransaction | true/false | true | boolean | enable metering history on a per transaction basis | +| transactionDataMeterValues | true/false | false | boolean | enable transaction data MeterValues at stop transaction | +| mainVoltageMeterValues | true/false | true | boolean | include charging stations main voltage MeterValues on three phased charging stations | +| phaseLineToLineVoltageMeterValues | true/false | false | boolean | include charging stations line to line voltage MeterValues on three phased charging stations | +| customValueLimitationMeterValues | true/false | true | boolean | enable limitation on custom fluctuated value in MeterValues | +| firmwareUpgrade | | {
"versionUpgrade": {
"step": 1
},
"reset": true
} | {
versionUpgrade: {
patternGroup: number;
step: number;
};
reset: boolean;
failureStatus: 'DownloadFailed' \| 'InstallationFailed';
} | Configuration section for simulating firmware upgrade support. | +| commandsSupport | | {
"incomingCommands": {},
"outgoingCommands": {}
} | {
incomingCommands: Record;
outgoingCommands?: Record;
} | Configuration section for OCPP commands support. Empty section or subsections means all implemented OCPP commands are supported | +| messageTriggerSupport | | {} | Record | Configuration section for OCPP commands trigger support. Empty section means all implemented OCPP trigger commands are supported | +| Configuration | | | ChargingStationOcppConfiguration | charging stations OCPP parameters configuration section | +| AutomaticTransactionGenerator | | | AutomaticTransactionGeneratorConfiguration | charging stations ATG configuration section | +| Connectors | | | Record | charging stations connectors configuration section | +| Evses | | | Record | charging stations EVSEs configuration section | #### Configuration section syntax example diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 9741fc25..96e7b8ce 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -6,6 +6,7 @@ import { fileURLToPath } from 'node:url'; import { isMainThread } from 'node:worker_threads'; import chalk from 'chalk'; +import { availableParallelism } from 'poolifier'; import { waitChargingStationEvents } from './ChargingStationUtils'; import type { AbstractUIServer } from './ui-server/AbstractUIServer'; @@ -35,7 +36,7 @@ import { logPrefix, logger, } from '../utils'; -import { type WorkerAbstract, WorkerFactory } from '../worker'; +import { type WorkerAbstract, WorkerConstants, WorkerFactory } from '../worker'; const moduleName = 'Bootstrap'; @@ -211,6 +212,13 @@ export class Bootstrap extends EventEmitter { } private initializeWorkerImplementation(): void { + let elementsPerWorker = WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER; + if ( + Configuration.getWorker()?.elementsPerWorker === 'auto' && + this.numberOfChargingStations > availableParallelism() + ) { + elementsPerWorker = Math.round(this.numberOfChargingStations / availableParallelism()); + } this.workerImplementation === null && (this.workerImplementation = WorkerFactory.getWorkerImplementation( this.workerScript, @@ -220,7 +228,7 @@ export class Bootstrap extends EventEmitter { elementStartDelay: Configuration.getWorker().elementStartDelay, poolMaxSize: Configuration.getWorker().poolMaxSize, poolMinSize: Configuration.getWorker().poolMinSize, - elementsPerWorker: Configuration.getWorker().elementsPerWorker, + elementsPerWorker, poolOptions: { workerChoiceStrategy: Configuration.getWorker().poolStrategy, messageHandler: this.messageHandler.bind(this) as (message: unknown) => void, diff --git a/src/types/ConfigurationData.ts b/src/types/ConfigurationData.ts index a244aa45..94ac3b34 100644 --- a/src/types/ConfigurationData.ts +++ b/src/types/ConfigurationData.ts @@ -53,7 +53,7 @@ export type StorageConfiguration = { export type WorkerConfiguration = { processType?: WorkerProcessType; startDelay?: number; - elementsPerWorker?: number; + elementsPerWorker?: number | 'auto'; elementStartDelay?: number; poolMinSize?: number; poolMaxSize?: number; diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index c22fc334..92ce46e3 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -288,7 +288,7 @@ export class Configuration { const defaultWorkerConfiguration: WorkerConfiguration = { processType: WorkerProcessType.workerSet, startDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY, - elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, + elementsPerWorker: 'auto', elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY, poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, -- 2.34.1