X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fversus-external-pools%2Ffunctions%2Ffunction-to-bench.js;h=89e9e2acae8c9d4fc09125fdde58090a50a1f4f7;hb=b4e757784e2b9ffe2c22b65b21e8bf665abd9f56;hp=b9054db22654b51d16ade84f9adf4c62aca192f4;hpb=6c06373320e61af1078a265851a15711a97b08af;p=poolifier.git diff --git a/benchmarks/versus-external-pools/functions/function-to-bench.js b/benchmarks/versus-external-pools/functions/function-to-bench.js index b9054db2..89e9e2ac 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,9 +1,22 @@ -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 || 'CPU_INTENSIVE' + data.taskType = data.taskType || TaskTypes.CPU_INTENSIVE data.taskSize = data.taskSize || 5000 + const baseDirectory = '/tmp/poolifier-benchmarks' switch (data.taskType) { - case 'CPU_INTENSIVE': + case TaskTypes.CPU_INTENSIVE: // CPU intensive task for (let i = 0; i < data.taskSize; i++) { const o = { @@ -12,7 +25,23 @@ module.exports = function (data) { 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++) { + 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