docs: refine README.md
[poolifier.git] / examples / dynamicExample.js
CommitLineData
aee46736 1const { DynamicThreadPool, PoolEvents } = require('poolifier')
bf962cba 2let resolved = 0
164d950a
JB
3let poolFull = 0
4let poolBusy = 0
cf9aa6c3 5const pool = new DynamicThreadPool(10, 20, './yourWorker.js', {
6 errorHandler: e => console.error(e),
53795b86 7 onlineHandler: () => console.info('worker is online')
cf9aa6c3 8})
aee46736
JB
9pool.emitter.on(PoolEvents.full, () => poolFull++)
10pool.emitter.on(PoolEvents.busy, () => poolBusy++)
bf962cba 11
15e5141f 12const start = performance.now()
bf962cba 13const iterations = 1000
292ad316 14for (let i = 1; i <= iterations; i++) {
583a27ce
JB
15 pool
16 .execute({})
7a6a0a96 17 .then(() => {
583a27ce
JB
18 resolved++
19 if (resolved === iterations) {
53795b86
JB
20 console.info('Time taken is ' + (performance.now() - start))
21 console.info('The pool was full for ' + poolFull + ' times')
22 return console.info('The pool was busy for ' + poolBusy + ' times')
583a27ce
JB
23 }
24 return null
25 })
26 .catch(err => console.error(err))
bf962cba 27}