try {
let numStationsTotal = 0;
let numConcurrentWorkers = 0;
- let worker: Wrk;
const chargingStationsPerWorker = Configuration.getChargingStationsPerWorker();
- let counter = 0;
+ let chargingStationsPerWorkerCounter = 0;
+ let worker: Wrk;
// Start each ChargingStation object in a worker thread
if (Configuration.getStationTemplateURLs()) {
- for await (const stationURL of Configuration.getStationTemplateURLs()) {
+ for (const stationURL of Configuration.getStationTemplateURLs()) {
try {
const nbStations = stationURL.numberOfStations ? stationURL.numberOfStations : 0;
- numStationsTotal += nbStations;
for (let index = 1; index <= nbStations; index++) {
const workerData = {
index,
templateFile: stationURL.file
} as WorkerData;
- if (counter === 0 || counter === chargingStationsPerWorker) {
- // Start new worker with one charging station
- worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, numStationsTotal);
+ if (Configuration.useWorkerPool()) {
+ worker = new Wrk('./dist/charging-station/StationWorker.js', workerData);
worker.start().catch(() => { });
- counter = 0;
- // Start workers sequentially to optimize memory at start time
+ numConcurrentWorkers = worker.getWorkerPoolSize();
+ numStationsTotal = numConcurrentWorkers;
+ // Start Wrk sequentially to optimize memory at start time
await Utils.sleep(Constants.START_WORKER_DELAY);
- } else {
- // Add charging station to existing Worker
- worker.addChargingStation(workerData, numStationsTotal);
+ } else if (!Configuration.useWorkerPool() && (chargingStationsPerWorkerCounter === 0 || chargingStationsPerWorkerCounter >= chargingStationsPerWorker)) {
+ // Start new Wrk with one charging station
+ worker = new Wrk('./dist/charging-station/StationWorker.js', workerData, chargingStationsPerWorker);
+ worker.start().catch(() => { });
+ numConcurrentWorkers++;
+ chargingStationsPerWorkerCounter = 1;
+ numStationsTotal++;
+ // Start Wrk sequentially to optimize memory at start time
+ await Utils.sleep(Constants.START_WORKER_DELAY);
+ } else if (!Configuration.useWorkerPool()) {
+ // Add charging station to existing Wrk
+ worker.addWorkerElement(workerData);
+ chargingStationsPerWorkerCounter++;
+ numStationsTotal++;
}
- counter++;
- numConcurrentWorkers = worker.concurrentWorkers;
}
} catch (error) {
// eslint-disable-next-line no-console
}
if (numStationsTotal === 0) {
console.log('No charging station template enabled in configuration, exiting');
+ } else if (Configuration.useWorkerPool()) {
+ console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + '/' + Configuration.getWorkerPoolMaxSize().toString() + ' worker(s) concurrently running');
} else {
- console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
+ console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) and ' + numConcurrentWorkers.toString() + ' worker(s) concurrently running');
}
} catch (error) {
// eslint-disable-next-line no-console
}
}
-Bootstrap.start();
+Bootstrap.start().catch(
+ (error) => {
+ console.error(error);
+ }
+);