From 479ba9f6225633cd90167c26aa0fe0e79401b989 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 9 Sep 2023 23:27:52 +0200 Subject: [PATCH] refactor: cleanup benchmark code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/benchmarks-utils.mjs | 98 +++++++++++-------- benchmarks/internal/bench.mjs | 8 +- .../dynamic-node-worker-threads-pool.mjs | 8 +- .../versus-external-pools/dynamic-piscina.mjs | 6 +- .../dynamic-poolifier.mjs | 6 +- .../dynamic-tinypool.mjs | 6 +- .../dynamic-worker-nodes.js | 3 +- .../dynamic-workerpool.mjs | 6 +- .../fixed-nanothreads.mjs | 8 +- .../versus-external-pools/fixed-piscina.mjs | 6 +- .../versus-external-pools/fixed-poolifier.mjs | 6 +- .../versus-external-pools/fixed-tinypool.mjs | 6 +- .../fixed-worker-nodes.js | 3 +- .../fixed-workerpool.mjs | 6 +- .../static-node-worker-threads-pool.mjs | 8 +- 15 files changed, 97 insertions(+), 87 deletions(-) diff --git a/benchmarks/benchmarks-utils.mjs b/benchmarks/benchmarks-utils.mjs index 259fca56..0aeca085 100644 --- a/benchmarks/benchmarks-utils.mjs +++ b/benchmarks/benchmarks-utils.mjs @@ -10,7 +10,54 @@ import { } from '../lib/index.mjs' import { TaskFunctions } from './benchmarks-types.mjs' -export const runTest = async (pool, { taskExecutions, workerData }) => { +export const buildPoolifierPool = ( + workerType, + poolType, + poolSize, + poolOptions +) => { + switch (poolType) { + case PoolTypes.fixed: + switch (workerType) { + case WorkerTypes.thread: + return new FixedThreadPool( + poolSize, + './benchmarks/internal/thread-worker.mjs', + poolOptions + ) + case WorkerTypes.cluster: + return new FixedClusterPool( + poolSize, + './benchmarks/internal/cluster-worker.mjs', + poolOptions + ) + } + break + case PoolTypes.dynamic: + switch (workerType) { + case WorkerTypes.thread: + return new DynamicThreadPool( + Math.floor(poolSize / 2), + poolSize, + './benchmarks/internal/thread-worker.mjs', + poolOptions + ) + case WorkerTypes.cluster: + return new DynamicClusterPool( + Math.floor(poolSize / 2), + poolSize, + './benchmarks/internal/cluster-worker.mjs', + poolOptions + ) + } + break + } +} + +export const runPoolifierTest = async ( + pool, + { taskExecutions, workerData } +) => { return new Promise((resolve, reject) => { let executions = 0 for (let i = 1; i <= taskExecutions; i++) { @@ -31,6 +78,16 @@ export const runTest = async (pool, { taskExecutions, workerData }) => { }) } +export const executeAsyncFn = async fn => { + try { + await fn() + } catch (e) { + console.error(e) + // eslint-disable-next-line n/no-process-exit + process.exit(1) + } +} + export const generateRandomInteger = ( max = Number.MAX_SAFE_INTEGER, min = 0 @@ -114,42 +171,3 @@ export const executeTaskFunction = data => { throw new Error('Unknown task function') } } - -export const buildPool = (workerType, poolType, poolSize, poolOptions) => { - switch (poolType) { - case PoolTypes.fixed: - switch (workerType) { - case WorkerTypes.thread: - return new FixedThreadPool( - poolSize, - './benchmarks/internal/thread-worker.mjs', - poolOptions - ) - case WorkerTypes.cluster: - return new FixedClusterPool( - poolSize, - './benchmarks/internal/cluster-worker.mjs', - poolOptions - ) - } - break - case PoolTypes.dynamic: - switch (workerType) { - case WorkerTypes.thread: - return new DynamicThreadPool( - Math.floor(poolSize / 2), - poolSize, - './benchmarks/internal/thread-worker.mjs', - poolOptions - ) - case WorkerTypes.cluster: - return new DynamicClusterPool( - Math.floor(poolSize / 2), - poolSize, - './benchmarks/internal/cluster-worker.mjs', - poolOptions - ) - } - break - } -} diff --git a/benchmarks/internal/bench.mjs b/benchmarks/internal/bench.mjs index 2749da42..a14ecd5f 100644 --- a/benchmarks/internal/bench.mjs +++ b/benchmarks/internal/bench.mjs @@ -7,7 +7,7 @@ import { availableParallelism } from '../../lib/index.mjs' import { TaskFunctions } from '../benchmarks-types.mjs' -import { buildPool, runTest } from '../benchmarks-utils.mjs' +import { buildPoolifierPool, runPoolifierTest } from '../benchmarks-utils.mjs' const poolSize = availableParallelism() const pools = [] @@ -22,7 +22,7 @@ for (const poolType of Object.values(PoolTypes)) { for (const measurement of [Measurements.runTime, Measurements.elu]) { pools.push([ `${poolType}|${workerType}|${workerChoiceStrategy}|tasks queue:${enableTasksQueue}|measurement:${measurement}`, - buildPool(workerType, poolType, poolSize, { + buildPoolifierPool(workerType, poolType, poolSize, { workerChoiceStrategy, workerChoiceStrategyOptions: { measurement @@ -34,7 +34,7 @@ for (const poolType of Object.values(PoolTypes)) { } else { pools.push([ `${poolType}|${workerType}|${workerChoiceStrategy}|tasks queue:${enableTasksQueue}`, - buildPool(workerType, poolType, poolSize, { + buildPoolifierPool(workerType, poolType, poolSize, { workerChoiceStrategy, enableTasksQueue }) @@ -53,7 +53,7 @@ const workerData = { const addPools = pools => pools.map(([name, pool]) => { return add(name, async () => { - await runTest(pool, { + await runPoolifierTest(pool, { taskExecutions, workerData }) 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 fcb1d84c..ed828502 100644 --- a/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs +++ b/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs @@ -1,9 +1,7 @@ -// IMPORT LIBRARIES import { DynamicPool } from 'node-worker-threads-pool' -// FINISH IMPORT LIBRARIES -// IMPORT FUNCTION TO BENCH +import { executeAsyncFn } from '../benchmarks-utils.mjs' import functionToBench from './functions/function-to-bench.js' -// FINISH IMPORT FUNCTION TO BENCH + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -30,4 +28,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/dynamic-piscina.mjs b/benchmarks/versus-external-pools/dynamic-piscina.mjs index ddda47e7..3f56a699 100644 --- a/benchmarks/versus-external-pools/dynamic-piscina.mjs +++ b/benchmarks/versus-external-pools/dynamic-piscina.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import Piscina from 'piscina' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -26,4 +26,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/dynamic-poolifier.mjs b/benchmarks/versus-external-pools/dynamic-poolifier.mjs index ce605ecc..4f6a8b40 100644 --- a/benchmarks/versus-external-pools/dynamic-poolifier.mjs +++ b/benchmarks/versus-external-pools/dynamic-poolifier.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import { DynamicThreadPool } from 'poolifier' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -28,4 +28,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/dynamic-tinypool.mjs b/benchmarks/versus-external-pools/dynamic-tinypool.mjs index c1de3772..77b5d111 100644 --- a/benchmarks/versus-external-pools/dynamic-tinypool.mjs +++ b/benchmarks/versus-external-pools/dynamic-tinypool.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import Tinypool from 'tinypool' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -26,4 +26,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/dynamic-worker-nodes.js b/benchmarks/versus-external-pools/dynamic-worker-nodes.js index 7a77d882..c85a5f59 100644 --- a/benchmarks/versus-external-pools/dynamic-worker-nodes.js +++ b/benchmarks/versus-external-pools/dynamic-worker-nodes.js @@ -1,7 +1,6 @@ 'use strict' -// IMPORT LIBRARIES const WorkerNodes = require('worker-nodes') -// FINISH IMPORT LIBRARIES + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { diff --git a/benchmarks/versus-external-pools/dynamic-workerpool.mjs b/benchmarks/versus-external-pools/dynamic-workerpool.mjs index 2fb02d4b..0e09f1bb 100644 --- a/benchmarks/versus-external-pools/dynamic-workerpool.mjs +++ b/benchmarks/versus-external-pools/dynamic-workerpool.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import workerpool from 'workerpool' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const dataArray = [ @@ -28,4 +28,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/fixed-nanothreads.mjs b/benchmarks/versus-external-pools/fixed-nanothreads.mjs index 4f589e2c..d5572059 100644 --- a/benchmarks/versus-external-pools/fixed-nanothreads.mjs +++ b/benchmarks/versus-external-pools/fixed-nanothreads.mjs @@ -1,9 +1,7 @@ -// IMPORT LIBRARIES import { ThreadPool } from 'nanothreads' -// FINISH IMPORT LIBRARIES -// IMPORT FUNCTION TO BENCH +import { executeAsyncFn } from '../benchmarks-utils.mjs' import functionToBench from './functions/function-to-bench.js' -// FINISH IMPORT FUNCTION TO BENCH + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -27,4 +25,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/fixed-piscina.mjs b/benchmarks/versus-external-pools/fixed-piscina.mjs index 5fe01b62..97150f5e 100644 --- a/benchmarks/versus-external-pools/fixed-piscina.mjs +++ b/benchmarks/versus-external-pools/fixed-piscina.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import Piscina from 'piscina' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -26,4 +26,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/fixed-poolifier.mjs b/benchmarks/versus-external-pools/fixed-poolifier.mjs index 9a84e1e6..cbc7410d 100644 --- a/benchmarks/versus-external-pools/fixed-poolifier.mjs +++ b/benchmarks/versus-external-pools/fixed-poolifier.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import { FixedThreadPool } from 'poolifier' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -27,4 +27,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/fixed-tinypool.mjs b/benchmarks/versus-external-pools/fixed-tinypool.mjs index 07b3abe8..7063b5b9 100644 --- a/benchmarks/versus-external-pools/fixed-tinypool.mjs +++ b/benchmarks/versus-external-pools/fixed-tinypool.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import Tinypool from 'tinypool' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -26,4 +26,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) diff --git a/benchmarks/versus-external-pools/fixed-worker-nodes.js b/benchmarks/versus-external-pools/fixed-worker-nodes.js index d5b01353..7dfa79bd 100644 --- a/benchmarks/versus-external-pools/fixed-worker-nodes.js +++ b/benchmarks/versus-external-pools/fixed-worker-nodes.js @@ -1,7 +1,6 @@ 'use strict' -// IMPORT LIBRARIES const WorkerNodes = require('worker-nodes') -// FINISH IMPORT LIBRARIES + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { diff --git a/benchmarks/versus-external-pools/fixed-workerpool.mjs b/benchmarks/versus-external-pools/fixed-workerpool.mjs index 6e11c145..6387b84e 100644 --- a/benchmarks/versus-external-pools/fixed-workerpool.mjs +++ b/benchmarks/versus-external-pools/fixed-workerpool.mjs @@ -1,6 +1,6 @@ -// IMPORT LIBRARIES import workerpool from 'workerpool' -// FINISH IMPORT LIBRARIES +import { executeAsyncFn } from '../benchmarks-utils.mjs' + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const dataArray = [ @@ -28,4 +28,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) 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 3632216b..3275d6f2 100644 --- a/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs +++ b/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs @@ -1,9 +1,7 @@ -// IMPORT LIBRARIES import { StaticPool } from 'node-worker-threads-pool' -// FINISH IMPORT LIBRARIES -// IMPORT FUNCTION TO BENCH +import { executeAsyncFn } from '../benchmarks-utils.mjs' import functionToBench from './functions/function-to-bench.js' -// FINISH IMPORT FUNCTION TO BENCH + const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) const data = { @@ -27,4 +25,4 @@ async function run () { process.exit() } -await run() +await executeAsyncFn(run) -- 2.34.1