perf: use optimized circular buffer implementation to store measurements history
[poolifier.git] / benchmarks / internal / bench.mjs
index 891ff9eca7118d5bb530e43d23315dcbdfa7c59f..3f467f9ea49689d642a4b9eae087416d806a9735 100644 (file)
-import assert from 'node:assert'
-import Benchmark from 'benchmark'
+import { writeFileSync } from 'node:fs'
+import { env } from 'node:process'
+// eslint-disable-next-line n/no-unsupported-features/node-builtins
+import { parseArgs } from 'node:util'
+
 import {
+  availableParallelism,
   PoolTypes,
-  WorkerChoiceStrategies,
-  WorkerTypes,
-  availableParallelism
+  WorkerTypes
 } from '../../lib/index.mjs'
-import { TaskFunctions } from '../benchmarks-types.mjs'
+import { TaskFunctions } from '../benchmarks-types.cjs'
 import {
-  LIST_FORMATTER,
-  buildPoolifierPool,
-  getPoolImplementationName,
-  runPoolifierTest
+  convertTatamiNgToBmf,
+  runPoolifierBenchmarkTatamiNg
 } from '../benchmarks-utils.mjs'
 
 const poolSize = availableParallelism()
-const fixedThreadPool = buildPoolifierPool(
-  WorkerTypes.thread,
-  PoolTypes.fixed,
-  poolSize
-)
-
 const taskExecutions = 1
 const workerData = {
-  function: TaskFunctions.jsonIntegerSerialization,
+  function: TaskFunctions.factorial,
   taskSize: 1000
 }
+const benchmarkReportFile = 'benchmark-report.json'
+let benchmarkReport
 
-const poolifierSuite = new Benchmark.Suite('Poolifier')
-
-for (const pool of [fixedThreadPool]) {
-  for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
-    for (const enableTasksQueue of [false, true]) {
-      poolifierSuite.add(
-        `${getPoolImplementationName(pool)}|${workerChoiceStrategy}|${
-          enableTasksQueue ? 'with' : 'without'
-        } tasks queue`,
-        async () => {
-          pool.setWorkerChoiceStrategy(workerChoiceStrategy)
-          pool.enableTasksQueue(enableTasksQueue)
-          assert.strictEqual(
-            pool.opts.workerChoiceStrategy,
-            workerChoiceStrategy
-          )
-          assert.strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
-          await runPoolifierTest(pool, {
+switch (
+  parseArgs({
+    args: process.argv,
+    options: {
+      type: {
+        type: 'string',
+        short: 't'
+      }
+    },
+    strict: true,
+    allowPositionals: true
+  }).values.type
+) {
+  case 'tatami-ng':
+  default:
+    benchmarkReport = convertTatamiNgToBmf(
+      await runPoolifierBenchmarkTatamiNg(
+        'FixedThreadPool',
+        WorkerTypes.thread,
+        PoolTypes.fixed,
+        poolSize,
+        {
+          taskExecutions,
+          workerData
+        }
+      )
+    )
+    benchmarkReport = {
+      ...benchmarkReport,
+      ...convertTatamiNgToBmf(
+        await runPoolifierBenchmarkTatamiNg(
+          'DynamicThreadPool',
+          WorkerTypes.thread,
+          PoolTypes.dynamic,
+          poolSize,
+          {
             taskExecutions,
             workerData
-          })
-        }
+          }
+        )
       )
     }
-  }
+    benchmarkReport = {
+      ...benchmarkReport,
+      ...convertTatamiNgToBmf(
+        await runPoolifierBenchmarkTatamiNg(
+          'FixedClusterPool',
+          WorkerTypes.cluster,
+          PoolTypes.fixed,
+          poolSize,
+          {
+            taskExecutions,
+            workerData
+          }
+        )
+      )
+    }
+    benchmarkReport = {
+      ...benchmarkReport,
+      ...convertTatamiNgToBmf(
+        await runPoolifierBenchmarkTatamiNg(
+          'DynamicClusterPool',
+          WorkerTypes.cluster,
+          PoolTypes.dynamic,
+          poolSize,
+          {
+            taskExecutions,
+            workerData
+          }
+        )
+      )
+    }
+    env.CI != null &&
+      writeFileSync(benchmarkReportFile, JSON.stringify(benchmarkReport))
+    break
 }
-
-poolifierSuite
-  .on('cycle', event => {
-    console.info(event.target.toString())
-  })
-  .on('complete', async function () {
-    console.info(
-      'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
-    )
-    await fixedThreadPool.destroy()
-  })
-  .run({ async: true })