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 | }) |
6961ca9a | 11 | let poolBusy = 0 |
aee46736 | 12 | pool.emitter.on(PoolEvents.busy, () => poolBusy++) |
3e460d6d | 13 | |
6961ca9a | 14 | let resolved = 0 |
15e5141f | 15 | const start = performance.now() |
bf962cba | 16 | const iterations = 1000 |
292ad316 | 17 | for (let i = 1; i <= iterations; i++) { |
583a27ce JB |
18 | pool |
19 | .execute({}) | |
7a6a0a96 | 20 | .then(() => { |
583a27ce JB |
21 | resolved++ |
22 | if (resolved === iterations) { | |
53795b86 JB |
23 | console.info('Time taken is ' + (performance.now() - start)) |
24 | return console.info('The pool was busy for ' + poolBusy + ' times') | |
583a27ce JB |
25 | } |
26 | return null | |
27 | }) | |
28 | .catch(err => console.error(err)) | |
6dc67cda | 29 | } |