chore: generate documentation
[poolifier.git] / examples / dynamicExample.js
... / ...
CommitLineData
1const {
2 DynamicThreadPool,
3 PoolEvents,
4 availableParallelism
5} = require('poolifier')
6
7const pool = new DynamicThreadPool(
8 Math.floor(availableParallelism() / 2),
9 availableParallelism(),
10 './yourWorker.js',
11 {
12 errorHandler: e => console.error(e),
13 onlineHandler: () => console.info('worker is online')
14 }
15)
16let poolFull = 0
17let poolReady = 0
18let poolBusy = 0
19pool.emitter.on(PoolEvents.full, () => poolFull++)
20pool.emitter.on(PoolEvents.ready, () => poolReady++)
21pool.emitter.on(PoolEvents.busy, () => poolBusy++)
22
23let resolved = 0
24const start = performance.now()
25const iterations = 1000
26for (let i = 1; i <= iterations; i++) {
27 pool
28 .execute({})
29 .then(() => {
30 resolved++
31 if (resolved === iterations) {
32 console.info('Time taken is ' + (performance.now() - start))
33 console.info('The pool was full for ' + poolFull + ' times')
34 console.info('The pool was ready for ' + poolReady + ' times')
35 return console.info('The pool was busy for ' + poolBusy + ' times')
36 }
37 return null
38 })
39 .catch(err => console.error(err))
40}