From e4bc7a498d48e2773b2e68fd4ffcaf4d328d5a0b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 20 Oct 2022 21:30:03 +0200 Subject: [PATCH] Benchmarks: fix workload function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .eslintrc.js | 1 + benchmarks/benchmarks-types.js | 7 +++ benchmarks/benchmarks-utils.js | 6 +-- benchmarks/internal/cluster/worker.js | 6 +-- benchmarks/internal/thread/worker.js | 6 +-- .../functions/function-to-bench.js | 50 ++++++++++++------- .../versus-external-pools/package-lock.json | 14 +++--- benchmarks/versus-external-pools/package.json | 2 +- 8 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 benchmarks/benchmarks-types.js diff --git a/.eslintrc.js b/.eslintrc.js index 419b7e67..59f0a113 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -36,6 +36,7 @@ module.exports = defineConfig({ { skipWords: [ 'browserslist', + 'builtins', 'christopher', 'cjs', 'comparator', diff --git a/benchmarks/benchmarks-types.js b/benchmarks/benchmarks-types.js new file mode 100644 index 00000000..91380343 --- /dev/null +++ b/benchmarks/benchmarks-types.js @@ -0,0 +1,7 @@ +const WorkerFunctions = { + jsonIntegerSerialization: 'jsonIntegerSerialization', + fibonacci: 'fibonacci', + factorial: 'factorial' +} + +module.exports = { WorkerFunctions } diff --git a/benchmarks/benchmarks-utils.js b/benchmarks/benchmarks-utils.js index 4e3cdc9c..30af25c9 100644 --- a/benchmarks/benchmarks-utils.js +++ b/benchmarks/benchmarks-utils.js @@ -1,8 +1,4 @@ -const WorkerFunctions = { - jsonIntegerSerialization: 'jsonIntegerSerialization', - fibonacci: 'fibonacci', - factorial: 'factorial' -} +const { WorkerFunctions } = require('./benchmarks-types') async function runPoolifierTest (pool, { tasks, workerData }) { return new Promise((resolve, reject) => { diff --git a/benchmarks/internal/cluster/worker.js b/benchmarks/internal/cluster/worker.js index c64363de..926278be 100644 --- a/benchmarks/internal/cluster/worker.js +++ b/benchmarks/internal/cluster/worker.js @@ -1,10 +1,8 @@ 'use strict' const { isMaster } = require('cluster') const { ClusterWorker } = require('../../../lib/index') -const { - WorkerFunctions, - executeWorkerFunction -} = require('../../benchmarks-utils') +const { executeWorkerFunction } = require('../../benchmarks-utils') +const { WorkerFunctions } = require('../../benchmarks-types') const debug = false diff --git a/benchmarks/internal/thread/worker.js b/benchmarks/internal/thread/worker.js index e84711ce..09060eba 100644 --- a/benchmarks/internal/thread/worker.js +++ b/benchmarks/internal/thread/worker.js @@ -1,10 +1,8 @@ 'use strict' const { isMainThread } = require('worker_threads') const { ThreadWorker } = require('../../../lib/index') -const { - WorkerFunctions, - executeWorkerFunction -} = require('../../benchmarks-utils') +const { executeWorkerFunction } = require('../../benchmarks-utils') +const { WorkerFunctions } = require('../../benchmarks-types') const debug = false diff --git a/benchmarks/versus-external-pools/functions/function-to-bench.js b/benchmarks/versus-external-pools/functions/function-to-bench.js index 2f75d143..89e9e2ac 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,35 +1,47 @@ -const fs = require('fs') -const { - WorkerFunctions, - executeWorkerFunction - // eslint-disable-next-line n/no-unpublished-require -} = require('../../benchmarks-utils') - -const TaskTypes = { - CPU_INTENSIVE: 'CPU_INTENSIVE', - IO_INTENSIVE: 'IO_INTENSIVE' -} - -module.exports = function (data) { +/** + * 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. + */ +function functionToBench (data) { + const fs = require('fs') + const TaskTypes = { + CPU_INTENSIVE: 'CPU_INTENSIVE', + IO_INTENSIVE: 'IO_INTENSIVE' + } data = data || {} data.taskType = data.taskType || TaskTypes.CPU_INTENSIVE data.taskSize = data.taskSize || 5000 - const benchmarksFilePath = '/tmp/poolifier-benchmarks' + const baseDirectory = '/tmp/poolifier-benchmarks' switch (data.taskType) { case TaskTypes.CPU_INTENSIVE: // CPU intensive task - data.function = data.function || WorkerFunctions.jsonIntegerSerialization - executeWorkerFunction(data) + for (let i = 0; i < data.taskSize; i++) { + const o = { + a: i + } + JSON.stringify(o) + } return { ok: 1 } case TaskTypes.IO_INTENSIVE: // IO intensive task + if (fs.existsSync(baseDirectory) === false) { + fs.mkdirSync(baseDirectory, { recursive: true }) + } for (let i = 0; i < data.taskSize; i++) { - fs.writeFileSync(benchmarksFilePath, i.toString(), 'utf8') - fs.readFileSync(benchmarksFilePath, 'utf8') - fs.unlinkSync(benchmarksFilePath) + const filePath = `${baseDirectory}/${i}` + fs.writeFileSync(filePath, i.toString(), { + encoding: 'utf8', + flag: 'a' + }) + fs.readFileSync(filePath, 'utf8') } return { ok: 1 } default: throw new Error(`Unknown task type: ${data.taskType}`) } } + +module.exports = functionToBench diff --git a/benchmarks/versus-external-pools/package-lock.json b/benchmarks/versus-external-pools/package-lock.json index 77e7497e..3a506b6e 100644 --- a/benchmarks/versus-external-pools/package-lock.json +++ b/benchmarks/versus-external-pools/package-lock.json @@ -11,7 +11,7 @@ "microjob": "0.7.0", "node-worker-threads-pool": "1.5.1", "piscina": "3.2.0", - "poolifier": "2.3.3", + "poolifier": "2.3.4", "threads": "1.7.0", "threadwork": "0.6.0", "worker-nodes": "2.4.0", @@ -256,9 +256,9 @@ } }, "node_modules/poolifier": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.3.3.tgz", - "integrity": "sha512-mxkMpqPMHtdGeJfBe7iXRkIlcXJNYNuCPCw0sLhch8k7sFGGk0pby5eToGVwBx6Yxc0o7aCR5JU1blnFLl8Igg==", + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.3.4.tgz", + "integrity": "sha512-kb5VsjkMxLPJagJmwnlv5bSe87O2V3hIPLAJLvmDlMoo9+2bqFLxCsxz+suC+sQyHxiLw97QL6pR1+oBVAq2Lg==", "engines": { "node": ">=16.0.0", "npm": ">=8.0.0" @@ -487,9 +487,9 @@ } }, "poolifier": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.3.3.tgz", - "integrity": "sha512-mxkMpqPMHtdGeJfBe7iXRkIlcXJNYNuCPCw0sLhch8k7sFGGk0pby5eToGVwBx6Yxc0o7aCR5JU1blnFLl8Igg==" + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/poolifier/-/poolifier-2.3.4.tgz", + "integrity": "sha512-kb5VsjkMxLPJagJmwnlv5bSe87O2V3hIPLAJLvmDlMoo9+2bqFLxCsxz+suC+sQyHxiLw97QL6pR1+oBVAq2Lg==" }, "threads": { "version": "1.7.0", diff --git a/benchmarks/versus-external-pools/package.json b/benchmarks/versus-external-pools/package.json index 10e566b2..b51245cd 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.5.1", "piscina": "3.2.0", - "poolifier": "2.3.3", + "poolifier": "2.3.4", "threads": "1.7.0", "threadwork": "0.6.0", "worker-nodes": "2.4.0", -- 2.34.1