X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fversus-external-pools%2Ffunctions%2Ffunction-to-bench.js;h=6a05cb629ad7bdcc816d393215bb6ae5d80a48d5;hb=c4216735df38e44ec4f7aee04e18955adb919c3f;hp=48a0fc78c8851f6c31c3e87d7d7d35eb3b6e0dc2;hpb=144f78e0cf2579cbd0a05ea30bc95ac6b57050f3;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 48a0fc78..6a05cb62 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,14 +1,50 @@ -module.exports = function (data) { - if (data.taskType === 'CPU_INTENSIVE') { - // CPU intensive task - for (let i = 0; i <= 5000; i++) { - const o = { - a: i +'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 baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt( + 281474976710655 + )}` + switch (data.taskType) { + case TaskTypes.CPU_INTENSIVE: + // CPU intensive task + 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) === true) { + fs.rmSync(baseDirectory, { recursive: true }) + } + 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') } - JSON.stringify(o) - } - return { ok: 1 } - } else { - throw new Error('Please specify the task type') + fs.rmSync(baseDirectory, { recursive: true }) + return { ok: 1 } + default: + throw new Error(`Unknown task type: ${data.taskType}`) } }