Bump typescript from 4.1.4 to 4.1.5
[poolifier.git] / benchmarks / bench.js
index 39d743ad1293b7c851cd6e4180ae2c8ad6047ff5..017ac20be223e0fd26faba804e54c2329dcf931c 100644 (file)
@@ -1,58 +1,92 @@
 const Benchmark = require('benchmark')
 const suite = new Benchmark.Suite()
-const FixedThreadPool = require('../lib/fixed')
-const Pool = require('worker-threads-pool')
-const size = 80
-const externalPool = new Pool({ max: size })
+const { FixedThreadPool } = require('../lib/index')
+const { DynamicThreadPool } = require('../lib/index')
+const size = 30
+const tasks = 1
 
-const fixedPool = new FixedThreadPool(size,
-  './yourWorker.js', { maxTasks: 10000 })
-// const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 })
+const LIST_FORMATTER = new Intl.ListFormat('en-US', {
+  style: 'long',
+  type: 'conjunction'
+})
+
+// pools
+const fixedPool = new FixedThreadPool(size, './threadWorker.js', {
+  maxTasks: 10000
+})
+const dynamicPool = new DynamicThreadPool(
+  size / 2,
+  size * 3,
+  './threadWorker.js',
+  { maxTasks: 10000 }
+)
 const workerData = { proof: 'ok' }
-let executions = 0
 
 // wait some seconds before start, my pools need to load threads !!!
 setTimeout(async () => {
   test()
 }, 3000)
 
-async function test () {
-  // add tests
-  suite.add('PioardiStaticPool', async function () {
-    executions++
-    await fixedPool.execute(workerData)
+// fixed pool proof
+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) {
+            return resolve('FINISH')
+          }
+          return null
+        })
+        .catch(err => {
+          console.error(err)
+        })
+    }
   })
-    .add('ExternalPool', async function () {
-      await new Promise((resolve, reject) => {
-        externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
-          if (err) {
-            return reject(err)
+}
+
+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) {
+            return resolve('FINISH')
           }
-          worker.on('error', reject)
-          worker.on('message', res => {
-            resolve(res)
-          })
+          return null
         })
-      })
+        .catch(err => console.error(err))
+    }
+  })
+}
+
+async function test () {
+  // add tests
+  suite
+    .add('PioardiStaticPool', async function () {
+      await fixedTest()
     })
-    /*
     .add('PioardiDynamicPool', async function () {
-      await dynamicPool.execute(workerData)
-    }) */
-  // add listeners
+      await dynamicTest()
+    })
+    // add listeners
     .on('cycle', function (event) {
-      console.log(String(event.target))
+      console.log(event.target.toString())
     })
     .on('complete', function () {
-      console.log(executions)
-      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
     .run({ async: true })
 }
-
-process.on('SIGKILL', () => {
-  fixedPool.destroy()
-  externalPool.destroy()
-})