Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-cluster...
[poolifier.git] / examples / javascript / fixedExample.cjs
1 'use strict'
2 const {
3 FixedThreadPool,
4 PoolEvents,
5 availableParallelism
6 } = require('poolifier')
7
8 const pool = new FixedThreadPool(availableParallelism(), './yourWorker.cjs', {
9 errorHandler: e => console.error(e),
10 onlineHandler: () => console.info('worker is online')
11 })
12 let poolReady = 0
13 let poolBusy = 0
14 pool.emitter?.on(PoolEvents.ready, () => poolReady++)
15 pool.emitter?.on(PoolEvents.busy, () => poolBusy++)
16
17 let resolved = 0
18 const start = performance.now()
19 const iterations = 1000
20 for (let i = 1; i <= iterations; i++) {
21 pool
22 .execute()
23 .then(() => {
24 resolved++
25 if (resolved === iterations) {
26 console.info(
27 `Time taken is ${(performance.now() - start).toFixed(2)}ms`
28 )
29 console.info(`The pool was ready for ${poolReady} times`)
30 console.info(`The pool was busy for ${poolBusy} times`)
31 return pool.destroy()
32 }
33 return undefined
34 })
35 .catch(err => console.error(err))
36 }