build: refine ignored files
[poolifier.git] / benchmarks / versus-external-pools / threadjs.js
... / ...
CommitLineData
1// IMPORT LIBRARIES
2const { spawn, Worker } = require('threads')
3// FINISH IMPORT LIBRARIES
4const size = parseInt(process.env.POOL_SIZE)
5const iterations = parseInt(process.env.NUM_ITERATIONS)
6const 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
13const workers = []
14async 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
23async 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
35run()