chore: v2.6.14
[poolifier.git] / examples / dynamicExample.js
index 94c665a6ae88ee55e15575ff72fbcfd176751842..f36138ac8085df0c07712f28150616dd7095a6f2 100644 (file)
@@ -1,22 +1,38 @@
-const { DynamicThreadPool } = require('poolifier')
-let resolved = 0
-let maxReached = 0
-const pool = new DynamicThreadPool(10, 20, './yourWorker.js', {
-  errorHandler: e => console.error(e),
-  onlineHandler: () => console.log('worker is online')
-})
-pool.emitter.on('busy', () => maxReached++)
+const {
+  DynamicThreadPool,
+  PoolEvents,
+  availableParallelism
+} = require('poolifier')
+
+const pool = new DynamicThreadPool(
+  Math.floor(availableParallelism() / 2),
+  availableParallelism(),
+  './yourWorker.js',
+  {
+    errorHandler: e => console.error(e),
+    onlineHandler: () => console.info('worker is online')
+  }
+)
+let poolFull = 0
+let poolReady = 0
+let poolBusy = 0
+pool.emitter.on(PoolEvents.full, () => poolFull++)
+pool.emitter.on(PoolEvents.ready, () => poolReady++)
+pool.emitter.on(PoolEvents.busy, () => poolBusy++)
 
-const start = Date.now()
+let resolved = 0
+const start = performance.now()
 const iterations = 1000
 for (let i = 1; i <= iterations; i++) {
   pool
     .execute({})
-    .then(res => {
+    .then(() => {
       resolved++
       if (resolved === iterations) {
-        console.log('Time take is ' + (Date.now() - start))
-        return console.log('The pool was busy for ' + maxReached + ' times')
+        console.info('Time taken is ' + (performance.now() - start))
+        console.info('The pool was full for ' + poolFull + ' times')
+        console.info('The pool was ready for ' + poolReady + ' times')
+        return console.info('The pool was busy for ' + poolBusy + ' times')
       }
       return null
     })