Commit | Line | Data |
---|---|---|
75876e4c | 1 | // IMPORT LIBRARIES |
73cda448 | 2 | const { spawn, Worker } = require('threads') |
75876e4c | 3 | // FINISH IMPORT LIBRARIES |
6bd72cd0 JB |
4 | const size = parseInt(process.env.POOL_SIZE) |
5 | const iterations = parseInt(process.env.NUM_ITERATIONS) | |
75876e4c APA |
6 | const 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 | |
13 | const workers = [] | |
14 | async function poolify () { | |
4a6952ff JB |
15 | for (let i = 0; i < size; i++) { |
16 | const worker = await spawn( | |
17 | new Worker('./workers/threadjs/function-to-bench-worker.js') | |
18 | ) | |
75876e4c APA |
19 | workers.push(worker) |
20 | } | |
21 | } | |
22 | ||
75876e4c APA |
23 | async 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 | ||
35 | run() |