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