X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fversus-external-pools%2Ffunctions%2Ffunction-to-bench.js;h=ff13bc9ecd2ad04fb034bab8a622198ad748e10a;hb=4a2cbff6a253589b5ceec0f0fde0ff4f5956a716;hp=89e9e2acae8c9d4fc09125fdde58090a50a1f4f7;hpb=e4bc7a498d48e2773b2e68fd4ffcaf4d328d5a0b;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 89e9e2ac..ff13bc9e 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,12 +1,13 @@ +'use strict' /** - * The worker function to execute during pools benchmarks. - * NOTE: This function requires to be self-contained, thread-safe and re-entrant. - * + * 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. */ -function functionToBench (data) { - const fs = require('fs') +const functionToBench = data => { + const crypto = require('node:crypto') + const fs = require('node:fs') const TaskTypes = { CPU_INTENSIVE: 'CPU_INTENSIVE', IO_INTENSIVE: 'IO_INTENSIVE' @@ -14,7 +15,9 @@ function functionToBench (data) { data = data || {} data.taskType = data.taskType || TaskTypes.CPU_INTENSIVE data.taskSize = data.taskSize || 5000 - const baseDirectory = '/tmp/poolifier-benchmarks' + const baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt( + 281474976710655 + )}` switch (data.taskType) { case TaskTypes.CPU_INTENSIVE: // CPU intensive task @@ -27,9 +30,10 @@ function functionToBench (data) { return { ok: 1 } case TaskTypes.IO_INTENSIVE: // IO intensive task - if (fs.existsSync(baseDirectory) === false) { - fs.mkdirSync(baseDirectory, { recursive: true }) + 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(), { @@ -38,6 +42,7 @@ function functionToBench (data) { }) fs.readFileSync(filePath, 'utf8') } + fs.rmSync(baseDirectory, { recursive: true }) return { ok: 1 } default: throw new Error(`Unknown task type: ${data.taskType}`)