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