X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fbenchmarks-utils.js;h=743936dabdbef3b1963fb63ce092b28881226d9b;hb=dab8c377b70fc962ec217f2aeb719842f9f94cd6;hp=e6abe96d5b1e065c07b709bd83c37095e9996935;hpb=965415bb43f5c76329f0854ed680e22068e3affb;p=poolifier.git diff --git a/benchmarks/benchmarks-utils.js b/benchmarks/benchmarks-utils.js index e6abe96d..743936da 100644 --- a/benchmarks/benchmarks-utils.js +++ b/benchmarks/benchmarks-utils.js @@ -1,14 +1,26 @@ -const { WorkerFunctions } = require('./benchmarks-types') +const crypto = require('crypto') +const fs = require('fs') +const { + DynamicClusterPool, + DynamicThreadPool, + FixedClusterPool, + FixedThreadPool +} = require('../lib') +const { + PoolTypes, + WorkerFunctions, + WorkerTypes +} = require('./benchmarks-types') -async function runPoolifierTest (pool, { tasks, workerData }) { +async function runTest (pool, { taskExecutions, workerData }) { return new Promise((resolve, reject) => { let executions = 0 - for (let i = 1; i <= tasks; i++) { + for (let i = 1; i <= taskExecutions; i++) { pool .execute(workerData) .then(() => { - executions++ - if (executions === tasks) { + ++executions + if (executions === taskExecutions) { return resolve({ ok: 1 }) } return null @@ -21,15 +33,6 @@ async function runPoolifierTest (pool, { tasks, workerData }) { }) } -function jsonIntegerSerialization (n) { - for (let i = 0; i < n; i++) { - const o = { - a: i - } - JSON.stringify(o) - } -} - function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { if (max < min || max < 0 || min < 0) { throw new RangeError('Invalid interval') @@ -42,9 +45,17 @@ function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { return Math.floor(Math.random() * (max + 1)) } +function jsonIntegerSerialization (n) { + for (let i = 0; i < n; i++) { + const o = { + a: i + } + JSON.stringify(o) + } +} + /** * Intentionally inefficient implementation. - * * @param {number} n - The number of fibonacci numbers to generate. * @returns {number} - The nth fibonacci number. */ @@ -55,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. */ @@ -66,6 +76,27 @@ function factorial (n) { return factorial(n - 1) * n } +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(), { + encoding: 'utf8', + flag: 'a' + }) + fs.readFileSync(filePath, 'utf8') + } + fs.rmSync(baseDirectory, { recursive: true }) +} + function executeWorkerFunction (data) { switch (data.function) { case WorkerFunctions.jsonIntegerSerialization: @@ -74,14 +105,57 @@ function executeWorkerFunction (data) { return fibonacci(data.taskSize || 1000) case WorkerFunctions.factorial: return factorial(data.taskSize || 1000) + case WorkerFunctions.readWriteFiles: + return readWriteFiles(data.taskSize || 1000) default: throw new Error('Unknown worker function') } } +function buildPool (workerType, poolType, poolSize, poolOptions) { + switch (poolType) { + case PoolTypes.FIXED: + switch (workerType) { + case WorkerTypes.THREAD: + return new FixedThreadPool( + poolSize, + './benchmarks/internal/thread-worker.js', + poolOptions + ) + case WorkerTypes.CLUSTER: + return new FixedClusterPool( + poolSize, + './benchmarks/internal/cluster-worker.js', + poolOptions + ) + } + break + case PoolTypes.DYNAMIC: + switch (workerType) { + case WorkerTypes.THREAD: + return new DynamicThreadPool( + poolSize / 2, + poolSize * 3, + './benchmarks/internal/thread-worker.js', + poolOptions + ) + case WorkerTypes.CLUSTER: + return new DynamicClusterPool( + poolSize / 2, + poolSize * 3, + './benchmarks/internal/cluster-worker.js', + poolOptions + ) + } + break + } +} + module.exports = { WorkerFunctions, + buildPool, executeWorkerFunction, generateRandomInteger, - runPoolifierTest + readWriteFiles, + runTest }