docs: refine .md files
[poolifier.git] / examples / 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 {
13 errorHandler: e => console.error(e),
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) {
53795b86
JB
33 console.info('Time taken is ' + (performance.now() - start))
34 console.info('The pool was full for ' + poolFull + ' times')
2431bdb4 35 console.info('The pool was ready for ' + poolReady + ' times')
53795b86 36 return console.info('The pool was busy for ' + poolBusy + ' times')
583a27ce
JB
37 }
38 return null
39 })
40 .catch(err => console.error(err))
bf962cba 41}