From 6bd72cd09232827a710b41952254cab597664247 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 20 Oct 2022 16:58:06 +0200 Subject: [PATCH] Benchmarks: properly parse env variables MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/versus-external-pools/dynamic-piscina.js | 6 +++--- benchmarks/versus-external-pools/dynamic-poolifier.js | 6 +++--- .../dynamic-suchmokuo-node-worker-threads-pool.js | 6 +++--- .../versus-external-pools/dynamic-worker-nodes.js | 6 +++--- benchmarks/versus-external-pools/dynamic-workerpool.js | 10 +++++++--- benchmarks/versus-external-pools/fixed-microjob.js | 6 +++--- benchmarks/versus-external-pools/fixed-piscina.js | 6 +++--- benchmarks/versus-external-pools/fixed-poolifier.js | 6 +++--- benchmarks/versus-external-pools/fixed-threadwork.js | 4 ++-- benchmarks/versus-external-pools/fixed-worker-nodes.js | 6 +++--- benchmarks/versus-external-pools/fixed-workerpool.js | 10 +++++++--- benchmarks/versus-external-pools/pool-threadwork.js | 2 +- .../static-suchmokuo-node-worker-threads-pool.js | 6 +++--- benchmarks/versus-external-pools/threadjs.js | 6 +++--- src/pools/abstract-pool.ts | 6 +++--- src/worker/abstract-worker.ts | 2 +- 16 files changed, 51 insertions(+), 43 deletions(-) diff --git a/benchmarks/versus-external-pools/dynamic-piscina.js b/benchmarks/versus-external-pools/dynamic-piscina.js index 7f707004..6b835d0e 100644 --- a/benchmarks/versus-external-pools/dynamic-piscina.js +++ b/benchmarks/versus-external-pools/dynamic-piscina.js @@ -1,12 +1,12 @@ // IMPORT LIBRARIES const Piscina = require('piscina') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const piscina = new Piscina({ diff --git a/benchmarks/versus-external-pools/dynamic-poolifier.js b/benchmarks/versus-external-pools/dynamic-poolifier.js index 197169a5..20628421 100644 --- a/benchmarks/versus-external-pools/dynamic-poolifier.js +++ b/benchmarks/versus-external-pools/dynamic-poolifier.js @@ -1,12 +1,12 @@ // IMPORT LIBRARIES const { DynamicThreadPool } = require('poolifier') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const dynamicPool = new DynamicThreadPool( diff --git a/benchmarks/versus-external-pools/dynamic-suchmokuo-node-worker-threads-pool.js b/benchmarks/versus-external-pools/dynamic-suchmokuo-node-worker-threads-pool.js index c65ffdea..0a56d1fd 100644 --- a/benchmarks/versus-external-pools/dynamic-suchmokuo-node-worker-threads-pool.js +++ b/benchmarks/versus-external-pools/dynamic-suchmokuo-node-worker-threads-pool.js @@ -4,12 +4,12 @@ const { DynamicPool } = require('node-worker-threads-pool') // IMPORT FUNCTION TO BENCH const functionToBench = require('./functions/function-to-bench') // FINISH IMPORT FUNCTION TO BENCH -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const pool = new DynamicPool(size) diff --git a/benchmarks/versus-external-pools/dynamic-worker-nodes.js b/benchmarks/versus-external-pools/dynamic-worker-nodes.js index 0c665f85..47484528 100644 --- a/benchmarks/versus-external-pools/dynamic-worker-nodes.js +++ b/benchmarks/versus-external-pools/dynamic-worker-nodes.js @@ -1,12 +1,12 @@ // IMPORT LIBRARIES const WorkerNodes = require('worker-nodes') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const workerNodes = new WorkerNodes( diff --git a/benchmarks/versus-external-pools/dynamic-workerpool.js b/benchmarks/versus-external-pools/dynamic-workerpool.js index 996522d3..04d90ab2 100644 --- a/benchmarks/versus-external-pools/dynamic-workerpool.js +++ b/benchmarks/versus-external-pools/dynamic-workerpool.js @@ -1,9 +1,13 @@ // IMPORT LIBRARIES const workerpool = require('workerpool') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) -const dataArray = ['MYBENCH', process.env.TASK_TYPE, process.env.TASK_SIZE] +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) +const dataArray = [ + 'MYBENCH', + process.env.TASK_TYPE, + parseInt(process.env.TASK_SIZE) +] const workerPool = workerpool.pool( './workers/workerpool/function-to-bench-worker.js', diff --git a/benchmarks/versus-external-pools/fixed-microjob.js b/benchmarks/versus-external-pools/fixed-microjob.js index dd028068..b61dcd94 100644 --- a/benchmarks/versus-external-pools/fixed-microjob.js +++ b/benchmarks/versus-external-pools/fixed-microjob.js @@ -4,12 +4,12 @@ const { job, start } = require('microjob') // IMPORT FUNCTION TO BENCH const functionToBench = require('./functions/function-to-bench') // FINISH IMPORT FUNCTION TO BENCH -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } async function run () { diff --git a/benchmarks/versus-external-pools/fixed-piscina.js b/benchmarks/versus-external-pools/fixed-piscina.js index fc8a87c1..8a92dbea 100644 --- a/benchmarks/versus-external-pools/fixed-piscina.js +++ b/benchmarks/versus-external-pools/fixed-piscina.js @@ -1,12 +1,12 @@ // IMPORT LIBRARIES const Piscina = require('piscina') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const piscina = new Piscina({ diff --git a/benchmarks/versus-external-pools/fixed-poolifier.js b/benchmarks/versus-external-pools/fixed-poolifier.js index fbfd484e..2a5e599a 100644 --- a/benchmarks/versus-external-pools/fixed-poolifier.js +++ b/benchmarks/versus-external-pools/fixed-poolifier.js @@ -1,12 +1,12 @@ // IMPORT LIBRARIES const { FixedThreadPool } = require('poolifier') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const fixedPool = new FixedThreadPool( diff --git a/benchmarks/versus-external-pools/fixed-threadwork.js b/benchmarks/versus-external-pools/fixed-threadwork.js index cb9c3ca0..d21fae6a 100644 --- a/benchmarks/versus-external-pools/fixed-threadwork.js +++ b/benchmarks/versus-external-pools/fixed-threadwork.js @@ -1,11 +1,11 @@ // IMPORT LIBRARIES const threadPool = require('./pool-threadwork') // FINISH IMPORT LIBRARIES -const iterations = Number(process.env.NUM_ITERATIONS) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } async function run () { diff --git a/benchmarks/versus-external-pools/fixed-worker-nodes.js b/benchmarks/versus-external-pools/fixed-worker-nodes.js index a720f197..49f8637d 100644 --- a/benchmarks/versus-external-pools/fixed-worker-nodes.js +++ b/benchmarks/versus-external-pools/fixed-worker-nodes.js @@ -1,12 +1,12 @@ // IMPORT LIBRARIES const WorkerNodes = require('worker-nodes') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const workerNodes = new WorkerNodes( diff --git a/benchmarks/versus-external-pools/fixed-workerpool.js b/benchmarks/versus-external-pools/fixed-workerpool.js index 499c17a5..cabdd9e7 100644 --- a/benchmarks/versus-external-pools/fixed-workerpool.js +++ b/benchmarks/versus-external-pools/fixed-workerpool.js @@ -1,9 +1,13 @@ // IMPORT LIBRARIES const workerpool = require('workerpool') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) -const dataArray = ['MYBENCH', process.env.TASK_TYPE, process.env.TASK_SIZE] +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) +const dataArray = [ + 'MYBENCH', + process.env.TASK_TYPE, + parseInt(process.env.TASK_SIZE) +] const workerPool = workerpool.pool( './workers/workerpool/function-to-bench-worker.js', diff --git a/benchmarks/versus-external-pools/pool-threadwork.js b/benchmarks/versus-external-pools/pool-threadwork.js index 36baad1f..2a2844f0 100644 --- a/benchmarks/versus-external-pools/pool-threadwork.js +++ b/benchmarks/versus-external-pools/pool-threadwork.js @@ -4,6 +4,6 @@ const { ThreadPool } = require('threadwork') // IMPORT FUNCTION TO BENCH const functionToBench = require('./functions/function-to-bench') // FINISH IMPORT FUNCTION TO BENCH -const size = Number(process.env.POOL_SIZE) +const size = parseInt(process.env.POOL_SIZE) module.exports = new ThreadPool({ task: functionToBench, size }) diff --git a/benchmarks/versus-external-pools/static-suchmokuo-node-worker-threads-pool.js b/benchmarks/versus-external-pools/static-suchmokuo-node-worker-threads-pool.js index 1c8f0d7f..75f3d2a4 100644 --- a/benchmarks/versus-external-pools/static-suchmokuo-node-worker-threads-pool.js +++ b/benchmarks/versus-external-pools/static-suchmokuo-node-worker-threads-pool.js @@ -4,12 +4,12 @@ const { StaticPool } = require('node-worker-threads-pool') // IMPORT FUNCTION TO BENCH const functionToBench = require('./functions/function-to-bench') // FINISH IMPORT FUNCTION TO BENCH -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } const pool = new StaticPool({ diff --git a/benchmarks/versus-external-pools/threadjs.js b/benchmarks/versus-external-pools/threadjs.js index b6b3a5fa..ee9178e9 100644 --- a/benchmarks/versus-external-pools/threadjs.js +++ b/benchmarks/versus-external-pools/threadjs.js @@ -1,12 +1,12 @@ // IMPORT LIBRARIES const { spawn, Worker } = require('threads') // FINISH IMPORT LIBRARIES -const size = Number(process.env.POOL_SIZE) -const iterations = Number(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) +const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env.TASK_TYPE, - taskSize: process.env.TASK_SIZE + taskSize: parseInt(process.env.TASK_SIZE) } // Threads.js is not really a pool so we need to write few additional code diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 40e06b15..346eb6d1 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -85,7 +85,7 @@ export abstract class AbstractPool< public readonly filePath: string, public readonly opts: PoolOptions ) { - if (!this.isMain()) { + if (this.isMain() === false) { throw new Error('Cannot start a pool from a worker!') } this.checkNumberOfWorkers(this.numberOfWorkers) @@ -97,7 +97,7 @@ export abstract class AbstractPool< this.createAndSetupWorker() } - if (this.opts.enableEvents) { + if (this.opts.enableEvents === true) { this.emitter = new PoolEmitter() } this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext( @@ -380,7 +380,7 @@ export abstract class AbstractPool< } private checkAndEmitBusy (): void { - if (this.opts.enableEvents && this.busy) { + if (this.opts.enableEvents === true && this.busy === true) { this.emitter?.emit('busy') } } diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 9ec38fed..3090b6f5 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -84,7 +84,7 @@ export abstract class AbstractWorker< ): void { if (value.data !== undefined && value.id !== undefined) { // Here you will receive messages - if (this.opts.async) { + if (this.opts.async === true) { this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) } else { this.runInAsyncScope(this.run.bind(this), this, fn, value) -- 2.34.1