X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fbenchmarks-utils.mjs;h=e8c67583bf1958d7f2f0a2294bd2f1c399ca9b49;hb=1a880eca683648d5228e2424db74553f58ecee7d;hp=48d5e41b1f0138a589fe4dbee31a762ac92db845;hpb=02749105fad5c3de792acbe23ed8dae1915ba2aa;p=poolifier.git diff --git a/benchmarks/benchmarks-utils.mjs b/benchmarks/benchmarks-utils.mjs index 48d5e41b..e8c67583 100644 --- a/benchmarks/benchmarks-utils.mjs +++ b/benchmarks/benchmarks-utils.mjs @@ -1,12 +1,14 @@ -import crypto from 'crypto' -import fs from 'fs' +import crypto from 'node:crypto' +import fs from 'node:fs' import { DynamicClusterPool, DynamicThreadPool, FixedClusterPool, - FixedThreadPool + FixedThreadPool, + PoolTypes, + WorkerTypes } from '../lib/index.mjs' -import { PoolTypes, WorkerFunctions, WorkerTypes } from './benchmarks-types.mjs' +import { TaskFunctions } from './benchmarks-types.mjs' export const runTest = async (pool, { taskExecutions, workerData }) => { return new Promise((resolve, reject) => { @@ -21,7 +23,7 @@ export const runTest = async (pool, { taskExecutions, workerData }) => { } return null }) - .catch(err => { + .catch((err) => { console.error(err) return reject(err) }) @@ -44,13 +46,14 @@ export const generateRandomInteger = ( return Math.floor(Math.random() * (max + 1)) } -const jsonIntegerSerialization = n => { +const jsonIntegerSerialization = (n) => { for (let i = 0; i < n; i++) { const o = { a: i } JSON.stringify(o) } + return { ok: 1 } } /** @@ -58,7 +61,7 @@ const jsonIntegerSerialization = n => { * @param {number} n - The number of fibonacci numbers to generate. * @returns {number} - The nth fibonacci number. */ -const fibonacci = n => { +const fibonacci = (n) => { if (n <= 1) return n return fibonacci(n - 1) + fibonacci(n - 2) } @@ -68,7 +71,7 @@ const fibonacci = n => { * @param {number} n - The number to calculate the factorial of. * @returns {number} - The factorial of n. */ -const factorial = n => { +const factorial = (n) => { if (n === 0) { return 1 } @@ -94,20 +97,21 @@ const readWriteFiles = ( fs.readFileSync(filePath, 'utf8') } fs.rmSync(baseDirectory, { recursive: true }) + return { ok: 1 } } -export const executeWorkerFunction = data => { +export const executeTaskFunction = (data) => { switch (data.function) { - case WorkerFunctions.jsonIntegerSerialization: + case TaskFunctions.jsonIntegerSerialization: return jsonIntegerSerialization(data.taskSize || 1000) - case WorkerFunctions.fibonacci: + case TaskFunctions.fibonacci: return fibonacci(data.taskSize || 1000) - case WorkerFunctions.factorial: + case TaskFunctions.factorial: return factorial(data.taskSize || 1000) - case WorkerFunctions.readWriteFiles: + case TaskFunctions.readWriteFiles: return readWriteFiles(data.taskSize || 1000) default: - throw new Error('Unknown worker function') + throw new Error('Unknown task function') } }