X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fversus-external-pools%2Ffunctions%2Ffunction-to-bench.js;h=89e9e2acae8c9d4fc09125fdde58090a50a1f4f7;hb=548140e67db5a4d10e7712fc0830c196a3f81968;hp=7ecc7e12b89059b3878f443c3f594cb5c69f1773;hpb=7a6a0a967f5d0978d7ad0714616194fe7592f69a;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 7ecc7e12..89e9e2ac 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,16 +1,20 @@ -const fs = require('fs') - -const TaskTypes = { - CPU_INTENSIVE: 'CPU_INTENSIVE', - IO_INTENSIVE: 'IO_INTENSIVE' -} - -module.exports = function (data) { - console.log('functionToBench', 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 @@ -23,13 +27,21 @@ module.exports = function (data) { 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