From 670734fc5562e286ce17b7f3075d115832e1436a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 17 Apr 2023 00:50:28 +0200 Subject: [PATCH] feat: add disk i/o internal benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/benchmarks-utils.js | 15 +++++++++++---- .../functions/function-to-bench.js | 11 ++++++++--- benchmarks/versus-external-pools/package.json | 3 +++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/benchmarks/benchmarks-utils.js b/benchmarks/benchmarks-utils.js index 50dcc40c..64df58c8 100644 --- a/benchmarks/benchmarks-utils.js +++ b/benchmarks/benchmarks-utils.js @@ -1,3 +1,4 @@ +const crypto = require('crypto') const fs = require('fs') const { PoolTypes, @@ -77,11 +78,16 @@ function factorial (n) { return factorial(n - 1) * n } -function readWriteFiles (n) { - const baseDirectory = '/tmp/poolifier-benchmarks' - if (fs.existsSync(baseDirectory) === false) { - fs.mkdirSync(baseDirectory, { recursive: true }) +function readWriteFiles ( + n, + baseDirectory = `/tmp/poolifier-benchmarks/${crypto.randomInt( + 281474976710655 + )}` +) { + if (fs.existsSync(baseDirectory) === true) { + fs.rmSync(baseDirectory, { recursive: true }) } + fs.mkdirSync(baseDirectory, { recursive: true }) for (let i = 0; i < n; i++) { const filePath = `${baseDirectory}/${i}` fs.writeFileSync(filePath, i.toString(), { @@ -90,6 +96,7 @@ function readWriteFiles (n) { }) fs.readFileSync(filePath, 'utf8') } + fs.rmSync(baseDirectory, { recursive: true }) } function executeWorkerFunction (data) { diff --git a/benchmarks/versus-external-pools/functions/function-to-bench.js b/benchmarks/versus-external-pools/functions/function-to-bench.js index 89e9e2ac..3009054e 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -6,6 +6,7 @@ * @returns {*} The result. */ function functionToBench (data) { + const crypto = require('crypto') const fs = require('fs') const TaskTypes = { CPU_INTENSIVE: 'CPU_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}`) diff --git a/benchmarks/versus-external-pools/package.json b/benchmarks/versus-external-pools/package.json index fe8f8cf1..eda132c6 100644 --- a/benchmarks/versus-external-pools/package.json +++ b/benchmarks/versus-external-pools/package.json @@ -5,6 +5,9 @@ "private": true, "main": "index.js", "author": "pioardi", + "engines": { + "node": ">=14.14.0" + }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, -- 2.34.1