X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fversus-external-pools%2Ffunctions%2Ffunction-to-bench.js;h=6a05cb629ad7bdcc816d393215bb6ae5d80a48d5;hb=refs%2Ftags%2Fv2.6.10;hp=b3e19fe95f81c76edec864b47812a4408b729a6c;hpb=8c97ee641800a73d02516dad3e6b5172ec477e2d;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 b3e19fe9..6a05cb62 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,15 +1,23 @@ -const fs = require('fs') - -const TaskTypes = { - CPU_INTENSIVE: 'CPU_INTENSIVE', - IO_INTENSIVE: 'IO_INTENSIVE' -} - -module.exports = function (data) { +'use strict' +/** + * 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. + */ +module.exports = function functionToBench (data) { + const crypto = require('crypto') + 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/${crypto.randomInt( + 281474976710655 + )}` switch (data.taskType) { case TaskTypes.CPU_INTENSIVE: // CPU intensive task @@ -22,11 +30,19 @@ module.exports = function (data) { return { ok: 1 } case TaskTypes.IO_INTENSIVE: // IO intensive task + if (fs.existsSync(baseDirectory) === true) { + fs.rmSync(baseDirectory, { recursive: true }) + } + 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') } + fs.rmSync(baseDirectory, { recursive: true }) return { ok: 1 } default: throw new Error(`Unknown task type: ${data.taskType}`)