From 9f7e7a99ea7dfa6f2fb3c2e0e025b85cfdd4e22e Mon Sep 17 00:00:00 2001 From: aardizio Date: Mon, 22 Feb 2021 16:13:01 +0100 Subject: [PATCH] Improvement on benchmarks --- benchmarks/README.md | 7 +-- .../versus-external-pools/BENCH-100000.MD | 9 ++-- benchmarks/versus-external-pools/bench.sh | 47 +++++++------------ .../{piscina.js => dynamic-piscina.js} | 5 +- .../dynamic-poolifier.js | 5 +- ...amic-suchmokuo-node-worker-threads-pool.js | 5 +- .../versus-external-pools/fixed-piscina.js | 26 ++++++++++ .../versus-external-pools/fixed-poolifier.js | 5 +- .../functions/function-to-bench.js | 14 ++++++ .../functions/json-stringify.js | 10 ---- .../versus-external-pools/package-lock.json | 6 +-- ...atic-suchmokuo-node-worker-threads-pool.js | 5 +- .../piscina/function-to-bench-worker.js | 3 ++ .../workers/piscina/json-stringify.worker.js | 3 -- .../poolifier/function-to-bench-worker.js | 4 ++ .../poolifier/json-stringify.worker.js | 4 -- 16 files changed, 92 insertions(+), 66 deletions(-) rename benchmarks/versus-external-pools/{piscina.js => dynamic-piscina.js} (81%) create mode 100644 benchmarks/versus-external-pools/fixed-piscina.js create mode 100644 benchmarks/versus-external-pools/functions/function-to-bench.js delete mode 100644 benchmarks/versus-external-pools/functions/json-stringify.js create mode 100644 benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.js delete mode 100644 benchmarks/versus-external-pools/workers/piscina/json-stringify.worker.js create mode 100644 benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.js delete mode 100644 benchmarks/versus-external-pools/workers/poolifier/json-stringify.worker.js diff --git a/benchmarks/README.md b/benchmarks/README.md index 907d8649..30bb4515 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -12,11 +12,10 @@ The versus-external-pools folder contains benchmarks versus other Node.js pools. To compare poolifier pools performance vs other pools performance we chose to use [hyperfine](https://github.com/sharkdp/hyperfine). We chose to use this tool because it allows to run isolated Node.js processes so that each pool does not impact each other. -We will add more details on each function that we benchmark. - Those are our results: -- CPU Intensive task with 100k operations submitted to each pool [BENCH-100000.MD](./versus-external-pools/BENCH-100000.MD) +- CPU Intensive task with 100k operations submitted to each pool [BENCH-100000.MD](./versus-external-pools/BENCH-100000.MD). +This benchmark ran on a MacBook Pro 2015, 2,2 GHz Intel Core i7 quad-core, 16 GB 1600 MHz DDR3 ## How to run benchmarks @@ -30,3 +29,5 @@ To run the benchmark versus other pools you will need to: - [Install hyperfine](https://github.com/sharkdp/hyperfine#installation) - Run the `./bench.sh` into the `versus-external-pools` folder + +> :warning: **Please be sure to use a quite PC when you run the benchmarks** diff --git a/benchmarks/versus-external-pools/BENCH-100000.MD b/benchmarks/versus-external-pools/BENCH-100000.MD index cd14d108..27371b2b 100644 --- a/benchmarks/versus-external-pools/BENCH-100000.MD +++ b/benchmarks/versus-external-pools/BENCH-100000.MD @@ -1,5 +1,8 @@ | Command | Mean [s] | Min [s] | Max [s] | Relative | |:---|---:|---:|---:|---:| -| `node static-suchmokuo-node-worker-threads-pool.js` | 47.473 ± 1.241 | 44.031 | 48.331 | 1.05 ± 0.03 | -| `node dynamic-poolifier.js` | 45.368 ± 0.173 | 45.028 | 45.595 | 1.00 | -| `node piscina.js` | 48.502 ± 0.390 | 47.896 | 49.362 | 1.07 ± 0.01 | +| `node static-suchmokuo-node-worker-threads-pool.js` | 50.403 ± 1.280 | 46.822 | 51.241 | 1.04 ± 0.04 | +| `node dynamic-suchmokuo-node-worker-threads-pool.js` | 62.664 ± 0.158 | 62.329 | 62.852 | 1.29 ± 0.03 | +| `node dynamic-poolifier.js` | 48.517 ± 1.233 | 47.662 | 51.249 | 1.00 | +| `node fixed-poolifier.js` | 48.539 ± 1.247 | 47.848 | 52.056 | 1.00 ± 0.04 | +| `node dynamic-piscina.js` | 51.187 ± 0.184 | 50.986 | 51.579 | 1.06 ± 0.03 | +| `node fixed-piscina.js` | 51.259 ± 0.087 | 51.114 | 51.404 | 1.06 ± 0.03 | diff --git a/benchmarks/versus-external-pools/bench.sh b/benchmarks/versus-external-pools/bench.sh index 05c156c9..290df239 100755 --- a/benchmarks/versus-external-pools/bench.sh +++ b/benchmarks/versus-external-pools/bench.sh @@ -1,37 +1,24 @@ -export NODE_ENV=production - -# Execute bench -# export POOL_SIZE=8 -# export NUM_ITERATIONS=10000 -# hyperfine --export-markdown BENCH-10000.MD --min-runs 10 \ -# 'node dynamic-poolifier.js' \ -# 'node dynamic-suchmokuo-node-worker-threads-pool.js' \ -# 'node fixed-poolifier.js' \ -# 'node static-suchmokuo-node-worker-threads-pool.js' \ -# 'node piscina.js' +### The -t argument is needed to specify the type of task that you want to benchmark. +### Supported values are CPU_INTENSIVE -# echo "Sleeping...." -# sleep 60 +taskType='CPU_INTENSIVE' +while getopts t: flag +do + case "${flag}" in + t) taskType=${OPTARG};; + esac +done +echo 'Running bench for task type:' $taskType +export TASK_TYPE=$taskType +# Execute bench +export NODE_ENV=production export POOL_SIZE=10 export NUM_ITERATIONS=100000 hyperfine --export-markdown BENCH-100000.MD --min-runs 10 \ 'node static-suchmokuo-node-worker-threads-pool.js' \ + 'node dynamic-suchmokuo-node-worker-threads-pool.js' \ 'node dynamic-poolifier.js' \ - 'node piscina.js' - -# export POOL_SIZE=8 -# export NUM_ITERATIONS=50000 -# hyperfine --export-markdown BENCH-50000.MD --min-runs 10 \ -# 'node dynamic-poolifier.js' \ -# 'node dynamic-suchmokuo-node-worker-threads-pool.js' \ -# 'node fixed-poolifier.js' \ -# 'node static-suchmokuo-node-worker-threads-pool.js' \ -# 'node piscina.js' - -# export NUM_ITERATIONS=100000 -# hyperfine --export-markdown BENCH-50000.MD --min-runs 20 \ -# 'node dynamic-poolifier.js' \ -# 'node static-suchmokuo-node-worker-threads-pool.js' \ -# 'node piscina.js' - + 'node fixed-poolifier.js' \ + 'node dynamic-piscina.js' \ + 'node fixed-piscina.js' diff --git a/benchmarks/versus-external-pools/piscina.js b/benchmarks/versus-external-pools/dynamic-piscina.js similarity index 81% rename from benchmarks/versus-external-pools/piscina.js rename to benchmarks/versus-external-pools/dynamic-piscina.js index 297ea681..0881d1d0 100644 --- a/benchmarks/versus-external-pools/piscina.js +++ b/benchmarks/versus-external-pools/dynamic-piscina.js @@ -4,11 +4,12 @@ const Piscina = require('piscina') const size = process.env.POOL_SIZE const iterations = process.env.NUM_ITERATIONS const data = { - test: 'MYBENCH' + test: 'MYBENCH', + taskType: process.env['TASK_TYPE'] } const piscina = new Piscina({ - filename: './workers/piscina/json-stringify.worker.js', + filename: './workers/piscina/function-to-bench-worker.js', minThreads: Number(size), maxThreads: size * 3, idleTimeout: 1000 * 60 // this is the same as poolifier default diff --git a/benchmarks/versus-external-pools/dynamic-poolifier.js b/benchmarks/versus-external-pools/dynamic-poolifier.js index e4f5f132..6a7406bc 100644 --- a/benchmarks/versus-external-pools/dynamic-poolifier.js +++ b/benchmarks/versus-external-pools/dynamic-poolifier.js @@ -4,13 +4,14 @@ const { FixedThreadPool, DynamicThreadPool } = require('poolifier') const size = process.env.POOL_SIZE const iterations = process.env.NUM_ITERATIONS const data = { - test: 'MYBENCH' + test: 'MYBENCH', + taskType: process.env['TASK_TYPE'] } const dynamicPool = new DynamicThreadPool( size, size * 3, - './workers/poolifier/json-stringify.worker.js', + './workers/poolifier/function-to-bench-worker.js', { maxTasks: 10000 } 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 8bf964d2..c183e536 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 @@ -2,12 +2,13 @@ const { DynamicPool, StaticPool } = require('node-worker-threads-pool') // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH -const functionToBench = require('./functions/json-stringify') +const functionToBench = require('./functions/function-to-bench') // FINISH IMPORT FUNCTION TO BENCH const size = process.env.POOL_SIZE const iterations = process.env.NUM_ITERATIONS const data = { - test: 'MYBENCH' + test: 'MYBENCH', + taskType: process.env['TASK_TYPE'] } const pool = new DynamicPool(Number(size)) diff --git a/benchmarks/versus-external-pools/fixed-piscina.js b/benchmarks/versus-external-pools/fixed-piscina.js new file mode 100644 index 00000000..d0164444 --- /dev/null +++ b/benchmarks/versus-external-pools/fixed-piscina.js @@ -0,0 +1,26 @@ +// IMPORT LIBRARIES +const Piscina = require('piscina') +// FINISH IMPORT LIBRARIES +const size = process.env.POOL_SIZE +const iterations = process.env.NUM_ITERATIONS +const data = { + test: 'MYBENCH', + taskType: process.env['TASK_TYPE'] +} + +const piscina = new Piscina({ + filename: './workers/piscina/function-to-bench-worker.js', + minThreads: Number(size), + idleTimeout: 1000 * 60 // this is the same as poolifier default +}) + +async function run () { + const promises = [] + for (let i = 0; i < iterations; i++) { + promises.push(piscina.runTask(data)) + } + 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 b62cd593..c9946208 100644 --- a/benchmarks/versus-external-pools/fixed-poolifier.js +++ b/benchmarks/versus-external-pools/fixed-poolifier.js @@ -4,12 +4,13 @@ const { FixedThreadPool, DynamicThreadPool } = require('poolifier') const size = process.env.POOL_SIZE const iterations = process.env.NUM_ITERATIONS const data = { - test: 'MYBENCH' + test: 'MYBENCH', + taskType: process.env['TASK_TYPE'] } const fixedPool = new FixedThreadPool( size, - './workers/poolifier/json-stringify.worker.js', + './workers/poolifier/function-to-bench-worker.js', { maxTasks: 100000 } diff --git a/benchmarks/versus-external-pools/functions/function-to-bench.js b/benchmarks/versus-external-pools/functions/function-to-bench.js new file mode 100644 index 00000000..0c9738e6 --- /dev/null +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -0,0 +1,14 @@ +module.exports = function (data) { + if ( data.taskType === 'CPU_INTENSIVE' ) { + // CPU Intensive task + for (let i = 0; i <= 5000; i++) { + const o = { + a: i + } + JSON.stringify(o) + } + return { ok: 1 } + } else { + throw new Error('Please specify the task type') + } +} diff --git a/benchmarks/versus-external-pools/functions/json-stringify.js b/benchmarks/versus-external-pools/functions/json-stringify.js deleted file mode 100644 index 4b009103..00000000 --- a/benchmarks/versus-external-pools/functions/json-stringify.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = function (data) { - for (let i = 0; i <= 5000; i++) { - const o = { - a: i - } - JSON.stringify(o) - } - // console.log('STRINGIFY FUNCTION FINISHED') - return { ok: 1 } -} diff --git a/benchmarks/versus-external-pools/package-lock.json b/benchmarks/versus-external-pools/package-lock.json index 11c2250f..d7e702d5 100644 --- a/benchmarks/versus-external-pools/package-lock.json +++ b/benchmarks/versus-external-pools/package-lock.json @@ -155,9 +155,9 @@ "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==" }, "poolifier": { - "version": "2.0.0-beta.6", - "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.0.0-beta.6.tgz", - "integrity": "sha512-n+IumaVITBY1/UD4gC4e6uXHlBIgy2+AKeX4BmhEP4phIM6DCYBYBRuVk5lk8LlXKiO8HFrQ3WXHzCpubqWyqA==" + "version": "2.0.0-beta.7", + "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.0.0-beta.7.tgz", + "integrity": "sha512-ZGrwPJdCRlQu3oIIAZBZQgrjOPzLUU+GNpPP4KpvVplL+XeDYHzIqtiss9M/3rYgmFuus6DiUd5JFWqnb5dtLg==" }, "symbol-observable": { "version": "1.2.0", 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 df7edd81..fa151d58 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 @@ -2,12 +2,13 @@ const { DynamicPool, StaticPool } = require('node-worker-threads-pool') // FINISH IMPORT LIBRARIES // IMPORT FUNCTION TO BENCH -const functionToBench = require('./functions/json-stringify') +const functionToBench = require('./functions/function-to-bench') // FINISH IMPORT FUNCTION TO BENCH const size = process.env.POOL_SIZE const iterations = process.env.NUM_ITERATIONS const data = { - test: 'MYBENCH' + test: 'MYBENCH', + taskType: process.env['TASK_TYPE'] } const pool = new StaticPool({ diff --git a/benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.js b/benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.js new file mode 100644 index 00000000..9fa8b472 --- /dev/null +++ b/benchmarks/versus-external-pools/workers/piscina/function-to-bench-worker.js @@ -0,0 +1,3 @@ +'use strict' +const functionToBench = require('../../functions/function-to-bench') +module.exports = functionToBench diff --git a/benchmarks/versus-external-pools/workers/piscina/json-stringify.worker.js b/benchmarks/versus-external-pools/workers/piscina/json-stringify.worker.js deleted file mode 100644 index 154e04a7..00000000 --- a/benchmarks/versus-external-pools/workers/piscina/json-stringify.worker.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict' -const jsonStringify = require('../../functions/json-stringify') -module.exports = jsonStringify diff --git a/benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.js b/benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.js new file mode 100644 index 00000000..99af6ea6 --- /dev/null +++ b/benchmarks/versus-external-pools/workers/poolifier/function-to-bench-worker.js @@ -0,0 +1,4 @@ +'use strict' +const { ThreadWorker } = require('poolifier') +const functionToBench = require('../../functions/function-to-bench') +module.exports = new ThreadWorker(functionToBench) diff --git a/benchmarks/versus-external-pools/workers/poolifier/json-stringify.worker.js b/benchmarks/versus-external-pools/workers/poolifier/json-stringify.worker.js deleted file mode 100644 index 73d08855..00000000 --- a/benchmarks/versus-external-pools/workers/poolifier/json-stringify.worker.js +++ /dev/null @@ -1,4 +0,0 @@ -'use strict' -const { ThreadWorker } = require('poolifier') -const jsonStringify = require('../../functions/json-stringify') -module.exports = new ThreadWorker(jsonStringify) -- 2.34.1