Run benchmarks versus latest external pool libraries
[poolifier.git] / benchmarks / internal / benchmark-utils.js
index 7cb4e54f500283d5c5da6e69b743f5b851ba344e..8ea43599282949a246d684dcf483a07c68377568 100644 (file)
@@ -1,10 +1,16 @@
+const WorkerFunctions = {
+  jsonIntegerSerialization: 'jsonIntegerSerialization',
+  fibonacci: 'fibonacci',
+  factorial: 'factorial'
+}
+
 async function runPoolifierTest (pool, { tasks, workerData }) {
   return new Promise((resolve, reject) => {
     let executions = 0
     for (let i = 1; i <= tasks; i++) {
       pool
         .execute(workerData)
-        .then(res => {
+        .then(() => {
           executions++
           if (executions === tasks) {
             return resolve('FINISH')
@@ -28,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)
@@ -62,16 +68,28 @@ function factorial (n) {
   }
 }
 
+function executeWorkerFunction (data) {
+  switch (data.function) {
+    case WorkerFunctions.jsonIntegerSerialization:
+      return jsonIntegerSerialization(data.n || 1000)
+    case WorkerFunctions.fibonacci:
+      return fibonacci(data.n || 1000)
+    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,
-  factorial,
-  LIST_FORMATTER
+  runPoolifierTest
 }