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