Update README.md
[poolifier.git] / benchmarks / myBench.js
index 6ea0abfed5e92b28fc5a5e1e833111fe6a77ad37..a69de002aab4c6840e03601b34546e3b54c29056 100644 (file)
@@ -1,13 +1,26 @@
-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 = 10
+const size = 16
 
 // pools
-const externalPool = new Pool({ max: size })
-const fixedPool = new FixedThreadPool(size, './yourWorker.js', { maxTasks: 10000 })
-const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 })
+const workerThreadsPool = new WorkerThreadsPool({ max: size })
+const workerPool = workerpool.pool('./external/workerpoolWorker.js', {
+  minWorkers: size / 2,
+  maxWorkers: size * 3,
+  workerType: 'thread'
+})
+const fixedPool = new FixedThreadPool(size, './thread/worker.js', {
+  maxTasks: 10000
+})
+const dynamicPool = new DynamicThreadPool(
+  size / 2,
+  size * 3,
+  './thread/worker.js',
+  { maxTasks: 10000 }
+)
 
 // data
 const workerData = { proof: 'ok' }
@@ -16,47 +29,105 @@ const workerData = { proof: 'ok' }
 async function fixedTest () {
   let executions = 0
   const time = Date.now()
-  for (let i = 0; i < tasks; i++) {
-    await fixedPool.execute(workerData)
-    executions++
+  for (let i = 0; i <= tasks; i++) {
+    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))
   }
-  console.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`)
 }
 
 async function dynamicTest () {
   let executions = 0
   const time = Date.now()
-  for (let i = 0; i < tasks; i++) {
-    await dynamicPool.execute(workerData)
-    executions++
+  for (let i = 0; i <= tasks; i++) {
+    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))
   }
-  console.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`)
 }
 
-async function externalPoolTest () {
+async function workerThreadsPoolTest () {
   let executions = 0
   const time = Date.now()
-  for (let i = 0; i < tasks; i++) {
-    await new Promise((resolve, reject) => {
-      externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
-        if (err) {
-          return reject(err)
+  for (let i = 0; i <= tasks; i++) {
+    new Promise((resolve, reject) => {
+      workerThreadsPool.acquire(
+        './external/workerThreadsWorker.js',
+        { workerData: workerData },
+        (err, worker) => {
+          if (err) {
+            return reject(err)
+          }
+          worker.on('error', reject)
+          worker.on('message', res => {
+            executions++
+            resolve(res)
+          })
         }
-        worker.on('error', reject)
-        worker.on('message', res => {
-          executions++
-          resolve(res)
-        })
-      })
+      )
     })
+      .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++
+        if (tasks === executions) {
+          return console.log(
+            `workerpool take ${
+              Date.now() - time
+            }ms to work on ${executions} tasks`
+          )
+        }
+        return null
+      })
+      .catch(err => console.error(err))
   }
-  console.log(`External pool take ${Date.now() - time} to work  on ${executions} tasks`)
 }
 
 async function test () {
   await fixedTest()
   await dynamicTest()
-  await externalPoolTest()
+  await workerThreadsPoolTest()
+  await workerpoolTest()
 }
 
 test()