build(deps-dev): bump @typescript-eslint/eslint-plugin
[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
8538ea4c
JB
20pool.emitter?.on(PoolEvents.full, () => poolFull++)
21pool.emitter?.on(PoolEvents.ready, () => poolReady++)
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) {
8538ea4c
JB
33 console.info(
34 `Time taken is ${(performance.now() - start).toFixed(2)}ms`
35 )
1c132fec
JB
36 console.info(`The pool was full for ${poolFull} times`)
37 console.info(`The pool was ready for ${poolReady} times`)
8538ea4c
JB
38 console.info(`The pool was busy for ${poolBusy} times`)
39 return pool.destroy()
583a27ce 40 }
fefd3cef 41 return undefined
583a27ce 42 })
041dc05b 43 .catch(err => console.error(err))
bf962cba 44}