75b44e22 |
1 | const FixedThreadPool = require('../lib/fixed') |
2 | const DynamicThreadPool = require('../lib/dynamic') |
3 | const Pool = require('worker-threads-pool') |
4 | const tasks = 1000 |
5 | const size = 10 |
6 | |
7 | // pools |
8 | const externalPool = new Pool({ max: size }) |
9 | const fixedPool = new FixedThreadPool(size, './yourWorker.js', { maxTasks: 10000 }) |
10 | const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 }) |
11 | |
12 | // data |
13 | const workerData = { proof: 'ok' } |
14 | |
15 | // fixed pool proof |
16 | async function fixedTest () { |
17 | let executions = 0 |
18 | const time = Date.now() |
19 | for (let i = 0; i < tasks; i++) { |
20 | await fixedPool.execute(workerData) |
21 | executions++ |
22 | } |
23 | console.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`) |
24 | } |
25 | |
26 | async function dynamicTest () { |
27 | let executions = 0 |
28 | const time = Date.now() |
29 | for (let i = 0; i < tasks; i++) { |
30 | await dynamicPool.execute(workerData) |
31 | executions++ |
32 | } |
33 | console.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`) |
34 | } |
35 | |
36 | async function externalPoolTest () { |
37 | let executions = 0 |
38 | const time = Date.now() |
39 | for (let i = 0; i < tasks; i++) { |
40 | await new Promise((resolve, reject) => { |
41 | externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => { |
42 | if (err) { |
43 | return reject(err) |
44 | } |
45 | worker.on('error', reject) |
46 | worker.on('message', res => { |
47 | executions++ |
48 | resolve(res) |
49 | }) |
50 | }) |
51 | }) |
52 | } |
53 | console.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`) |
54 | } |
55 | |
56 | async function test () { |
57 | await fixedTest() |
58 | await dynamicTest() |
59 | await externalPoolTest() |
60 | } |
61 | |
62 | test() |