1 const FixedThreadPool
= require('../lib/fixed')
2 const DynamicThreadPool
= require('../lib/dynamic')
3 const Pool
= require('worker-threads-pool')
8 const externalPool
= new Pool({ max
: size
})
9 const fixedPool
= new FixedThreadPool(size
, './yourWorker.js', { maxTasks
: 10000 })
10 const dynamicPool
= new DynamicThreadPool(size
/ 2, size
* 3, './yourWorker.js', { maxTasks
: 10000 })
13 const workerData
= { proof
: 'ok' }
16 async
function fixedTest () {
18 const time
= Date
.now()
19 for (let i
= 0; i
<= tasks
; i
++) {
20 fixedPool
.execute(workerData
).then(res
=> {
22 if (executions
=== tasks
) console
.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`)
27 async
function dynamicTest () {
29 const time
= Date
.now()
30 for (let i
= 0; i
<= tasks
; i
++) {
31 dynamicPool
.execute(workerData
).then(res
=> {
33 if (executions
=== tasks
) console
.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`)
38 async
function externalPoolTest () {
40 const time
= Date
.now()
41 for (let i
= 0; i
<= tasks
; i
++) {
42 new Promise((resolve
, reject
) => {
43 externalPool
.acquire('./externalWorker.js', { workerData
: workerData
}, (err
, worker
) => {
47 worker
.on('error', reject
)
48 worker
.on('message', res
=> {
54 if (tasks
=== executions
) console
.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`)
59 async
function test () {