From 144f78e0cf2579cbd0a05ea30bc95ac6b57050f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 9 Mar 2021 10:36:41 +0100 Subject: [PATCH] Add workerpool to external pools benchmark (#263) --- benchmarks/versus-external-pools/bench.sh | 6 +++-- .../dynamic-poolifier.js | 2 +- ...amic-suchmokuo-node-worker-threads-pool.js | 2 +- .../dynamic-workerpool.js | 26 +++++++++++++++++++ .../versus-external-pools/fixed-poolifier.js | 2 +- .../versus-external-pools/fixed-workerpool.js | 26 +++++++++++++++++++ .../functions/function-to-bench.js | 2 +- .../versus-external-pools/package-lock.json | 6 ++--- benchmarks/versus-external-pools/package.json | 2 +- ...atic-suchmokuo-node-worker-threads-pool.js | 2 +- .../workerpool/function-to-bench-worker.js | 11 ++++++++ 11 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 benchmarks/versus-external-pools/dynamic-workerpool.js create mode 100644 benchmarks/versus-external-pools/fixed-workerpool.js create mode 100644 benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.js diff --git a/benchmarks/versus-external-pools/bench.sh b/benchmarks/versus-external-pools/bench.sh index b6c339f8..ae5b76ee 100755 --- a/benchmarks/versus-external-pools/bench.sh +++ b/benchmarks/versus-external-pools/bench.sh @@ -21,6 +21,8 @@ hyperfine --export-markdown BENCH-100000.md --min-runs 10 \ 'node fixed-piscina.js' \ 'node dynamic-poolifier.js' \ 'node fixed-poolifier.js' \ - 'node static-suchmokuo-node-worker-threads-pool.js' \ 'node dynamic-suchmokuo-node-worker-threads-pool.js' \ - 'node threadjs.js' + 'node static-suchmokuo-node-worker-threads-pool.js' \ + 'node threadjs.js' \ + 'node dynamic-workerpool.js' \ + 'node fixed-workerpool.js' diff --git a/benchmarks/versus-external-pools/dynamic-poolifier.js b/benchmarks/versus-external-pools/dynamic-poolifier.js index 3c51ac4c..b20082a3 100644 --- a/benchmarks/versus-external-pools/dynamic-poolifier.js +++ b/benchmarks/versus-external-pools/dynamic-poolifier.js @@ -1,5 +1,5 @@ // IMPORT LIBRARIES -const { FixedThreadPool, DynamicThreadPool } = require('poolifier') +const { DynamicThreadPool } = require('poolifier') // FINISH IMPORT LIBRARIES const size = process.env.POOL_SIZE const iterations = process.env.NUM_ITERATIONS 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 c183e536..a418bcfc 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 @@ -1,5 +1,5 @@ // IMPORT LIBRARIES -const { DynamicPool, StaticPool } = require('node-worker-threads-pool') +const { DynamicPool } = require('node-worker-threads-pool') // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH const functionToBench = require('./functions/function-to-bench') diff --git a/benchmarks/versus-external-pools/dynamic-workerpool.js b/benchmarks/versus-external-pools/dynamic-workerpool.js new file mode 100644 index 00000000..557e45e6 --- /dev/null +++ b/benchmarks/versus-external-pools/dynamic-workerpool.js @@ -0,0 +1,26 @@ +// IMPORT LIBRARIES +const workerpool = require('workerpool') +// FINISH IMPORT LIBRARIES +const size = process.env.POOL_SIZE +const iterations = process.env.NUM_ITERATIONS +const dataArray = ['MYBENCH', process.env['TASK_TYPE']] + +const workerPool = workerpool.pool( + './workers/workerpool/function-to-bench-worker.js', + { + minWorkers: Number(size), + maxWorkers: size * 3, + workerType: 'thread' + } +) + +async function run () { + const promises = [] + for (let i = 0; i < iterations; i++) { + promises.push(workerPool.exec('functionToBench', dataArray)) + } + await Promise.all(promises) + process.exit() +} + +run() diff --git a/benchmarks/versus-external-pools/fixed-poolifier.js b/benchmarks/versus-external-pools/fixed-poolifier.js index e32a1166..f4573435 100644 --- a/benchmarks/versus-external-pools/fixed-poolifier.js +++ b/benchmarks/versus-external-pools/fixed-poolifier.js @@ -1,5 +1,5 @@ // IMPORT LIBRARIES -const { FixedThreadPool, DynamicThreadPool } = require('poolifier') +const { FixedThreadPool } = require('poolifier') // FINISH IMPORT LIBRARIES const size = process.env.POOL_SIZE const iterations = process.env.NUM_ITERATIONS diff --git a/benchmarks/versus-external-pools/fixed-workerpool.js b/benchmarks/versus-external-pools/fixed-workerpool.js new file mode 100644 index 00000000..5807f67c --- /dev/null +++ b/benchmarks/versus-external-pools/fixed-workerpool.js @@ -0,0 +1,26 @@ +// IMPORT LIBRARIES +const workerpool = require('workerpool') +// FINISH IMPORT LIBRARIES +const size = process.env.POOL_SIZE +const iterations = process.env.NUM_ITERATIONS +const dataArray = ['MYBENCH', process.env['TASK_TYPE']] + +const workerPool = workerpool.pool( + './workers/workerpool/function-to-bench-worker.js', + { + minWorkers: Number(size), + maxWorkers: Number(size), + workerType: 'thread' + } +) + +async function run () { + const promises = [] + for (let i = 0; i < iterations; i++) { + promises.push(workerPool.exec('functionToBench', dataArray)) + } + await Promise.all(promises) + process.exit() +} + +run() diff --git a/benchmarks/versus-external-pools/functions/function-to-bench.js b/benchmarks/versus-external-pools/functions/function-to-bench.js index 23312bfc..48a0fc78 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,6 +1,6 @@ module.exports = function (data) { if (data.taskType === 'CPU_INTENSIVE') { - // CPU Intensive task + // CPU intensive task for (let i = 0; i <= 5000; i++) { const o = { a: i diff --git a/benchmarks/versus-external-pools/package-lock.json b/benchmarks/versus-external-pools/package-lock.json index e05eac1c..9877bf75 100644 --- a/benchmarks/versus-external-pools/package-lock.json +++ b/benchmarks/versus-external-pools/package-lock.json @@ -230,9 +230,9 @@ } }, "workerpool": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.1.0.tgz", - "integrity": "sha512-toV7q9rWNYha963Pl/qyeZ6wG+3nnsyvolaNUS8+R5Wtw6qJPTxIlOP1ZSvcGhEJw+l3HMMmtiNo9Gl61G4GVg==" + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.1.2.tgz", + "integrity": "sha512-I/gDW4LwV3bslk4Yiqd4XoNYlnvV03LON7KuIjmQ90yDnKND1sR2LK/JA1g1tmd71oe6KPSvN0JpBzXIH6xAgA==" }, "wrappy": { "version": "1.0.2", diff --git a/benchmarks/versus-external-pools/package.json b/benchmarks/versus-external-pools/package.json index 1f5d1735..40d74149 100644 --- a/benchmarks/versus-external-pools/package.json +++ b/benchmarks/versus-external-pools/package.json @@ -16,6 +16,6 @@ "threads": "1.6.3", "threadwork": "0.6.0", "worker-threads-pool": "2.0.0", - "workerpool": "6.1.0" + "workerpool": "6.1.2" } } 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 fa151d58..17543d4b 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 @@ -1,5 +1,5 @@ // IMPORT LIBRARIES -const { DynamicPool, StaticPool } = require('node-worker-threads-pool') +const { StaticPool } = require('node-worker-threads-pool') // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH const functionToBench = require('./functions/function-to-bench') diff --git a/benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.js b/benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.js new file mode 100644 index 00000000..02f777f7 --- /dev/null +++ b/benchmarks/versus-external-pools/workers/workerpool/function-to-bench-worker.js @@ -0,0 +1,11 @@ +'use strict' +const workerpool = require('workerpool') +const functionToBench = require('../../functions/function-to-bench') + +function workerPoolWrapperFunctionToBench (testName, taskType) { + return functionToBench({ test: testName, taskType: taskType }) +} + +workerpool.worker({ + functionToBench: workerPoolWrapperFunctionToBench +}) -- 2.34.1