build(deps-dev): apply updates
[poolifier.git] / benchmarks / benchmarks-utils.mjs
index f04e4d0f64a0608b9258d5b346a5f4ce9fd21501..6c01efcb348fa7ae11faf07131a3ea4d307cde0a 100644 (file)
@@ -1,7 +1,6 @@
 import { strictEqual } from 'node:assert'
 
-import Benchmark from 'benchmark'
-import { bench, group } from 'mitata'
+import { bench, clear, group, run } from 'tatami-ng'
 
 import {
   DynamicClusterPool,
@@ -11,7 +10,7 @@ import {
   Measurements,
   PoolTypes,
   WorkerChoiceStrategies,
-  WorkerTypes
+  WorkerTypes,
 } from '../lib/index.mjs'
 import { executeTaskFunction } from './benchmarks-utils.cjs'
 
@@ -55,60 +54,39 @@ const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
 }
 
 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 runPoolifierBenchmarkBenchmarkJs = async (
+export const runPoolifierBenchmarkTatamiNg = async (
   name,
   workerType,
   poolType,
   poolSize,
   { taskExecutions, workerData }
 ) => {
-  return await new Promise((resolve, reject) => {
+  try {
     const pool = buildPoolifierPool(workerType, poolType, poolSize)
-    try {
-      const suite = new Benchmark.Suite(name)
-      for (const workerChoiceStrategy of Object.values(
-        WorkerChoiceStrategies
-      )) {
-        for (const enableTasksQueue of [false, true]) {
-          if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
-            for (const measurement of [
-              Measurements.runTime,
-              Measurements.elu
-            ]) {
-              suite.add(
+    for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
+      for (const enableTasksQueue of [false, true]) {
+        if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
+          for (const measurement of [Measurements.runTime, Measurements.elu]) {
+            group(name, () => {
+              bench(
                 `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
                   enableTasksQueue ? 'with' : 'without'
                 } tasks queue`,
                 async () => {
                   await runPoolifierPool(pool, {
                     taskExecutions,
-                    workerData
+                    workerData,
                   })
                 },
                 {
-                  onStart: () => {
+                  before: () => {
                     pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
-                      measurement
+                      measurement,
                     })
                     pool.enableTasksQueue(enableTasksQueue)
                     strictEqual(
@@ -120,23 +98,25 @@ export const runPoolifierBenchmarkBenchmarkJs = async (
                       pool.opts.workerChoiceStrategyOptions.measurement,
                       measurement
                     )
-                  }
+                  },
                 }
               )
-            }
-          } else {
-            suite.add(
+            })
+          }
+        } else {
+          group(name, () => {
+            bench(
               `${name} with ${workerChoiceStrategy} and ${
                 enableTasksQueue ? 'with' : 'without'
               } tasks queue`,
               async () => {
                 await runPoolifierPool(pool, {
                   taskExecutions,
-                  workerData
+                  workerData,
                 })
               },
               {
-                onStart: () => {
+                before: () => {
                   pool.setWorkerChoiceStrategy(workerChoiceStrategy)
                   pool.enableTasksQueue(enableTasksQueue)
                   strictEqual(
@@ -144,119 +124,39 @@ export const runPoolifierBenchmarkBenchmarkJs = async (
                     workerChoiceStrategy
                   )
                   strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
-                }
-              }
-            )
-          }
-        }
-      }
-      suite
-        .on('cycle', event => {
-          console.info(event.target.toString())
-        })
-        .on('complete', function () {
-          console.info(
-            'Fastest is ' +
-              LIST_FORMATTER.format(this.filter('fastest').map('name'))
-          )
-          const destroyTimeout = setTimeout(() => {
-            console.error('Pool destroy timeout reached (30s)')
-            resolve()
-          }, 30000)
-          pool
-            .destroy()
-            .then(resolve)
-            .catch(reject)
-            .finally(() => {
-              clearTimeout(destroyTimeout)
-            })
-            .catch(() => {})
-        })
-        .run({ async: true })
-    } catch (error) {
-      pool
-        .destroy()
-        .then(() => {
-          return reject(error)
-        })
-        .catch(() => {})
-    }
-  })
-}
-
-export const buildPoolifierBenchmarkMitata = (
-  name,
-  workerType,
-  poolType,
-  poolSize,
-  { taskExecutions, workerData }
-) => {
-  try {
-    const pool = buildPoolifierPool(workerType, poolType, poolSize)
-    for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
-      for (const enableTasksQueue of [false, true]) {
-        if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
-          for (const measurement of [Measurements.runTime, Measurements.elu]) {
-            group(name, () => {
-              bench(
-                `${name} with ${workerChoiceStrategy}, with ${measurement} and ${
-                  enableTasksQueue ? 'with' : 'without'
-                } tasks queue`,
-                async () => {
-                  pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
-                    measurement
-                  })
-                  pool.enableTasksQueue(enableTasksQueue)
-                  strictEqual(
-                    pool.opts.workerChoiceStrategy,
-                    workerChoiceStrategy
-                  )
-                  strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
-                  strictEqual(
-                    pool.opts.workerChoiceStrategyOptions.measurement,
-                    measurement
-                  )
-                  await runPoolifierPool(pool, {
-                    taskExecutions,
-                    workerData
-                  })
-                }
-              )
-            })
-          }
-        } else {
-          group(name, () => {
-            bench(
-              `${name} with ${workerChoiceStrategy} and ${
-                enableTasksQueue ? 'with' : 'without'
-              } tasks queue`,
-              async () => {
-                pool.setWorkerChoiceStrategy(workerChoiceStrategy)
-                pool.enableTasksQueue(enableTasksQueue)
-                strictEqual(
-                  pool.opts.workerChoiceStrategy,
-                  workerChoiceStrategy
-                )
-                strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
-                await runPoolifierPool(pool, {
-                  taskExecutions,
-                  workerData
-                })
+                },
               }
             )
           })
         }
       }
     }
-    return pool
+    const report = await run()
+    clear()
+    await pool.destroy()
+    return report
   } catch (error) {
     console.error(error)
   }
 }
 
-const LIST_FORMATTER = new Intl.ListFormat('en-US', {
-  style: 'long',
-  type: 'conjunction'
-})
+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 }