From 79e7d3680bb510d4c5419701de6638fb19816c2c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 19 Mar 2021 21:41:11 +0100 Subject: [PATCH] Add worker-threads-pool to external pools benchmarks (#281) --- benchmarks/README.md | 1 + benchmarks/versus-external-pools/bench.sh | 3 +- .../versus-external-pools/dynamic-piscina.js | 6 ++-- .../dynamic-poolifier.js | 6 ++-- ...amic-suchmokuo-node-worker-threads-pool.js | 6 ++-- .../dynamic-workerpool.js | 6 ++-- .../versus-external-pools/fixed-microjob.js | 6 ++-- .../versus-external-pools/fixed-piscina.js | 6 ++-- .../versus-external-pools/fixed-poolifier.js | 6 ++-- .../versus-external-pools/fixed-threadwork.js | 4 +-- .../fixed-worker-threads-pool.js | 29 +++++++++++++++++++ .../versus-external-pools/fixed-workerpool.js | 8 ++--- .../versus-external-pools/package-lock.json | 6 ++-- benchmarks/versus-external-pools/package.json | 2 +- ...atic-suchmokuo-node-worker-threads-pool.js | 6 ++-- benchmarks/versus-external-pools/threadjs.js | 4 +-- .../function-to-bench-worker.js | 4 +++ package-lock.json | 1 + 18 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 benchmarks/versus-external-pools/fixed-worker-threads-pool.js create mode 100644 benchmarks/versus-external-pools/workers/worker-threads-pool/function-to-bench-worker.js diff --git a/benchmarks/README.md b/benchmarks/README.md index bdec9bf2..4059359a 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -19,6 +19,7 @@ External pools with which we compared the poolifier results: - [workerpool](https://github.com/josdejong/workerpool) - [threadwork](https://github.com/kevlened/threadwork) - [microjob](https://github.com/wilk/microjob) +- [worker-threads-pool](https://github.com/watson/worker-threads-pool) Those are our results: diff --git a/benchmarks/versus-external-pools/bench.sh b/benchmarks/versus-external-pools/bench.sh index d3f51216..94c5a3b5 100755 --- a/benchmarks/versus-external-pools/bench.sh +++ b/benchmarks/versus-external-pools/bench.sh @@ -27,4 +27,5 @@ hyperfine --export-markdown BENCH-100000.md --min-runs 10 \ 'node dynamic-workerpool.js' \ 'node fixed-workerpool.js' \ 'node fixed-threadwork.js' \ - 'node fixed-microjob.js' + 'node fixed-microjob.js' \ + 'node fixed-worker-threads-pool.js' diff --git a/benchmarks/versus-external-pools/dynamic-piscina.js b/benchmarks/versus-external-pools/dynamic-piscina.js index 0881d1d0..d78dd568 100644 --- a/benchmarks/versus-external-pools/dynamic-piscina.js +++ b/benchmarks/versus-external-pools/dynamic-piscina.js @@ -1,8 +1,8 @@ // IMPORT LIBRARIES const Piscina = require('piscina') // FINISH IMPORT LIBRARIES -const size = process.env.POOL_SIZE -const iterations = process.env.NUM_ITERATIONS +const size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] @@ -10,7 +10,7 @@ const data = { const piscina = new Piscina({ filename: './workers/piscina/function-to-bench-worker.js', - minThreads: Number(size), + minThreads: 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 b20082a3..f466d28a 100644 --- a/benchmarks/versus-external-pools/dynamic-poolifier.js +++ b/benchmarks/versus-external-pools/dynamic-poolifier.js @@ -1,15 +1,15 @@ // IMPORT LIBRARIES const { DynamicThreadPool } = require('poolifier') // FINISH IMPORT LIBRARIES -const size = process.env.POOL_SIZE -const iterations = process.env.NUM_ITERATIONS +const size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] } const dynamicPool = new DynamicThreadPool( - Number(size), + size, size * 3, './workers/poolifier/function-to-bench-worker.js' ) 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 a418bcfc..fd0b7bd1 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 @@ -4,14 +4,14 @@ const { DynamicPool } = require('node-worker-threads-pool') // IMPORT FUNCTION TO BENCH 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 size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] } -const pool = new DynamicPool(Number(size)) +const pool = new DynamicPool(size) async function run () { const promises = [] diff --git a/benchmarks/versus-external-pools/dynamic-workerpool.js b/benchmarks/versus-external-pools/dynamic-workerpool.js index 557e45e6..9b926936 100644 --- a/benchmarks/versus-external-pools/dynamic-workerpool.js +++ b/benchmarks/versus-external-pools/dynamic-workerpool.js @@ -1,14 +1,14 @@ // IMPORT LIBRARIES const workerpool = require('workerpool') // FINISH IMPORT LIBRARIES -const size = process.env.POOL_SIZE -const iterations = process.env.NUM_ITERATIONS +const size = Number(process.env.POOL_SIZE) +const iterations = Number(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), + minWorkers: size, maxWorkers: size * 3, workerType: 'thread' } diff --git a/benchmarks/versus-external-pools/fixed-microjob.js b/benchmarks/versus-external-pools/fixed-microjob.js index 4bc64da9..287d8a6f 100644 --- a/benchmarks/versus-external-pools/fixed-microjob.js +++ b/benchmarks/versus-external-pools/fixed-microjob.js @@ -4,15 +4,15 @@ const { job, start } = require('microjob') // IMPORT FUNCTION TO BENCH 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 size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] } async function run () { - await start({ maxWorkers: Number(size) }) + await start({ maxWorkers: size }) const promises = [] for (let i = 0; i < iterations; i++) { promises.push( diff --git a/benchmarks/versus-external-pools/fixed-piscina.js b/benchmarks/versus-external-pools/fixed-piscina.js index d0164444..d1f785ed 100644 --- a/benchmarks/versus-external-pools/fixed-piscina.js +++ b/benchmarks/versus-external-pools/fixed-piscina.js @@ -1,8 +1,8 @@ // IMPORT LIBRARIES const Piscina = require('piscina') // FINISH IMPORT LIBRARIES -const size = process.env.POOL_SIZE -const iterations = process.env.NUM_ITERATIONS +const size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] @@ -10,7 +10,7 @@ const data = { const piscina = new Piscina({ filename: './workers/piscina/function-to-bench-worker.js', - minThreads: Number(size), + minThreads: size, idleTimeout: 1000 * 60 // this is the same as poolifier default }) diff --git a/benchmarks/versus-external-pools/fixed-poolifier.js b/benchmarks/versus-external-pools/fixed-poolifier.js index f4573435..28aa7e3a 100644 --- a/benchmarks/versus-external-pools/fixed-poolifier.js +++ b/benchmarks/versus-external-pools/fixed-poolifier.js @@ -1,15 +1,15 @@ // IMPORT LIBRARIES const { FixedThreadPool } = require('poolifier') // FINISH IMPORT LIBRARIES -const size = process.env.POOL_SIZE -const iterations = process.env.NUM_ITERATIONS +const size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] } const fixedPool = new FixedThreadPool( - Number(size), + size, './workers/poolifier/function-to-bench-worker.js' ) diff --git a/benchmarks/versus-external-pools/fixed-threadwork.js b/benchmarks/versus-external-pools/fixed-threadwork.js index dfba6c00..fcc370ff 100644 --- a/benchmarks/versus-external-pools/fixed-threadwork.js +++ b/benchmarks/versus-external-pools/fixed-threadwork.js @@ -4,8 +4,8 @@ const { ThreadPool } = require('threadwork') // IMPORT FUNCTION TO BENCH 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 size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] diff --git a/benchmarks/versus-external-pools/fixed-worker-threads-pool.js b/benchmarks/versus-external-pools/fixed-worker-threads-pool.js new file mode 100644 index 00000000..ed7e2ae1 --- /dev/null +++ b/benchmarks/versus-external-pools/fixed-worker-threads-pool.js @@ -0,0 +1,29 @@ +// IMPORT LIBRARIES +const Pool = require('worker-threads-pool') +// FINISH IMPORT LIBRARIES +const size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) +const data = { + test: 'MYBENCH', + taskType: process.env['TASK_TYPE'] +} + +const pool = new Pool({ max: size }) + +async function run () { + const promises = [] + for (let i = 0; i < iterations; i++) { + promises.push( + pool.acquire( + './workers/worker-threads-pool/function-to-bench-worker.js', + { + workerData: data + } + ) + ) + } + await Promise.all(promises) + process.exit() +} + +run() diff --git a/benchmarks/versus-external-pools/fixed-workerpool.js b/benchmarks/versus-external-pools/fixed-workerpool.js index 5807f67c..cf60a366 100644 --- a/benchmarks/versus-external-pools/fixed-workerpool.js +++ b/benchmarks/versus-external-pools/fixed-workerpool.js @@ -1,15 +1,15 @@ // IMPORT LIBRARIES const workerpool = require('workerpool') // FINISH IMPORT LIBRARIES -const size = process.env.POOL_SIZE -const iterations = process.env.NUM_ITERATIONS +const size = Number(process.env.POOL_SIZE) +const iterations = Number(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), + minWorkers: size, + maxWorkers: size, workerType: 'thread' } ) diff --git a/benchmarks/versus-external-pools/package-lock.json b/benchmarks/versus-external-pools/package-lock.json index 9877bf75..43eb15f3 100644 --- a/benchmarks/versus-external-pools/package-lock.json +++ b/benchmarks/versus-external-pools/package-lock.json @@ -182,9 +182,9 @@ } }, "poolifier": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.0.0.tgz", - "integrity": "sha512-17gRE5OJJvTusOdLzykjrNkFoZFUu2GLKXFrqMqx2EhKmIRS1XzVej+LdhUJn+sdsAap/RQDXVW2u0GcSMSuQQ==" + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.0.1.tgz", + "integrity": "sha512-klkLv35QVumXKrQj6Totvwqb0lw34j7mIsEm8uZXXHlcpVCvnhhgVFVyuZUOSNTjZnBCaiB0KcLos03cqj38CA==" }, "symbol-observable": { "version": "1.2.0", diff --git a/benchmarks/versus-external-pools/package.json b/benchmarks/versus-external-pools/package.json index 40d74149..0a0166b8 100644 --- a/benchmarks/versus-external-pools/package.json +++ b/benchmarks/versus-external-pools/package.json @@ -12,7 +12,7 @@ "microjob": "0.7.0", "node-worker-threads-pool": "1.4.3", "piscina": "2.2.0", - "poolifier": "2.0.0", + "poolifier": "2.0.1", "threads": "1.6.3", "threadwork": "0.6.0", "worker-threads-pool": "2.0.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 17543d4b..ded55b88 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 @@ -4,15 +4,15 @@ const { StaticPool } = require('node-worker-threads-pool') // IMPORT FUNCTION TO BENCH 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 size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] } const pool = new StaticPool({ - size: Number(size), + size: size, task: functionToBench }) diff --git a/benchmarks/versus-external-pools/threadjs.js b/benchmarks/versus-external-pools/threadjs.js index df5c087f..2cc96d57 100644 --- a/benchmarks/versus-external-pools/threadjs.js +++ b/benchmarks/versus-external-pools/threadjs.js @@ -1,8 +1,8 @@ // IMPORT LIBRARIES const { spawn, Thread, Worker } = require('threads') // FINISH IMPORT LIBRARIES -const size = process.env.POOL_SIZE -const iterations = process.env.NUM_ITERATIONS +const size = Number(process.env.POOL_SIZE) +const iterations = Number(process.env.NUM_ITERATIONS) const data = { test: 'MYBENCH', taskType: process.env['TASK_TYPE'] diff --git a/benchmarks/versus-external-pools/workers/worker-threads-pool/function-to-bench-worker.js b/benchmarks/versus-external-pools/workers/worker-threads-pool/function-to-bench-worker.js new file mode 100644 index 00000000..202eeb3d --- /dev/null +++ b/benchmarks/versus-external-pools/workers/worker-threads-pool/function-to-bench-worker.js @@ -0,0 +1,4 @@ +'use strict' +const { workerData } = require('worker_threads') +const functionToBench = require('../../functions/function-to-bench') +functionToBench(workerData) diff --git a/package-lock.json b/package-lock.json index 36cd3d91..aab663c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1086,6 +1086,7 @@ "requires": { "anymatch": "~3.1.1", "braces": "~3.0.2", + "fsevents": "~2.3.1", "glob-parent": "~5.1.0", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", -- 2.34.1