-const WorkerFunctions = {
- jsonIntegerSerialization: 'jsonIntegerSerialization',
- fibonacci: 'fibonacci',
- factorial: 'factorial'
-}
+const { WorkerFunctions } = require('./benchmarks-types')
async function runPoolifierTest (pool, { tasks, workerData }) {
return new Promise((resolve, reject) => {
-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
"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",
}
},
"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"
}
},
"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",