Fix strategy handling in pool options (#259)
[poolifier.git] / benchmarks / internal / cluster / dynamic.js
index 468eee3b68db72e0d67671aeaf493d2e2b50e5ef..3c00c50b8c7af84ebfc45f5f6cb4a2c814104c41 100644 (file)
@@ -1,34 +1,38 @@
-const { DynamicClusterPool } = require('../../../lib/index')
+const {
+  DynamicClusterPool,
+  WorkerChoiceStrategies
+} = require('../../../lib/index')
+const { runPoolifierTest } = require('../benchmark-utils')
 
 const size = 30
+const numberOfTasks = 1
 
 const dynamicPool = new DynamicClusterPool(
+  size / 2,
+  size * 3,
+  './benchmarks/internal/cluster/worker.js'
+)
+
+const dynamicPoolLessRecentlyUsed = new DynamicClusterPool(
   size / 2,
   size * 3,
   './benchmarks/internal/cluster/worker.js',
-  {
-    maxTasks: 10000
-  }
+  { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
 )
 
 async function dynamicClusterTest (
-  { tasks, workerData } = { tasks: 1, workerData: { proof: 'ok' } }
+  { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
 ) {
-  return new Promise((resolve, reject) => {
-    let executions = 0
-    for (let i = 0; i <= tasks; i++) {
-      dynamicPool
-        .execute(workerData)
-        .then(res => {
-          executions++
-          if (executions === tasks) {
-            return resolve('FINISH')
-          }
-          return null
-        })
-        .catch(err => console.error(err))
-    }
-  })
+  return runPoolifierTest(dynamicPool, { tasks, workerData })
 }
 
-module.exports = { dynamicClusterTest }
+async function dynamicClusterTestLessRecentlyUsed (
+  { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
+) {
+  return runPoolifierTest(dynamicPoolLessRecentlyUsed, { tasks, workerData })
+}
+
+module.exports = {
+  dynamicClusterTest,
+  dynamicClusterTestLessRecentlyUsed
+}