Commit | Line | Data |
---|---|---|
75876e4c APA |
1 | // IMPORT LIBRARIES |
2 | const { spawn, Thread, Worker } = require('threads') | |
3 | // FINISH IMPORT LIBRARIES | |
4 | const size = process.env.POOL_SIZE | |
5 | const iterations = 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(new Worker("./workers/threadjs/function-to-bench-worker.js")) | |
16 | workers.push(worker) | |
17 | } | |
18 | } | |
19 | ||
20 | ||
21 | async function run () { | |
22 | await poolify() | |
23 | const promises = [] | |
24 | for (let i = 0; i < iterations; i++) { | |
25 | const worker = workers[(i % size)] | |
26 | promises.push(worker.exposedFunction(data)) | |
27 | } | |
28 | await Promise.all(promises) | |
29 | process.exit() | |
30 | } | |
31 | ||
32 | run() |