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