6ea0abfed5e92b28fc5a5e1e833111fe6a77ad37
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
, size
* 2, './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 await fixedPool
.execute(workerData
)
23 console
.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`)
26 async
function dynamicTest () {
28 const time
= Date
.now()
29 for (let i
= 0; i
< tasks
; i
++) {
30 await dynamicPool
.execute(workerData
)
33 console
.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`)
36 async
function externalPoolTest () {
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
) => {
45 worker
.on('error', reject
)
46 worker
.on('message', res
=> {
53 console
.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`)
56 async
function test () {
59 await
externalPoolTest()