Add benchmark script (#104)
[poolifier.git] / examples / dynamicExample.js
CommitLineData
60fbd6d6 1const { DynamicThreadPool } = require('poolifier')
bf962cba 2let resolved = 0
3let maxReached = 0
cf9aa6c3 4const pool = new DynamicThreadPool(10, 20, './yourWorker.js', {
5 errorHandler: e => console.error(e),
6 onlineHandler: () => console.log('worker is online')
7})
bf962cba 8pool.emitter.on('FullPool', () => maxReached++)
9
10const start = Date.now()
11const iterations = 1000
12for (let i = 0; i <= iterations; i++) {
583a27ce
JB
13 pool
14 .execute({})
15 .then(res => {
16 resolved++
17 if (resolved === iterations) {
18 console.log('Time take is ' + (Date.now() - start))
19 return console.log('The pool was full for ' + maxReached + ' times')
20 }
21 return null
22 })
23 .catch(err => console.error(err))
bf962cba 24}