Fix strategy handling in pool options (#259)
[poolifier.git] / benchmarks / internal / thread / dynamic.js
index dde75474daccf296dfab673737e02c5082cc787c..290820778e44ec7df7d2b2f2101b9dafeef4cb8c 100644 (file)
@@ -1,29 +1,38 @@
-const { DynamicThreadPool } = require('../../../lib/index')
+const {
+  DynamicThreadPool,
+  WorkerChoiceStrategies
+} = require('../../../lib/index')
+const { runPoolifierTest } = require('../benchmark-utils')
 
 const size = 30
+const numberOfTasks = 1
 
-const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './worker.js', {
-  maxTasks: 10000
-})
+const dynamicPool = new DynamicThreadPool(
+  size / 2,
+  size * 3,
+  './benchmarks/internal/thread/worker.js'
+)
+
+const dynamicPoolLessRecentlyUsed = new DynamicThreadPool(
+  size / 2,
+  size * 3,
+  './benchmarks/internal/thread/worker.js',
+  { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
+)
 
 async function dynamicThreadTest (
-  { 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 = { dynamicThreadTest }
+async function dynamicThreadTestLessRecentlyUsed (
+  { tasks, workerData } = { tasks: numberOfTasks, workerData: { proof: 'ok' } }
+) {
+  return runPoolifierTest(dynamicPoolLessRecentlyUsed, { tasks, workerData })
+}
+
+module.exports = {
+  dynamicThreadTest,
+  dynamicThreadTestLessRecentlyUsed
+}