X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fversus-external-pools%2Ffunctions%2Ffunction-to-bench.js;h=eca756b715d3a0052c99db37aafe9cc6ab1e3492;hb=0628df39eb1eb95630d08d9759a83c750a34ff7e;hp=bbc17c18da0cdc29f59da0e2652cb8daaea452e7;hpb=d1a9aa414b60e38c91f5623f3572dc46c50f5f14;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 bbc17c18..eca756b7 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,35 +1,52 @@ -const fs = require('fs') -const { - WorkerFunctions, - executeWorkerFunction - // eslint-disable-next-line node/no-unpublished-require -} = require('../../benchmarks-utils') - -const TaskTypes = { - CPU_INTENSIVE: 'CPU_INTENSIVE', - IO_INTENSIVE: 'IO_INTENSIVE' -} - -module.exports = function (data) { +'use strict' +/** + * The task function to execute during pools benchmarks. + * NOTE: This function requires to be self-contained, thread-safe and re-entrant (node-worker-threads-pool requirement). + * @param {*} data The worker data. + * @returns {*} The result. + */ +const 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 - 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) === 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}`) } } + +module.exports = functionToBench