From bea2d6e33a81c97cfb0cc003befd149eddcd4708 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 26 Jun 2023 13:07:17 +0200 Subject: [PATCH] refactor: partially revert ESM conversion in benchmarking code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/README.md | 4 +++- .../dynamic-node-worker-threads-pool.mjs | 2 +- ...ic-worker-nodes.mjs => dynamic-worker-nodes.js} | 14 +++++++++++--- .../versus-external-pools/fixed-microjob.mjs | 2 +- ...ixed-worker-nodes.mjs => fixed-worker-nodes.js} | 14 +++++++++++--- ...{function-to-bench.mjs => function-to-bench.js} | 7 +++---- .../versus-external-pools/hyperfine_benchmarks.sh | 6 ++++-- .../versus-external-pools/pool-threadwork.mjs | 2 +- .../static-node-worker-threads-pool.mjs | 2 +- .../workers/piscina/function-to-bench-worker.mjs | 2 +- .../workers/poolifier/function-to-bench-worker.mjs | 2 +- .../workers/threadjs/function-to-bench-worker.mjs | 2 +- .../workers/tinypool/function-to-bench-worker.mjs | 2 +- .../worker-nodes/function-to-bench-worker.js | 2 ++ .../worker-nodes/function-to-bench-worker.mjs | 2 -- .../workerpool/function-to-bench-worker.mjs | 2 +- 16 files changed, 43 insertions(+), 24 deletions(-) rename benchmarks/versus-external-pools/{dynamic-worker-nodes.mjs => dynamic-worker-nodes.js} (71%) rename benchmarks/versus-external-pools/{fixed-worker-nodes.mjs => fixed-worker-nodes.js} (71%) rename benchmarks/versus-external-pools/functions/{function-to-bench.mjs => function-to-bench.js} (92%) create mode 100644 benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.js delete mode 100644 benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.mjs diff --git a/benchmarks/README.md b/benchmarks/README.md index fcbfd652..3b7f0702 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -17,6 +17,7 @@ External pools with which we compared the poolifier results: - [tinypool](https://github.com/tinylibs/tinypool) - [workerpool](https://github.com/josdejong/workerpool) - [worker-nodes](https://github.com/allegro/node-worker-nodes) +- [node-worker-threads-pool](https://github.com/SUCHMOKUO/node-worker-threads-pool) - [threads.js](https://github.com/andywer/threads.js/) - [threadwork](https://github.com/kevlened/threadwork) - [microjob](https://github.com/wilk/microjob) @@ -29,7 +30,8 @@ Those are our results: External pools with which we used to compare the poolifier results: -- [node-worker-threads-pool](https://github.com/SUCHMOKUO/node-worker-threads-pool): removed because it does not support dynamic modules import or import outside the worker function. The worker function is expected to be self-contained, which makes it difficult to use in real world application without ugly hacks. + + - [worker-threads-pool](https://github.com/watson/worker-threads-pool): removed because unmaintained since more than 4 years. ### Internal 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 743b01d6..debe8513 100644 --- a/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs +++ b/benchmarks/versus-external-pools/dynamic-node-worker-threads-pool.mjs @@ -2,7 +2,7 @@ import { DynamicPool } from 'node-worker-threads-pool' // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH -import functionToBench from './functions/function-to-bench.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) diff --git a/benchmarks/versus-external-pools/dynamic-worker-nodes.mjs b/benchmarks/versus-external-pools/dynamic-worker-nodes.js similarity index 71% rename from benchmarks/versus-external-pools/dynamic-worker-nodes.mjs rename to benchmarks/versus-external-pools/dynamic-worker-nodes.js index 5404c9b8..fffa5594 100644 --- a/benchmarks/versus-external-pools/dynamic-worker-nodes.mjs +++ b/benchmarks/versus-external-pools/dynamic-worker-nodes.js @@ -1,5 +1,5 @@ // IMPORT LIBRARIES -import WorkerNodes from 'worker-nodes' +const WorkerNodes = require('worker-nodes') // FINISH IMPORT LIBRARIES const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) @@ -10,7 +10,7 @@ const data = { } const workerNodes = new WorkerNodes( - import.meta.resolve('./workers/worker-nodes/function-to-bench-worker'), + require.resolve('./workers/worker-nodes/function-to-bench-worker'), { minWorkers: size, maxWorkers: size * 3, @@ -28,4 +28,12 @@ async function run () { process.exit() } -await run() +(async () => { + try { + await run() + } catch (e) { + console.error(e) + // eslint-disable-next-line n/no-process-exit + process.exit(1) + } +})() diff --git a/benchmarks/versus-external-pools/fixed-microjob.mjs b/benchmarks/versus-external-pools/fixed-microjob.mjs index f4e121a5..85075865 100644 --- a/benchmarks/versus-external-pools/fixed-microjob.mjs +++ b/benchmarks/versus-external-pools/fixed-microjob.mjs @@ -2,7 +2,7 @@ import { job, start } from 'microjob' // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH -import functionToBench from './functions/function-to-bench.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) diff --git a/benchmarks/versus-external-pools/fixed-worker-nodes.mjs b/benchmarks/versus-external-pools/fixed-worker-nodes.js similarity index 71% rename from benchmarks/versus-external-pools/fixed-worker-nodes.mjs rename to benchmarks/versus-external-pools/fixed-worker-nodes.js index bffef10c..641afd77 100644 --- a/benchmarks/versus-external-pools/fixed-worker-nodes.mjs +++ b/benchmarks/versus-external-pools/fixed-worker-nodes.js @@ -1,5 +1,5 @@ // IMPORT LIBRARIES -import WorkerNodes from 'worker-nodes' +const WorkerNodes = require('worker-nodes') // FINISH IMPORT LIBRARIES const size = parseInt(process.env.POOL_SIZE) const iterations = parseInt(process.env.NUM_ITERATIONS) @@ -10,7 +10,7 @@ const data = { } const workerNodes = new WorkerNodes( - import.meta.resolve('./workers/worker-nodes/function-to-bench-worker'), + require.resolve('./workers/worker-nodes/function-to-bench-worker'), { minWorkers: size, maxWorkers: size, @@ -28,4 +28,12 @@ async function run () { process.exit() } -await run() +(async () => { + try { + await run() + } catch (e) { + console.error(e) + // eslint-disable-next-line n/no-process-exit + process.exit(1) + } +})() diff --git a/benchmarks/versus-external-pools/functions/function-to-bench.mjs b/benchmarks/versus-external-pools/functions/function-to-bench.js similarity index 92% rename from benchmarks/versus-external-pools/functions/function-to-bench.mjs rename to benchmarks/versus-external-pools/functions/function-to-bench.js index 4705273c..74a07d1e 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.mjs +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,13 +1,12 @@ -import crypto from 'crypto' -import fs from 'fs' - /** * The worker function to execute during pools benchmarks. * NOTE: This function requires to be self-contained, thread-safe and re-entrant. * @param {*} data The worker data. * @returns {*} The result. */ -export default function functionToBench (data) { +module.exports = function functionToBench (data) { + const crypto = require('crypto') + const fs = require('fs') const TaskTypes = { CPU_INTENSIVE: 'CPU_INTENSIVE', IO_INTENSIVE: 'IO_INTENSIVE' diff --git a/benchmarks/versus-external-pools/hyperfine_benchmarks.sh b/benchmarks/versus-external-pools/hyperfine_benchmarks.sh index b73ccaf4..b6534d82 100755 --- a/benchmarks/versus-external-pools/hyperfine_benchmarks.sh +++ b/benchmarks/versus-external-pools/hyperfine_benchmarks.sh @@ -9,8 +9,10 @@ hyperfine --export-markdown BENCH-100000.md --min-runs 20 --prepare 'sleep 2' -- 'node dynamic-tinypool.mjs' \ 'node dynamic-workerpool.mjs' \ 'node fixed-workerpool.mjs' \ - 'node --experimental-import-meta-resolve dynamic-worker-nodes.mjs' \ - 'node --experimental-import-meta-resolve fixed-worker-nodes.mjs' \ + 'node dynamic-worker-nodes.js' \ + 'node fixed-worker-nodes.js' \ + 'node dynamic-node-worker-threads-pool.mjs' \ + 'node static-node-worker-threads-pool.mjs' \ 'node threadjs.mjs' \ 'node fixed-threadwork.mjs' \ 'node fixed-microjob.mjs' diff --git a/benchmarks/versus-external-pools/pool-threadwork.mjs b/benchmarks/versus-external-pools/pool-threadwork.mjs index 4541e58c..545c013e 100644 --- a/benchmarks/versus-external-pools/pool-threadwork.mjs +++ b/benchmarks/versus-external-pools/pool-threadwork.mjs @@ -2,7 +2,7 @@ import { ThreadPool } from 'threadwork' // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH -import functionToBench from './functions/function-to-bench.mjs' +import functionToBench from './functions/function-to-bench.js' // FINISH IMPORT FUNCTION TO BENCH const size = parseInt(process.env.POOL_SIZE) 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 b943be5d..b8a90ea3 100644 --- a/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs +++ b/benchmarks/versus-external-pools/static-node-worker-threads-pool.mjs @@ -2,7 +2,7 @@ import { StaticPool } from 'node-worker-threads-pool' // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH -import functionToBench from './functions/function-to-bench.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) diff --git a/benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.mjs b/benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.mjs index 3a335928..41f6fd79 100644 --- a/benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.mjs +++ b/benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.mjs @@ -1,2 +1,2 @@ -import functionToBench from '../../functions/function-to-bench.mjs' +import functionToBench from '../../functions/function-to-bench.js' export default functionToBench diff --git a/benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.mjs b/benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.mjs index f889e44a..3b058629 100644 --- a/benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.mjs +++ b/benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.mjs @@ -1,3 +1,3 @@ import { ThreadWorker } from 'poolifier' -import functionToBench from '../../functions/function-to-bench.mjs' +import functionToBench from '../../functions/function-to-bench.js' export default new ThreadWorker(functionToBench) diff --git a/benchmarks/versus-external-pools/workers/threadjs/function-to-bench-worker.mjs b/benchmarks/versus-external-pools/workers/threadjs/function-to-bench-worker.mjs index d77174f9..8217e22d 100644 --- a/benchmarks/versus-external-pools/workers/threadjs/function-to-bench-worker.mjs +++ b/benchmarks/versus-external-pools/workers/threadjs/function-to-bench-worker.mjs @@ -1,5 +1,5 @@ import { expose } from 'threads/worker' -import functionToBench from '../../functions/function-to-bench.mjs' +import functionToBench from '../../functions/function-to-bench.js' expose({ exposedFunction (data) { diff --git a/benchmarks/versus-external-pools/workers/tinypool/function-to-bench-worker.mjs b/benchmarks/versus-external-pools/workers/tinypool/function-to-bench-worker.mjs index 3a335928..41f6fd79 100644 --- a/benchmarks/versus-external-pools/workers/tinypool/function-to-bench-worker.mjs +++ b/benchmarks/versus-external-pools/workers/tinypool/function-to-bench-worker.mjs @@ -1,2 +1,2 @@ -import functionToBench from '../../functions/function-to-bench.mjs' +import functionToBench from '../../functions/function-to-bench.js' export default functionToBench diff --git a/benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.js b/benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.js new file mode 100644 index 00000000..3f3fc231 --- /dev/null +++ b/benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.js @@ -0,0 +1,2 @@ +const functionToBench = require('../../functions/function-to-bench') +module.exports = functionToBench diff --git a/benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.mjs b/benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.mjs deleted file mode 100644 index afa4ce13..00000000 --- a/benchmarks/versus-external-pools/workers/worker-nodes/function-to-bench-worker.mjs +++ /dev/null @@ -1,2 +0,0 @@ -import functionToBench from '../../functions/function-to-bench.mjs' -export { functionToBench } diff --git a/benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.mjs b/benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.mjs index d8981c86..c49013c3 100644 --- a/benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.mjs +++ b/benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.mjs @@ -1,5 +1,5 @@ import workerpool from 'workerpool' -import functionToBench from '../../functions/function-to-bench.mjs' +import functionToBench from '../../functions/function-to-bench.js' function wrapperFunctionToBench (testName, taskType, taskSize) { return functionToBench({ -- 2.34.1