1 const { FixedThreadPool
} = require('../lib/index')
2 const { DynamicThreadPool
} = require('../lib/index')
3 const WorkerThreadsPool
= require('worker-threads-pool')
4 const workerpool
= require('workerpool')
9 const workerThreadsPool
= new WorkerThreadsPool({ max
: size
})
10 const workerPool
= workerpool
.pool('./workerpoolWorker.js', {
15 const fixedPool
= new FixedThreadPool(size
, './threadWorker.js', {
18 const dynamicPool
= new DynamicThreadPool(
26 const workerData
= { proof
: 'ok' }
29 async
function fixedTest () {
31 const time
= Date
.now()
32 for (let i
= 0; i
<= tasks
; i
++) {
37 if (executions
=== tasks
) {
41 }ms to work on ${executions} tasks`
46 .catch(err
=> console
.error(err
))
50 async
function dynamicTest () {
52 const time
= Date
.now()
53 for (let i
= 0; i
<= tasks
; i
++) {
58 if (executions
=== tasks
) {
62 }ms to work on ${executions} tasks`
67 .catch(err
=> console
.error(err
))
71 async
function workerThreadsPoolTest () {
73 const time
= Date
.now()
74 for (let i
= 0; i
<= tasks
; i
++) {
75 new Promise((resolve
, reject
) => {
76 workerThreadsPool
.acquire(
77 './workerThreadsWorker.js',
78 { workerData
: workerData
},
83 worker
.on('error', reject
)
84 worker
.on('message', res
=> {
92 if (tasks
=== executions
) {
94 `worker threads pool take ${
96 }ms to work on ${executions} tasks`
101 .catch(err
=> console
.error(err
))
105 async
function workerpoolTest () {
107 const time
= Date
.now()
108 for (let i
= 0; i
<= tasks
; i
++) {
110 .exec('yourFunction', [workerData
])
115 .catch(err
=> console
.error(err
))
117 if (tasks
=== executions
) {
121 }ms to work on ${executions} tasks`
126 .catch(err
=> console
.error(err
))
130 async
function test () {
133 await
workerThreadsPoolTest()
134 await
workerpoolTest()