Merge pull request #125 from pioardi/vscode-debug
[poolifier.git] / benchmarks / bench.js
index d7b2f1a36c322f29b5745bcd527ccd159113a903..b2091613e7909d4f920043032413014923e3e740 100644 (file)
@@ -1,95 +1,48 @@
 const Benchmark = require('benchmark')
+const { dynamicClusterTest } = require('./cluster/dynamic')
+const { fixedClusterTest } = require('./cluster/fixed')
+const { dynamicThreadTest } = require('./thread/dynamic')
+const { fixedThreadTest } = require('./thread/fixed')
+
 const suite = new Benchmark.Suite()
-const FixedThreadPool = require('../lib/fixed')
-const DynamicThreadPool = require('../lib/dynamic')
-const Pool = require('worker-threads-pool')
-const size = 30
-const tasks = 1
 
-// pools
-const externalPool = new Pool({ max: size })
-const fixedPool = new FixedThreadPool(size,
-  './yourWorker.js', { maxTasks: 10000 })
-const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './yourWorker.js', { maxTasks: 10000 })
-const workerData = { proof: 'ok' }
+const LIST_FORMATTER = new Intl.ListFormat('en-US', {
+  style: 'long',
+  type: 'conjunction'
+})
 
 // wait some seconds before start, my pools need to load threads !!!
 setTimeout(async () => {
   test()
 }, 3000)
 
-// 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) {
-          resolve('FINISH')
-        }
-      })
-    }
-  })
-}
-
-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')
-        }
-      })
-    }
-  })
-}
-
-async function externalPoolTest () {
-  return new Promise((resolve, reject) => {
-    let executions = 0
-    for (let i = 0; i <= tasks; i++) {
-      new Promise((resolve, reject) => {
-        externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
-          if (err) {
-            return reject(err)
-          }
-          worker.on('error', reject)
-          worker.on('message', res => {
-            executions++
-            resolve(res)
-          })
-        })
-      }).then(res => {
-        if (tasks === executions) {
-          resolve('FINISH')
-        }
-      })
-    }
-  })
-}
-
 async function test () {
   // add tests
-  suite.add('PioardiStaticPool', async function () {
-    await fixedTest()
-  })
-    .add('PioardiDynamicPool', async function () {
-      await dynamicTest()
+  suite
+    .add('Pioardi:Static:ThreadPool', async function () {
+      await fixedThreadTest()
+    })
+    .add('Pioardi:Dynamic:ThreadPool', async function () {
+      await dynamicThreadTest()
+    })
+    .add('Pioardi:Static:ClusterPool', async function () {
+      await fixedClusterTest()
     })
-    .add('ExternalPool', async function () {
-      await externalPoolTest()
+    .add('Pioardi:Dynamic:ClusterPool', async function () {
+      await dynamicClusterTest()
     })
-  // add listeners
+    // 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
     .run({ async: true })
 }