From 76e2e9784b6912c2bf33f19768ecc2121168110d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 6 Jul 2023 20:44:20 +0200 Subject: [PATCH] feat: add nanothreads 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 --- .vscode/settings.json | 1 + .../fixed-nanothreads.mjs | 30 +++++++++++++++++++ .../hyperfine_benchmarks.sh | 1 + benchmarks/versus-external-pools/package.json | 3 +- .../versus-external-pools/pnpm-lock.yaml | 15 +++++++--- 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 benchmarks/versus-external-pools/fixed-nanothreads.mjs diff --git a/.vscode/settings.json b/.vscode/settings.json index 867b65ff..002ad7d1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,6 +24,7 @@ "microtime", "mochawesome", "MYBENCH", + "nanothreads", "nocombine", "nproc", "octocat", diff --git a/benchmarks/versus-external-pools/fixed-nanothreads.mjs b/benchmarks/versus-external-pools/fixed-nanothreads.mjs new file mode 100644 index 00000000..4f589e2c --- /dev/null +++ b/benchmarks/versus-external-pools/fixed-nanothreads.mjs @@ -0,0 +1,30 @@ +// IMPORT LIBRARIES +import { ThreadPool } from 'nanothreads' +// FINISH IMPORT LIBRARIES +// IMPORT FUNCTION TO BENCH +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 = { + test: 'MYBENCH', + taskType: process.env.TASK_TYPE, + taskSize: parseInt(process.env.TASK_SIZE) +} + +const threadPool = new ThreadPool({ + task: functionToBench, + count: size +}) + +async function run () { + const promises = new Set() + for (let i = 0; i < iterations; i++) { + promises.add(threadPool.exec(data)) + } + await Promise.all(promises) + // eslint-disable-next-line n/no-process-exit + process.exit() +} + +await run() diff --git a/benchmarks/versus-external-pools/hyperfine_benchmarks.sh b/benchmarks/versus-external-pools/hyperfine_benchmarks.sh index 67d25af3..594eb446 100755 --- a/benchmarks/versus-external-pools/hyperfine_benchmarks.sh +++ b/benchmarks/versus-external-pools/hyperfine_benchmarks.sh @@ -13,3 +13,4 @@ hyperfine --export-markdown BENCH-100000.md --min-runs 20 --prepare 'sleep 5' -- 'node fixed-worker-nodes.js' \ 'node dynamic-node-worker-threads-pool.mjs' \ 'node static-node-worker-threads-pool.mjs' \ + 'node fixed-nanothreads.mjs' diff --git a/benchmarks/versus-external-pools/package.json b/benchmarks/versus-external-pools/package.json index 245cca37..6d3a9926 100644 --- a/benchmarks/versus-external-pools/package.json +++ b/benchmarks/versus-external-pools/package.json @@ -17,10 +17,11 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "dependencies": { + "nanothreads": "0.3.7", "node-worker-threads-pool": "1.5.1", "piscina": "4.0.0", "poolifier": "2.6.8", - "tinypool": "0.6.0", + "tinypool": "0.7.0", "worker-nodes": "2.6.0", "workerpool": "6.4.0" } diff --git a/benchmarks/versus-external-pools/pnpm-lock.yaml b/benchmarks/versus-external-pools/pnpm-lock.yaml index 08f2d5c8..3224f6ff 100644 --- a/benchmarks/versus-external-pools/pnpm-lock.yaml +++ b/benchmarks/versus-external-pools/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + nanothreads: + specifier: 0.3.7 + version: 0.3.7 node-worker-threads-pool: specifier: 1.5.1 version: 1.5.1 @@ -15,8 +18,8 @@ dependencies: specifier: 2.6.8 version: 2.6.8 tinypool: - specifier: 0.6.0 - version: 0.6.0 + specifier: 0.7.0 + version: 0.7.0 worker-nodes: specifier: 2.6.0 version: 2.6.0 @@ -76,6 +79,10 @@ packages: isarray: 1.0.0 dev: false + /nanothreads@0.3.7: + resolution: {integrity: sha512-l/gbxnQBeMGspLoa+JKhqDrsBtqMW4G5ElsEgbVigR5gl6IBHzBjpM7URGeu0UQN4k7DK0o+J3GmHgaXv+aDQw==} + dev: false + /nice-napi@1.0.2: resolution: {integrity: sha512-px/KnJAJZf5RuBGcfD+Sp2pAKq0ytz8j+1NehvgIGFkvtvFrDM3T8E4x/JJODXK9WZow8RRGrbA9QQ3hs+pDhA==} os: ['!win32'] @@ -121,8 +128,8 @@ packages: requiresBuild: true dev: false - /tinypool@0.6.0: - resolution: {integrity: sha512-FdswUUo5SxRizcBc6b1GSuLpLjisa8N8qMyYoP3rl+bym+QauhtJP5bvZY1ytt8krKGmMLYIRl36HBZfeAoqhQ==} + /tinypool@0.7.0: + resolution: {integrity: sha512-zSYNUlYSMhJ6Zdou4cJwo/p7w5nmAH17GRfU/ui3ctvjXFErXXkruT4MWW6poDeXgCaIBlGLrfU6TbTXxyGMww==} engines: {node: '>=14.0.0'} dev: false -- 2.34.1