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