Commit | Line | Data |
---|---|---|
583a27ce JB |
1 | const { FixedThreadPool } = require('../lib/index') |
2 | const { DynamicThreadPool } = require('../lib/index') | |
3 | const WorkerThreadsPool = require('worker-threads-pool') | |
4 | const workerpool = require('workerpool') | |
75b44e22 | 5 | const tasks = 1000 |
106744f7 | 6 | const size = 16 |
75b44e22 | 7 | |
8 | // pools | |
583a27ce JB |
9 | const workerThreadsPool = new WorkerThreadsPool({ max: size }) |
10 | const workerPool = workerpool.pool('./workerpoolWorker.js', { | |
11 | minWorkers: size / 2, | |
12 | maxWorkers: size * 3, | |
13 | workerType: 'thread' | |
14 | }) | |
15 | const fixedPool = new FixedThreadPool(size, './threadWorker.js', { | |
cf9aa6c3 | 16 | maxTasks: 10000 |
17 | }) | |
18 | const dynamicPool = new DynamicThreadPool( | |
19 | size / 2, | |
20 | size * 3, | |
583a27ce | 21 | './threadWorker.js', |
cf9aa6c3 | 22 | { maxTasks: 10000 } |
23 | ) | |
75b44e22 | 24 | |
25 | // data | |
26 | const workerData = { proof: 'ok' } | |
27 | ||
28 | // fixed pool proof | |
29 | async function fixedTest () { | |
30 | let executions = 0 | |
31 | const time = Date.now() | |
8d9ce260 | 32 | for (let i = 0; i <= tasks; i++) { |
583a27ce JB |
33 | fixedPool |
34 | .execute(workerData) | |
35 | .then(res => { | |
36 | executions++ | |
37 | if (executions === tasks) { | |
38 | return console.log( | |
39 | `Fixed pool take ${ | |
40 | Date.now() - time | |
41 | }ms to work on ${executions} tasks` | |
42 | ) | |
43 | } | |
44 | return null | |
45 | }) | |
46 | .catch(err => console.error(err)) | |
75b44e22 | 47 | } |
75b44e22 | 48 | } |
49 | ||
50 | async function dynamicTest () { | |
51 | let executions = 0 | |
52 | const time = Date.now() | |
8d9ce260 | 53 | for (let i = 0; i <= tasks; i++) { |
583a27ce JB |
54 | dynamicPool |
55 | .execute(workerData) | |
56 | .then(res => { | |
57 | executions++ | |
58 | if (executions === tasks) { | |
59 | return console.log( | |
60 | `Dynamic pool take ${ | |
61 | Date.now() - time | |
62 | }ms to work on ${executions} tasks` | |
63 | ) | |
64 | } | |
65 | return null | |
66 | }) | |
67 | .catch(err => console.error(err)) | |
75b44e22 | 68 | } |
75b44e22 | 69 | } |
70 | ||
583a27ce | 71 | async function workerThreadsPoolTest () { |
75b44e22 | 72 | let executions = 0 |
73 | const time = Date.now() | |
8d9ce260 | 74 | for (let i = 0; i <= tasks; i++) { |
75 | new Promise((resolve, reject) => { | |
583a27ce JB |
76 | workerThreadsPool.acquire( |
77 | './workerThreadsWorker.js', | |
cf9aa6c3 | 78 | { workerData: workerData }, |
79 | (err, worker) => { | |
80 | if (err) { | |
81 | return reject(err) | |
82 | } | |
83 | worker.on('error', reject) | |
84 | worker.on('message', res => { | |
85 | executions++ | |
86 | resolve(res) | |
87 | }) | |
75b44e22 | 88 | } |
cf9aa6c3 | 89 | ) |
75b44e22 | 90 | }) |
583a27ce JB |
91 | .then(res => { |
92 | if (tasks === executions) { | |
93 | return console.log( | |
94 | `worker threads pool take ${ | |
95 | Date.now() - time | |
96 | }ms to work on ${executions} tasks` | |
97 | ) | |
98 | } | |
99 | return null | |
100 | }) | |
101 | .catch(err => console.error(err)) | |
102 | } | |
103 | } | |
104 | ||
105 | async function workerpoolTest () { | |
106 | let executions = 0 | |
107 | const time = Date.now() | |
108 | for (let i = 0; i <= tasks; i++) { | |
109 | workerPool | |
110 | .exec('yourFunction', [workerData]) | |
111 | .then(res => { | |
112 | executions++ | |
113 | return null | |
114 | }) | |
115 | .catch(err => console.error(err)) | |
116 | .then(res => { | |
117 | if (tasks === executions) { | |
118 | return console.log( | |
119 | `workerpool take ${ | |
120 | Date.now() - time | |
121 | }ms to work on ${executions} tasks` | |
122 | ) | |
123 | } | |
124 | return null | |
125 | }) | |
126 | .catch(err => console.error(err)) | |
75b44e22 | 127 | } |
75b44e22 | 128 | } |
129 | ||
130 | async function test () { | |
583a27ce JB |
131 | await fixedTest() |
132 | await dynamicTest() | |
133 | await workerThreadsPoolTest() | |
134 | await workerpoolTest() | |
75b44e22 | 135 | } |
136 | ||
137 | test() |