Add benchmark script (#104)
[poolifier.git] / benchmarks / myBench.js
index b714ab9e3a3b24cac366a7e6aecaa0d39662a26c..216b338e1777f9a74a9a825c4f53dbbd5be849db 100644 (file)
@@ -1,18 +1,24 @@
-const FixedThreadPool = require('../lib/fixed')
-const DynamicThreadPool = require('../lib/dynamic')
-const Pool = require('worker-threads-pool')
+const { FixedThreadPool } = require('../lib/index')
+const { DynamicThreadPool } = require('../lib/index')
+const WorkerThreadsPool = require('worker-threads-pool')
+const workerpool = require('workerpool')
 const tasks = 1000
 const size = 16
 
 // pools
-const externalPool = new Pool({ max: size })
-const fixedPool = new FixedThreadPool(size, './yourWorker.js', {
+const workerThreadsPool = new WorkerThreadsPool({ max: size })
+const workerPool = workerpool.pool('./workerpoolWorker.js', {
+  minWorkers: size / 2,
+  maxWorkers: size * 3,
+  workerType: 'thread'
+})
+const fixedPool = new FixedThreadPool(size, './threadWorker.js', {
   maxTasks: 10000
 })
 const dynamicPool = new DynamicThreadPool(
   size / 2,
   size * 3,
-  './yourWorker.js',
+  './threadWorker.js',
   { maxTasks: 10000 }
 )
 
@@ -24,14 +30,20 @@ async function fixedTest () {
   let executions = 0
   const time = Date.now()
   for (let i = 0; i <= tasks; i++) {
-    fixedPool.execute(workerData).then(res => {
-      executions++
-      if (executions === tasks) {
-        console.log(
-          `Fixed pool take ${Date.now() - time} to work on ${executions} tasks`
-        )
-      }
-    })
+    fixedPool
+      .execute(workerData)
+      .then(res => {
+        executions++
+        if (executions === tasks) {
+          return console.log(
+            `Fixed pool take ${
+              Date.now() - time
+            }ms to work on ${executions} tasks`
+          )
+        }
+        return null
+      })
+      .catch(err => console.error(err))
   }
 }
 
@@ -39,26 +51,30 @@ async function dynamicTest () {
   let executions = 0
   const time = Date.now()
   for (let i = 0; i <= tasks; i++) {
-    dynamicPool.execute(workerData).then(res => {
-      executions++
-      if (executions === tasks) {
-        console.log(
-          `Dynamic pool take ${
-            Date.now() - time
-          } to work on ${executions} tasks`
-        )
-      }
-    })
+    dynamicPool
+      .execute(workerData)
+      .then(res => {
+        executions++
+        if (executions === tasks) {
+          return console.log(
+            `Dynamic pool take ${
+              Date.now() - time
+            }ms to work on ${executions} tasks`
+          )
+        }
+        return null
+      })
+      .catch(err => console.error(err))
   }
 }
 
-async function externalPoolTest () {
+async function workerThreadsPoolTest () {
   let executions = 0
   const time = Date.now()
   for (let i = 0; i <= tasks; i++) {
     new Promise((resolve, reject) => {
-      externalPool.acquire(
-        './externalWorker.js',
+      workerThreadsPool.acquire(
+        './workerThreadsWorker.js',
         { workerData: workerData },
         (err, worker) => {
           if (err) {
@@ -71,22 +87,51 @@ async function externalPoolTest () {
           })
         }
       )
-    }).then(res => {
-      if (tasks === executions) {
-        console.log(
-          `External pool take ${
-            Date.now() - time
-          } to work  on ${executions} tasks`
-        )
-      }
     })
+      .then(res => {
+        if (tasks === executions) {
+          return console.log(
+            `worker threads pool take ${
+              Date.now() - time
+            }ms to work on ${executions} tasks`
+          )
+        }
+        return null
+      })
+      .catch(err => console.error(err))
+  }
+}
+
+async function workerpoolTest () {
+  let executions = 0
+  const time = Date.now()
+  for (let i = 0; i <= tasks; i++) {
+    workerPool
+      .exec('yourFunction', [workerData])
+      .then(res => {
+        executions++
+        return null
+      })
+      .catch(err => console.error(err))
+      .then(res => {
+        if (tasks === executions) {
+          return console.log(
+            `workerpool take ${
+              Date.now() - time
+            }ms to work on ${executions} tasks`
+          )
+        }
+        return null
+      })
+      .catch(err => console.error(err))
   }
 }
 
 async function test () {
-  fixedTest()
-  dynamicTest()
-  externalPoolTest()
+  await fixedTest()
+  await dynamicTest()
+  await workerThreadsPoolTest()
+  await workerpoolTest()
 }
 
 test()