chore: v3.1.26
[poolifier.git] / benchmarks / internal / bench.mjs
index 7d3e90f288c478c84dd3cedabba6cb8a371a227a..b59514d225b45adf149f011798156af1a7bcf675 100644 (file)
@@ -1,66 +1,66 @@
-import assert from 'node:assert'
-import Benchmark from 'benchmark'
+import { exit } from 'node:process'
+
 import {
+  availableParallelism,
   PoolTypes,
-  WorkerChoiceStrategies,
-  WorkerTypes,
-  availableParallelism
+  WorkerTypes
 } from '../../lib/index.mjs'
-import { TaskFunctions } from '../benchmarks-types.mjs'
-import {
-  LIST_FORMATTER,
-  buildPoolifierPool,
-  runPoolifierTest
-} from '../benchmarks-utils.mjs'
+import { TaskFunctions } from '../benchmarks-types.cjs'
+import { runPoolifierPoolBenchmark } from '../benchmarks-utils.cjs'
 
 const poolSize = availableParallelism()
-const fixedThreadPool = buildPoolifierPool(
-  WorkerTypes.thread,
-  PoolTypes.fixed,
-  poolSize
-)
-
 const taskExecutions = 1
 const workerData = {
   function: TaskFunctions.jsonIntegerSerialization,
-  taskSize: 100
+  taskSize: 1000
 }
 
-const poolifierSuite = new Benchmark.Suite('Poolifier')
+// FixedThreadPool
+await runPoolifierPoolBenchmark(
+  'FixedThreadPool',
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  {
+    taskExecutions,
+    workerData
+  }
+)
+
+// DynamicThreadPool
+await runPoolifierPoolBenchmark(
+  'DynamicThreadPool',
+  WorkerTypes.thread,
+  PoolTypes.dynamic,
+  poolSize,
+  {
+    taskExecutions,
+    workerData
+  }
+)
+
+// FixedClusterPool
+await runPoolifierPoolBenchmark(
+  'FixedClusterPool',
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  {
+    taskExecutions,
+    workerData
+  }
+)
 
-for (const pool of [fixedThreadPool]) {
-  for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
-    for (const enableTasksQueue of [false, true]) {
-      poolifierSuite.add(
-        `${pool.constructor.name}|${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, {
-            taskExecutions,
-            workerData
-          })
-        }
-      )
-    }
+// DynamicClusterPool
+await runPoolifierPoolBenchmark(
+  'DynamicClusterPool',
+  WorkerTypes.cluster,
+  PoolTypes.dynamic,
+  poolSize,
+  {
+    taskExecutions,
+    workerData
   }
-}
+)
 
-poolifierSuite
-  .on('cycle', event => {
-    console.info(event.target.toString())
-  })
-  .on('complete', function () {
-    console.info(
-      'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
-    )
-    fixedThreadPool.destroy()
-  })
-  .run({ async: true })
+exit()