refactor: cleanup enums namespace
[poolifier.git] / benchmarks / internal / bench.js
index c3db71651a1e84b333490ded1bcd10f29650ff41..627bd0ed48d6e12f3c6ecda13ed3aad0a88990ba 100644 (file)
-const Benchmark = require('benchmark')
+const Benchmark = require('benny')
+const { WorkerChoiceStrategies } = require('../../lib')
 const {
-  dynamicClusterTest,
-  dynamicClusterTestLessRecentlyUsed
-} = require('./cluster/dynamic')
-const { fixedClusterTest } = require('./cluster/fixed')
-const {
-  dynamicThreadTest,
-  dynamicThreadTestLessRecentlyUsed
-} = require('./thread/dynamic')
-const { fixedThreadTest } = require('./thread/fixed')
+  PoolTypes,
+  WorkerFunctions,
+  WorkerTypes
+} = require('../benchmarks-types')
+const { buildPool, runTest } = require('../benchmarks-utils')
+
+const poolSize = 30
+const taskExecutions = 1
+const workerData = {
+  function: WorkerFunctions.jsonIntegerSerialization,
+  taskSize: 1000
+}
+const tasksQueuePoolOption = { enableTasksQueue: true }
+const workerChoiceStrategyRoundRobinPoolOption = {
+  workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN
+}
+const workerChoiceStrategyLeastUsedPoolOption = {
+  workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED
+}
+const workerChoiceStrategyLeastBusyPoolOption = {
+  workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY
+}
+const workerChoiceStrategyWeightedRoundRobinPoolOption = {
+  workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+}
+const workerChoiceStrategyFairSharePoolOption = {
+  workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE
+}
+
+const fixedThreadPoolRoundRobin = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyRoundRobinPoolOption
+)
+
+const fixedThreadPoolRoundRobinTasksQueue = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  { ...workerChoiceStrategyRoundRobinPoolOption, ...tasksQueuePoolOption }
+)
+
+const fixedThreadPoolLeastUsed = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyLeastUsedPoolOption
+)
+
+const fixedThreadPoolLeastBusy = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyLeastBusyPoolOption
+)
+
+const fixedThreadPoolWeightedRoundRobin = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyWeightedRoundRobinPoolOption
+)
+
+const fixedThreadPoolFairShare = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyFairSharePoolOption
+)
+
+const fixedThreadPoolFairShareTasksQueue = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.fixed,
+  poolSize,
+  { ...workerChoiceStrategyFairSharePoolOption, ...tasksQueuePoolOption }
+)
+
+const dynamicThreadPoolRoundRobin = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyRoundRobinPoolOption
+)
+
+const dynamicThreadPoolLeastUsed = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyLeastUsedPoolOption
+)
+
+const dynamicThreadPoolLeastBusy = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyLeastBusyPoolOption
+)
+
+const dynamicThreadPoolWeightedRoundRobin = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyWeightedRoundRobinPoolOption
+)
+
+const dynamicThreadPoolFairShare = buildPool(
+  WorkerTypes.thread,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyFairSharePoolOption
+)
+
+const fixedClusterPoolRoundRobin = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyRoundRobinPoolOption
+)
+
+const fixedClusterPoolRoundRobinTasksQueue = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  { ...workerChoiceStrategyRoundRobinPoolOption, ...tasksQueuePoolOption }
+)
 
-const suite = new Benchmark.Suite('poolifier')
+const fixedClusterPoolLeastUsed = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyLeastUsedPoolOption
+)
 
-const LIST_FORMATTER = new Intl.ListFormat('en-US', {
-  style: 'long',
-  type: 'conjunction'
-})
+const fixedClusterPoolLeastBusy = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyLeastBusyPoolOption
+)
 
-// Wait some seconds before start, pools need to load threads !!!
-setTimeout(async () => {
-  test()
-}, 3000)
+const fixedClusterPoolWeightedRoundRobin = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyWeightedRoundRobinPoolOption
+)
 
