fix: fix formatting issue.
[poolifier.git] / examples / javascript / dynamicExample.js
CommitLineData
670ede1c 1'use strict'
6961ca9a
JB
2const {
3 DynamicThreadPool,
4 PoolEvents,
5 availableParallelism
6} = require('poolifier')
7
8const 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 17let poolFull = 0
2431bdb4 18let poolReady = 0
164d950a 19let poolBusy = 0
aee46736 20pool.emitter.on(PoolEvents.full, () => poolFull++)
2431bdb4 21pool.emitter.on(PoolEvents.ready, () => poolReady++)
aee46736 22pool.emitter.on(PoolEvents.busy, () => poolBusy++)
bf962cba 23
6961ca9a 24let resolved = 0
15e5141f 25const start = performance.now()
bf962cba 26const iterations = 1000
292ad316 27for (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}