X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Finternal%2Fbenchmark-utils.js;h=60cecbe5c8b565314c59549244e72db6c7178220;hb=301c53e7ae6cf1120298ea10868a9091904c01a2;hp=5f0c643b0c189a9d6d232deae5e8c0effe9cbae8;hpb=bdacc2d25f190728221f7fb8c8cd2aba175cb18d;p=poolifier.git diff --git a/benchmarks/internal/benchmark-utils.js b/benchmarks/internal/benchmark-utils.js index 5f0c643b..60cecbe5 100644 --- a/benchmarks/internal/benchmark-utils.js +++ b/benchmarks/internal/benchmark-utils.js @@ -1,3 +1,9 @@ +const WorkerFunctions = { + jsonIntegerSerialization: 'jsonIntegerSerialization', + fibonacci: 'fibonacci', + factorial: 'factorial' +} + async function runPoolifierTest (pool, { tasks, workerData }) { return new Promise((resolve, reject) => { let executions = 0 @@ -11,7 +17,10 @@ async function runPoolifierTest (pool, { tasks, workerData }) { } return null }) - .catch(err => console.error(err)) + .catch(err => { + console.error(err) + return reject(err) + }) } }) } @@ -25,7 +34,7 @@ function jsonIntegerSerialization (n) { } } -function generateRandomInteger (max, min = 0) { +function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { max = Math.floor(max) if (min) { min = Math.ceil(min) @@ -37,23 +46,50 @@ function generateRandomInteger (max, min = 0) { /** * Intentionally inefficient implementation. * - * @param {*} n - * @returns {number} + * @param {number} n - The number of fibonacci numbers to generate. + * @returns {number} - The nth fibonacci number. */ function fibonacci (n) { if (n <= 1) return 1 return fibonacci(n - 1) + fibonacci(n - 2) } +/** + * Intentionally inefficient implementation. + * + * @param {number} n - The number to calculate the factorial of. + * @returns {number} - The factorial of n. + */ +function factorial (n) { + if (n === 0) { + return 1 + } else { + return factorial(n - 1) * n + } +} + +function executeWorkerFunction (data) { + switch (data.function) { + case WorkerFunctions.jsonIntegerSerialization: + return jsonIntegerSerialization(data.n || 1000) + case WorkerFunctions.fibonacci: + return fibonacci(data.n || 50) + case WorkerFunctions.factorial: + return factorial(data.n || 1000) + default: + throw new Error('Unknown worker function') + } +} + const LIST_FORMATTER = new Intl.ListFormat('en-US', { style: 'long', type: 'conjunction' }) module.exports = { - runPoolifierTest, - jsonIntegerSerialization, + LIST_FORMATTER, + WorkerFunctions, + executeWorkerFunction, generateRandomInteger, - fibonacci, - LIST_FORMATTER + runPoolifierTest }