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