| 1 | const { |
| 2 | FixedThreadPool, |
| 3 | PoolEvents, |
| 4 | availableParallelism |
| 5 | } = require('poolifier') |
| 6 | |
| 7 | const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', { |
| 8 | errorHandler: e => console.error(e), |
| 9 | onlineHandler: () => console.info('worker is online') |
| 10 | }) |
| 11 | let poolReady = 0 |
| 12 | let poolBusy = 0 |
| 13 | pool.emitter.on(PoolEvents.ready, () => poolReady++) |
| 14 | pool.emitter.on(PoolEvents.busy, () => poolBusy++) |
| 15 | |
| 16 | let resolved = 0 |
| 17 | const start = performance.now() |
| 18 | const iterations = 1000 |
| 19 | for (let i = 1; i <= iterations; i++) { |
| 20 | pool |
| 21 | .execute({}) |
| 22 | .then(() => { |
| 23 | resolved++ |
| 24 | if (resolved === iterations) { |
| 25 | console.info('Time taken is ' + (performance.now() - start)) |
| 26 | console.info('The pool was ready for ' + poolReady + ' times') |
| 27 | return console.info('The pool was busy for ' + poolBusy + ' times') |
| 28 | } |
| 29 | return null |
| 30 | }) |
| 31 | .catch(err => console.error(err)) |
| 32 | } |