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