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