Apply dependencies update (#482)
[poolifier.git] / benchmarks / versus-external-pools / threadjs.js
CommitLineData
75876e4c
APA
1// IMPORT LIBRARIES
2const { spawn, Thread, Worker } = require('threads')
3// FINISH IMPORT LIBRARIES
79e7d368
JB
4const size = Number(process.env.POOL_SIZE)
5const iterations = Number(process.env.NUM_ITERATIONS)
75876e4c
APA
6const 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
12const workers = []
13async function poolify () {
4a6952ff
JB
14 for (let i = 0; i < size; i++) {
15 const worker = await spawn(
16 new Worker('./workers/threadjs/function-to-bench-worker.js')
17 )
75876e4c
APA
18 workers.push(worker)
19 }
20}
21
75876e4c
APA
22async function run () {
23 await poolify()
24 const promises = []
25 for (let i = 0; i < iterations; i++) {
4a6952ff 26 const worker = workers[i % size]
75876e4c
APA
27 promises.push(worker.exposedFunction(data))
28 }
29 await Promise.all(promises)
30 process.exit()
31}
32
33run()