]> Piment Noir Git Repositories - poolifier.git/blobdiff - benchmarks/benchmarks-utils.mjs
refactor: avoid delete usage
[poolifier.git] / benchmarks / benchmarks-utils.mjs
index 8620dd681b10be4b6019dfc3d2be0dcfcf770311..0743037da81d4bf8bc2385f15d67bd2ea2a59521 100644 (file)
@@ -1,6 +1,5 @@
 import { strictEqual } from 'node:assert'
-
-import { bench, clear, group, run } from 'tatami-ng'
+import { bench, group, run } from 'tatami-ng'
 
 import {
   DynamicClusterPool,
@@ -10,30 +9,21 @@ import {
   Measurements,
   PoolTypes,
   WorkerChoiceStrategies,
-  WorkerTypes
+  WorkerTypes,
 } from '../lib/index.mjs'
 import { executeTaskFunction } from './benchmarks-utils.cjs'
 
 const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
   switch (poolType) {
-    case PoolTypes.fixed:
+    case PoolTypes.dynamic:
       switch (workerType) {
-        case WorkerTypes.thread:
-          return new FixedThreadPool(
-            poolSize,
-            './benchmarks/internal/thread-worker.mjs',
-            poolOptions
-          )
         case WorkerTypes.cluster:
-          return new FixedClusterPool(
+          return new DynamicClusterPool(
+            Math.floor(poolSize / 2),
             poolSize,
             './benchmarks/internal/cluster-worker.cjs',
             poolOptions
           )
-      }
-      break
-    case PoolTypes.dynamic:
-      switch (workerType) {
         case WorkerTypes.thread:
           return new DynamicThreadPool(
             Math.floor(poolSize / 2),
@@ -41,37 +31,31 @@ const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
             './benchmarks/internal/thread-worker.mjs',
             poolOptions
           )
+      }
+      break
+    case PoolTypes.fixed:
+      switch (workerType) {
         case WorkerTypes.cluster:
-          return new DynamicClusterPool(
-            Math.floor(poolSize / 2),
+          return new FixedClusterPool(
             poolSize,
             './benchmarks/internal/cluster-worker.cjs',
             poolOptions
           )
+        case WorkerTypes.thread:
+          return new FixedThreadPool(
+            poolSize,
+            './benchmarks/internal/thread-worker.mjs',
+            poolOptions
+          )
       }
       break
   }
 }
 
 const runPoolifierPool = async (pool, { taskExecutions, workerData }) => {
-  return await new Promise((resolve, reject) => {
-    let executions = 0
-    for (let i = 1; i <= taskExecutions; i++) {
-      pool
-        .execute(workerData)
-        .then(() => {
-          ++executions
-          if (executions === taskExecutions) {
-            resolve({ ok: 1 })
-          }
-          return undefined
-        })
-        .catch(err => {
-          console.error(err)
-          reject(err)
-        })
-    }
-  })
+  for (let i = 1; i <= taskExecutions; i++) {
+    await pool.execute(workerData)
+  }
 }
 
 export const runPoolifierBenchmarkTatamiNg = async (
@@ -79,6 +63,7 @@ export const runPoolifierBenchmarkTatamiNg = async (
   workerType,
   poolType,
   poolSize,
+  benchmarkReporter,
   { taskExecutions, workerData }
 ) => {
   try {
@@ -95,13 +80,13 @@ export const runPoolifierBenchmarkTatamiNg = async (
                 async () => {
                   await runPoolifierPool(pool, {
                     taskExecutions,
-                    workerData
+                    workerData,
                   })
                 },
                 {
                   before: () => {
                     pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
-                      measurement
+                      measurement,
                     })
                     pool.enableTasksQueue(enableTasksQueue)
                     strictEqual(
@@ -113,7 +98,7 @@ export const runPoolifierBenchmarkTatamiNg = async (
                       pool.opts.workerChoiceStrategyOptions.measurement,
                       measurement
                     )
-                  }
+                  },
                 }
               )
             })
@@ -127,7 +112,7 @@ export const runPoolifierBenchmarkTatamiNg = async (
               async () => {
                 await runPoolifierPool(pool, {
                   taskExecutions,
-                  workerData
+                  workerData,
                 })
               },
               {
@@ -139,15 +124,14 @@ export const runPoolifierBenchmarkTatamiNg = async (
                     workerChoiceStrategy
                   )
                   strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
-                }
+                },
               }
             )
           })
         }
       }
     }
-    const report = await run()
-    clear()
+    const report = await run({ reporter: benchmarkReporter })
     await pool.destroy()
     return report
   } catch (error) {
@@ -155,23 +139,4 @@ export const runPoolifierBenchmarkTatamiNg = async (
   }
 }
 
-export const convertTatamiNgToBmf = report => {
-  return report.benchmarks
-    .map(({ name, stats }) => {
-      return {
-        [name]: {
-          latency: {
-            value: stats?.avg,
-            lower_value: stats?.min,
-            upper_value: stats?.max
-          },
-          throughput: {
-            value: stats?.iter
-          }
-        }
-      }
-    })
-    .reduce((obj, item) => Object.assign(obj, item), {})
-}
-
 export { executeTaskFunction }