Add benchmark script (#104)
[poolifier.git] / benchmarks / bench.js
index e0daca2d0aa84a85387f23414b884b1c57170f2d..017ac20be223e0fd26faba804e54c2329dcf931c 100644 (file)
@@ -1,18 +1,23 @@
 const Benchmark = require('benchmark')
 const suite = new Benchmark.Suite()
-const FixedThreadPool = require('../lib/fixed')
-const DynamicThreadPool = require('../lib/dynamic')
+const { FixedThreadPool } = require('../lib/index')
+const { DynamicThreadPool } = require('../lib/index')
 const size = 30
 const tasks = 1
 
+const LIST_FORMATTER = new Intl.ListFormat('en-US', {
+  style: 'long',
+  type: 'conjunction'
+})
+
 // pools
-const fixedPool = new FixedThreadPool(size, './yourWorker.js', {
+const fixedPool = new FixedThreadPool(size, './threadWorker.js', {
   maxTasks: 10000
 })
 const dynamicPool = new DynamicThreadPool(
   size / 2,
   size * 3,
-  './yourWorker.js',
+  './threadWorker.js',
   { maxTasks: 10000 }
 )
 const workerData = { proof: 'ok' }
@@ -27,12 +32,18 @@ async function fixedTest () {
   return new Promise((resolve, reject) => {
     let executions = 0
     for (let i = 0; i <= tasks; i++) {
-      fixedPool.execute(workerData).then(res => {
-        executions++
-        if (executions === tasks) {
-          resolve('FINISH')
-        }
-      })
+      fixedPool
+        .execute(workerData)
+        .then(res => {
+          executions++
+          if (executions === tasks) {
+            return resolve('FINISH')
+          }
+          return null
+        })
+        .catch(err => {
+          console.error(err)
+        })
     }
   })
 }
@@ -41,12 +52,16 @@ async function dynamicTest () {
   return new Promise((resolve, reject) => {
     let executions = 0
     for (let i = 0; i <= tasks; i++) {
-      dynamicPool.execute(workerData).then(res => {
-        executions++
-        if (executions === tasks) {
-          resolve('FINISH')
-        }
-      })
+      dynamicPool
+        .execute(workerData)
+        .then(res => {
+          executions++
+          if (executions === tasks) {
+            return resolve('FINISH')
+          }
+          return null
+        })
+        .catch(err => console.error(err))
     }
   })
 }
@@ -62,11 +77,15 @@ async function test () {
     })
     // add listeners
     .on('cycle', function (event) {
-      console.log(String(event.target))
+      console.log(event.target.toString())
     })
     .on('complete', function () {
-      this.filter('fastest').map('name')
-      console.log('Fastest is ' + this.filter('fastest').map('name'))
+      console.log(
+        'Fastest is ' +
+          LIST_FORMATTER.format(this.filter('fastest').map('name'))
+      )
+      // eslint-disable-next-line no-process-exit
+      process.exit()
     })
     // run async
     .run({ async: true })