-async function test () {
-  // Add tests
-  suite
-    .add('Poolifier:Static:ThreadPool', async function () {
-      await fixedThreadTest()
+const fixedClusterPoolFairShare = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  workerChoiceStrategyFairSharePoolOption
+)
+
+const fixedClusterPoolFairShareTaskQueue = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.fixed,
+  poolSize,
+  { ...workerChoiceStrategyFairSharePoolOption, ...tasksQueuePoolOption }
+)
+
+const dynamicClusterPoolRoundRobin = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyRoundRobinPoolOption
+)
+
+const dynamicClusterPoolLeastUsed = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyLeastUsedPoolOption
+)
+
+const dynamicClusterPoolLeastBusy = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyLeastBusyPoolOption
+)
+
+const dynamicClusterPoolWeightedRoundRobin = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyWeightedRoundRobinPoolOption
+)
+
+const dynamicClusterPoolFairShare = buildPool(
+  WorkerTypes.cluster,
+  PoolTypes.dynamic,
+  poolSize,
+  workerChoiceStrategyFairSharePoolOption
+)
+
+const resultsFile = 'poolifier'
+const resultsFolder = 'benchmarks/internal/results'
+
+Benchmark.suite(
+  'Poolifier',
+  Benchmark.add('Fixed:ThreadPool:RoundRobin', async () => {
+    await runTest(fixedThreadPoolRoundRobin, {
+      taskExecutions,
+      workerData
     })
-    .add('Poolifier:Dynamic:ThreadPool', async function () {
-      await dynamicThreadTest()
+  }),
+  Benchmark.add(
+    'Fixed:ThreadPool:RoundRobin:{ enableTasksQueue: true }',
+    async () => {
+      await runTest(fixedThreadPoolRoundRobinTasksQueue, {
+        taskExecutions,
+        workerData
+      })
+    }
+  ),
+  Benchmark.add('Fixed:ThreadPool:LeastUsed', async () => {
+    await runTest(fixedThreadPoolLeastUsed, {
+      taskExecutions,
+      workerData
     })
-    .add('Poolifier:Dynamic:ThreadPool:LessRecentlyUsed', async function () {
-      await dynamicThreadTestLessRecentlyUsed()
+  }),
+  Benchmark.add('Fixed:ThreadPool:LeastBusy', async () => {
+    await runTest(fixedThreadPoolLeastBusy, {
+      taskExecutions,
+      workerData
     })
-    .add('Poolifier:Static:ClusterPool', async function () {
-      await fixedClusterTest()
+  }),
+  Benchmark.add('Fixed:ThreadPool:WeightedRoundRobin', async () => {
+    await runTest(fixedThreadPoolWeightedRoundRobin, {
+      taskExecutions,
+      workerData
     })
-    .add('Poolifier:Dynamic:ClusterPool', async function () {
-      await dynamicClusterTest()
+  }),
+  Benchmark.add('Fixed:ThreadPool:FairShare', async () => {
+    await runTest(fixedThreadPoolFairShare, {
+      taskExecutions,
+      workerData
     })
-    .add('Poolifier:Dynamic:ClusterPool:LessRecentlyUsed', async function () {
-      await dynamicClusterTestLessRecentlyUsed()
+  }),
+  Benchmark.add(
+    'Fixed:ThreadPool:FairShare:{ enableTasksQueue: true }',
+    async () => {
+      await runTest(fixedThreadPoolFairShareTasksQueue, {
+        taskExecutions,
+        workerData
+      })
+    }
+  ),
+  Benchmark.add('Dynamic:ThreadPool:RoundRobin', async () => {
+    await runTest(dynamicThreadPoolRoundRobin, {
+      taskExecutions,
+      workerData
     })
-    // Add listeners
-    .on('cycle', function (event) {
-      console.log(event.target.toString())
+  }),
+  Benchmark.add('Dynamic:ThreadPool:LeastUsed', async () => {
+    await runTest(dynamicThreadPoolLeastUsed, {
+      taskExecutions,
+      workerData
     })
-    .on('complete', function () {
-      console.log(
-        'Fastest is ' +
-          LIST_FORMATTER.format(this.filter('fastest').map('name'))
-      )
-      // eslint-disable-next-line no-process-exit
-      process.exit()
+  }),
+  Benchmark.add('Dynamic:ThreadPool:LeastBusy', async () => {
+    await runTest(dynamicThreadPoolLeastBusy, {
+      taskExecutions,
+      workerData
     })
-    .run({ async: true, queued: true })
-}
+  }),
+  Benchmark.add('Dynamic:ThreadPool:WeightedRoundRobin', async () => {
+    await runTest(dynamicThreadPoolWeightedRoundRobin, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Dynamic:ThreadPool:FairShare', async () => {
+    await runTest(dynamicThreadPoolFairShare, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Fixed:ClusterPool:RoundRobin', async () => {
+    await runTest(fixedClusterPoolRoundRobin, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add(
+    'Fixed:ClusterPool:RoundRobin:{ enableTasksQueue: true }',
+    async () => {
+      await runTest(fixedClusterPoolRoundRobinTasksQueue, {
+        taskExecutions,
+        workerData
+      })
+    }
+  ),
+  Benchmark.add('Fixed:ClusterPool:LeastUsed', async () => {
+    await runTest(fixedClusterPoolLeastUsed, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Fixed:ClusterPool:LeastBusy', async () => {
+    await runTest(fixedClusterPoolLeastBusy, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Fixed:ClusterPool:WeightedRoundRobin', async () => {
+    await runTest(fixedClusterPoolWeightedRoundRobin, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Fixed:ClusterPool:FairShare', async () => {
+    await runTest(fixedClusterPoolFairShare, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add(
+    'Fixed:ClusterPool:FairShare:{ enableTasksQueue: true }',
+    async () => {
+      await runTest(fixedClusterPoolFairShareTaskQueue, {
+        taskExecutions,
+        workerData
+      })
+    }
+  ),
+  Benchmark.add('Dynamic:ClusterPool:RoundRobin', async () => {
+    await runTest(dynamicClusterPoolRoundRobin, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Dynamic:ClusterPool:LeastUsed', async () => {
+    await runTest(dynamicClusterPoolLeastUsed, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Dynamic:ClusterPool:LeastBusy', async () => {
+    await runTest(dynamicClusterPoolLeastBusy, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Dynamic:ClusterPool:WeightedRoundRobin', async () => {
+    await runTest(dynamicClusterPoolWeightedRoundRobin, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.add('Dynamic:ClusterPool:FairShare', async () => {
+    await runTest(dynamicClusterPoolFairShare, {
+      taskExecutions,
+      workerData
+    })
+  }),
+  Benchmark.cycle(),
+  Benchmark.complete(),
+  Benchmark.save({
+    file: resultsFile,
+    folder: resultsFolder,
+    format: 'json',
+    details: true
+  }),
+  Benchmark.save({
+    file: resultsFile,
+    folder: resultsFolder,
+    format: 'chart.html',
+    details: true
+  }),
+  Benchmark.save({
+    file: resultsFile,
+    folder: resultsFolder,
+    format: 'table.html',
+    details: true
+  })
+)
+  .then(() => {
+    // eslint-disable-next-line n/no-process-exit
+    return process.exit()
+  })
+  .catch(err => console.error(err))