build(deps-dev): apply updates
[poolifier.git] / benchmarks / versus-external-pools / threadjs.js
1 // IMPORT LIBRARIES
2 const { spawn, Worker } = require('threads')
3 // FINISH IMPORT LIBRARIES
4 const size = parseInt(process.env.POOL_SIZE)
5 const iterations = parseInt(process.env.NUM_ITERATIONS)
6 const data = {
7 test: 'MYBENCH',
8 taskType: process.env.TASK_TYPE,
9 taskSize: parseInt(process.env.TASK_SIZE)
10 }
11
12 // Threads.js is not really a pool so we need to write few additional code
13 const workers = []
14 async function poolify () {
15 for (let i = 0; i < size; i++) {
16 const worker = await spawn(
17 new Worker('./workers/threadjs/function-to-bench-worker.js')
18 )
19 workers.push(worker)
20 }
21 }
22
23 async function run () {
24 await poolify()
25 const promises = []
26 for (let i = 0; i < iterations; i++) {
27 const worker = workers[i % size]
28 promises.push(worker.exposedFunction(data))
29 }
30 await Promise.all(promises)
31 // eslint-disable-next-line n/no-process-exit
32 process.exit()
33 }
34
35 run()