chore: v2.4.12
[poolifier.git] / benchmarks / benchmarks-utils.js
index 50dcc40c6edaff1a45d885c7c0dd5d531efe3ae6..743936dabdbef3b1963fb63ce092b28881226d9b 100644 (file)
@@ -1,15 +1,16 @@
+const crypto = require('crypto')
 const fs = require('fs')
-const {
-  PoolTypes,
-  WorkerFunctions,
-  WorkerTypes
-} = require('./benchmarks-types')
 const {
   DynamicClusterPool,
   DynamicThreadPool,
   FixedClusterPool,
   FixedThreadPool
 } = require('../lib')
+const {
+  PoolTypes,
+  WorkerFunctions,
+  WorkerTypes
+} = require('./benchmarks-types')
 
 async function runTest (pool, { taskExecutions, workerData }) {
   return new Promise((resolve, reject) => {
@@ -55,7 +56,6 @@ function jsonIntegerSerialization (n) {
 
 /**
  * Intentionally inefficient implementation.
- *
  * @param {number} n - The number of fibonacci numbers to generate.
  * @returns {number} - The nth fibonacci number.
  */
@@ -66,7 +66,6 @@ function fibonacci (n) {
 
 /**
  * Intentionally inefficient implementation.
- *
  * @param {number} n - The number to calculate the factorial of.
  * @returns {number} - The factorial of n.
  */
@@ -77,11 +76,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 +94,7 @@ function readWriteFiles (n) {
     })
     fs.readFileSync(filePath, 'utf8')
   }
+  fs.rmSync(baseDirectory, { recursive: true })
 }
 
 function executeWorkerFunction (data) {
@@ -107,7 +112,7 @@ function executeWorkerFunction (data) {
   }
 }
 
-function buildPool (poolType, poolSize, workerType, poolOptions) {
+function buildPool (workerType, poolType, poolSize, poolOptions) {
   switch (poolType) {
     case PoolTypes.FIXED:
       switch (workerType) {