import Configuration from '../utils/Configuration';
import Pool from 'worker-threads-pool';
import { Worker } from 'worker_threads';
+import WorkerData from '../types/WorkerData';
export default class Wrk {
- private _workerData;
- private _workerScript;
- private _pool;
+ private _workerScript: string;
+ private _workerData: WorkerData;
+ private _pool: Pool;
private _concurrentWorkers: number;
/**
* Create a new `Wrk`.
*
- * @param {String} workerScript
- * @param {Object} workerData
- * @param {Number} numConcurrentWorkers
+ * @param {string} workerScript
+ * @param {WorkerData} workerData
+ * @param {number} numConcurrentWorkers
*/
- constructor(workerScript, workerData, numConcurrentWorkers) {
+ constructor(workerScript: string, workerData: WorkerData, numConcurrentWorkers: number) {
this._workerData = workerData;
this._workerScript = workerScript;
- this._numConcurrentWorkers = numConcurrentWorkers;
if (Configuration.useWorkerPool()) {
+ this._concurrentWorkers = Configuration.getWorkerPoolSize();
this._pool = new Pool({ max: Configuration.getWorkerPoolSize() });
+ } else {
+ this._concurrentWorkers = numConcurrentWorkers;
}
}
/**
- * @param {Number} numConcurrentWorkers
- * @private
+ * @return {number}
+ * @public
*/
- set _numConcurrentWorkers(numConcurrentWorkers: number) {
- this._concurrentWorkers = numConcurrentWorkers;
- }
-
- get _numConcurrentWorkers(): number {
+ public get concurrentWorkers(): number {
return this._concurrentWorkers;
}
static start() {
try {
let numStationsTotal = 0;
+ let numConcurrentWorkers = 0;
// Start each ChargingStation object in a worker thread
if (Configuration.getStationTemplateURLs()) {
Configuration.getStationTemplateURLs().forEach((stationURL: StationTemplateURL) => {
templateFile: stationURL.file,
}, numStationsTotal);
worker.start().catch(() => {});
+ 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 {
- console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s)');
+ console.log('Charging station simulator started with ' + numStationsTotal.toString() + ' charging station(s) of ' + numConcurrentWorkers.toString() + ' concurrently running');
}
} catch (error) {
// eslint-disable-next-line no-console
}
static roundTo(number: number, scale: number): number {
- return Utils.convertToFloat(number.toFixed(scale));
+ const roundPower = Math.pow(10, scale);
+ return Math.round(number * roundPower) / roundPower;
}
static getRandomFloatRounded(max: number, min = 0, scale = 2): number {