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