chore: v2.7.4
[poolifier.git] / examples / javascript / fixedExample.js
CommitLineData
670ede1c 1'use strict'
6961ca9a
JB
2const {
3 FixedThreadPool,
4 PoolEvents,
5 availableParallelism
6} = require('poolifier')
7
8const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
041dc05b 9 errorHandler: e => console.error(e),
53795b86 10 onlineHandler: () => console.info('worker is online')
cf9aa6c3 11})
2431bdb4 12let poolReady = 0
6961ca9a 13let poolBusy = 0
2431bdb4 14pool.emitter.on(PoolEvents.ready, () => poolReady++)
aee46736 15pool.emitter.on(PoolEvents.busy, () => poolBusy++)
3e460d6d 16
6961ca9a 17let resolved = 0
15e5141f 18const start = performance.now()
bf962cba 19const iterations = 1000
292ad316 20for (let i = 1; i <= iterations; i++) {
583a27ce 21 pool
8923de44 22 .execute()
7a6a0a96 23 .then(() => {
583a27ce
JB
24 resolved++
25 if (resolved === iterations) {
1c132fec
JB
26 console.info(`Time taken is ${performance.now() - start}`)
27 console.info(`The pool was ready for ${poolReady} times`)
28 return console.info(`The pool was busy for ${poolBusy} times`)
583a27ce
JB
29 }
30 return null
31 })
041dc05b 32 .catch(err => console.error(err))
6dc67cda 33}