From 91a4016649c4a3128725286074b5bd894a32bad7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 22 Sep 2023 14:43:15 +0200 Subject: [PATCH] perf: add benchmark defaults to external pools benchmark 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/bench.sh | 6 +++--- .../dynamic-node-worker-threads-pool.mjs | 13 +++++++------ .../versus-external-pools/dynamic-piscina.mjs | 13 +++++++------ .../versus-external-pools/dynamic-poolifier.mjs | 13 +++++++------ .../versus-external-pools/dynamic-tinypool.mjs | 13 +++++++------ .../versus-external-pools/dynamic-worker-nodes.js | 12 +++++++----- .../versus-external-pools/dynamic-workerpool.mjs | 13 +++++++------ .../versus-external-pools/fixed-nanothreads.mjs | 13 +++++++------ benchmarks/versus-external-pools/fixed-piscina.mjs | 13 +++++++------ .../versus-external-pools/fixed-poolifier.mjs | 13 +++++++------ benchmarks/versus-external-pools/fixed-tinypool.mjs | 13 +++++++------ .../versus-external-pools/fixed-worker-nodes.js | 12 +++++++----- .../versus-external-pools/fixed-workerpool.mjs | 13 +++++++------ .../static-node-worker-threads-pool.mjs | 13 +++++++------ benchmarks/versus-external-pools/utils.mjs | 9 +++++++++ 15 files changed, 103 insertions(+), 79 deletions(-) diff --git a/benchmarks/versus-external-pools/bench.sh b/benchmarks/versus-external-pools/bench.sh index 7d7e9651..eeefbe80 100755 --- a/benchmarks/versus-external-pools/bench.sh +++ b/benchmarks/versus-external-pools/bench.sh @@ -38,14 +38,14 @@ done taskType=${taskType:-'CPU_INTENSIVE'} taskSize=${taskSize:-5000} poolSize=${poolSize:-$(nproc --all)} -iterations=${iterations:-100000} +numIterations=${iterations:-100000} -echo 'Running benchmarks with pool size:' ${poolSize}', number of iterations:' ${iterations}', task type:' ${taskType} 'and task size:' ${taskSize} +echo 'Running benchmarks with pool size:' ${poolSize}', number of iterations:' ${numIterations}', task type:' ${taskType} 'and task size:' ${taskSize} export NODE_ENV=production export TASK_TYPE=${taskType} export TASK_SIZE=${taskSize} export POOL_SIZE=${poolSize} -export NUM_ITERATIONS=${iterations} +export NUM_ITERATIONS=${numIterations} case "$OSTYPE" in darwin*) caffeinate ./hyperfine_benchmarks.sh diff --git a/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs b/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs index c4be1ee1..7b7361d0 100644 --- a/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs +++ b/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs @@ -1,20 +1,21 @@ import { DynamicPool } from 'node-worker-threads-pool' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' import functionToBench from './functions/function-to-bench.js' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const dynamicPool = new DynamicPool(size) async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add( dynamicPool.exec({ task: functionToBench, diff --git a/benchmarks/versus-external-pools/dynamic-piscina.mjs b/benchmarks/versus-external-pools/dynamic-piscina.mjs index 06c71779..4567714e 100644 --- a/benchmarks/versus-external-pools/dynamic-piscina.mjs +++ b/benchmarks/versus-external-pools/dynamic-piscina.mjs @@ -1,12 +1,13 @@ import Piscina from 'piscina' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const piscina = new Piscina({ @@ -18,7 +19,7 @@ const piscina = new Piscina({ async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(piscina.run(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/dynamic-poolifier.mjs b/benchmarks/versus-external-pools/dynamic-poolifier.mjs index 98d21006..0251de2f 100644 --- a/benchmarks/versus-external-pools/dynamic-poolifier.mjs +++ b/benchmarks/versus-external-pools/dynamic-poolifier.mjs @@ -1,12 +1,13 @@ import { DynamicThreadPool } from 'poolifier' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const dynamicThreadPool = new DynamicThreadPool( @@ -20,7 +21,7 @@ const dynamicThreadPool = new DynamicThreadPool( async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(dynamicThreadPool.execute(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/dynamic-tinypool.mjs b/benchmarks/versus-external-pools/dynamic-tinypool.mjs index 3c00b134..9043d1a0 100644 --- a/benchmarks/versus-external-pools/dynamic-tinypool.mjs +++ b/benchmarks/versus-external-pools/dynamic-tinypool.mjs @@ -1,12 +1,13 @@ import Tinypool from 'tinypool' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const tinypool = new Tinypool({ @@ -18,7 +19,7 @@ const tinypool = new Tinypool({ async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(tinypool.run(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/dynamic-worker-nodes.js b/benchmarks/versus-external-pools/dynamic-worker-nodes.js index c85a5f59..a67adb7f 100644 --- a/benchmarks/versus-external-pools/dynamic-worker-nodes.js +++ b/benchmarks/versus-external-pools/dynamic-worker-nodes.js @@ -1,12 +1,14 @@ 'use strict' const WorkerNodes = require('worker-nodes') +const { BenchmarkDefaults } = require('./utils.mjs') -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const workerNodes = new WorkerNodes( @@ -20,7 +22,7 @@ const workerNodes = new WorkerNodes( async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(workerNodes.call.functionToBench(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/dynamic-workerpool.mjs b/benchmarks/versus-external-pools/dynamic-workerpool.mjs index f3baa446..bd68c035 100644 --- a/benchmarks/versus-external-pools/dynamic-workerpool.mjs +++ b/benchmarks/versus-external-pools/dynamic-workerpool.mjs @@ -1,12 +1,13 @@ import workerpool from 'workerpool' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const dataArray = [ 'MYBENCH', - process.env.TASK_TYPE, - parseInt(process.env.TASK_SIZE) + process.env.TASK_TYPE || BenchmarkDefaults.taskType, + parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize ] const workerPool = workerpool.pool( @@ -20,7 +21,7 @@ const workerPool = workerpool.pool( async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(workerPool.exec('functionToBench', dataArray)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/fixed-nanothreads.mjs b/benchmarks/versus-external-pools/fixed-nanothreads.mjs index d18f71b5..3b497af5 100644 --- a/benchmarks/versus-external-pools/fixed-nanothreads.mjs +++ b/benchmarks/versus-external-pools/fixed-nanothreads.mjs @@ -1,13 +1,14 @@ import { ThreadPool } from 'nanothreads' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' import functionToBench from './functions/function-to-bench.js' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const threadPool = new ThreadPool({ @@ -17,7 +18,7 @@ const threadPool = new ThreadPool({ async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(threadPool.exec(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/fixed-piscina.mjs b/benchmarks/versus-external-pools/fixed-piscina.mjs index 761586ac..ad9b938a 100644 --- a/benchmarks/versus-external-pools/fixed-piscina.mjs +++ b/benchmarks/versus-external-pools/fixed-piscina.mjs @@ -1,12 +1,13 @@ import Piscina from 'piscina' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const piscina = new Piscina({ @@ -18,7 +19,7 @@ const piscina = new Piscina({ async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(piscina.run(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/fixed-poolifier.mjs b/benchmarks/versus-external-pools/fixed-poolifier.mjs index 5efd786e..a25b7fe4 100644 --- a/benchmarks/versus-external-pools/fixed-poolifier.mjs +++ b/benchmarks/versus-external-pools/fixed-poolifier.mjs @@ -1,12 +1,13 @@ import { FixedThreadPool } from 'poolifier' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const fixedThreadPool = new FixedThreadPool( @@ -19,7 +20,7 @@ const fixedThreadPool = new FixedThreadPool( async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(fixedThreadPool.execute(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/fixed-tinypool.mjs b/benchmarks/versus-external-pools/fixed-tinypool.mjs index 307ac3b5..5e41d40b 100644 --- a/benchmarks/versus-external-pools/fixed-tinypool.mjs +++ b/benchmarks/versus-external-pools/fixed-tinypool.mjs @@ -1,12 +1,13 @@ import Tinypool from 'tinypool' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const tinypool = new Tinypool({ @@ -18,7 +19,7 @@ const tinypool = new Tinypool({ async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(tinypool.run(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/fixed-worker-nodes.js b/benchmarks/versus-external-pools/fixed-worker-nodes.js index 7dfa79bd..9af2936b 100644 --- a/benchmarks/versus-external-pools/fixed-worker-nodes.js +++ b/benchmarks/versus-external-pools/fixed-worker-nodes.js @@ -1,12 +1,14 @@ 'use strict' const WorkerNodes = require('worker-nodes') +const { BenchmarkDefaults } = require('./utils.mjs') -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const workerNodes = new WorkerNodes( @@ -20,7 +22,7 @@ const workerNodes = new WorkerNodes( async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(workerNodes.call.functionToBench(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/fixed-workerpool.mjs b/benchmarks/versus-external-pools/fixed-workerpool.mjs index 9e016331..6ebc0d69 100644 --- a/benchmarks/versus-external-pools/fixed-workerpool.mjs +++ b/benchmarks/versus-external-pools/fixed-workerpool.mjs @@ -1,12 +1,13 @@ import workerpool from 'workerpool' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const dataArray = [ 'MYBENCH', - process.env.TASK_TYPE, - parseInt(process.env.TASK_SIZE) + process.env.TASK_TYPE || BenchmarkDefaults.taskType, + parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize ] const workerPool = workerpool.pool( @@ -20,7 +21,7 @@ const workerPool = workerpool.pool( async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(workerPool.exec('functionToBench', dataArray)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs b/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs index db14b180..0868d26c 100644 --- a/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs +++ b/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs @@ -1,13 +1,14 @@ import { StaticPool } from 'node-worker-threads-pool' -import { executeAsyncFn } from './utils.mjs' +import { BenchmarkDefaults, executeAsyncFn } from './utils.mjs' import functionToBench from './functions/function-to-bench.js' -const size = parseInt(process.env.POOL_SIZE) -const iterations = parseInt(process.env.NUM_ITERATIONS) +const size = parseInt(process.env.POOL_SIZE) || BenchmarkDefaults.poolSize +const numIterations = + parseInt(process.env.NUM_ITERATIONS) || BenchmarkDefaults.numIterations const data = { test: 'MYBENCH', - taskType: process.env.TASK_TYPE, - taskSize: parseInt(process.env.TASK_SIZE) + taskType: process.env.TASK_TYPE || BenchmarkDefaults.taskType, + taskSize: parseInt(process.env.TASK_SIZE) || BenchmarkDefaults.taskSize } const staticPool = new StaticPool({ @@ -17,7 +18,7 @@ const staticPool = new StaticPool({ async function run () { const promises = new Set() - for (let i = 0; i < iterations; i++) { + for (let i = 0; i < numIterations; i++) { promises.add(staticPool.exec(data)) } await Promise.all(promises) diff --git a/benchmarks/versus-external-pools/utils.mjs b/benchmarks/versus-external-pools/utils.mjs index 3d26ea16..8d58ded7 100644 --- a/benchmarks/versus-external-pools/utils.mjs +++ b/benchmarks/versus-external-pools/utils.mjs @@ -1,3 +1,5 @@ +import { availableParallelism } from 'poolifier' + export const executeAsyncFn = async fn => { try { await fn() @@ -7,3 +9,10 @@ export const executeAsyncFn = async fn => { process.exit(1) } } + +export const BenchmarkDefaults = { + poolSize: availableParallelism(), + numIterations: 100000, + taskType: 'CPU_INTENSIVE', + taskSize: 5000 +} -- 2.34.1