1 const Benchmark
= require('benchmark')
2 const suite
= new Benchmark
.Suite()
3 const FixedThreadPool
= require('../lib/fixed')
4 const DynamicThreadPool
= require('../lib/dynamic')
5 const Pool
= require('worker-threads-pool')
7 const externalPool
= new Pool({ max
: size
})
9 const fixedPool
= new FixedThreadPool(size
,
10 './yourWorker.js', { maxTasks
: 10000 })
11 const dynamicPool
= new DynamicThreadPool(size
, size
* 2, './yourWorker.js', { maxTasks
: 10000 })
12 const workerData
= { proof
: 'ok' }
16 // wait some seconds before start, my pools need to load threads !!!
17 setTimeout(async () => {
21 async
function test () {
23 suite
.add('PioardiStaticPool', async
function () {
25 await fixedPool
.execute(workerData
)
28 .add('ExternalPool', async
function () {
29 await
new Promise((resolve
, reject
) => {
30 externalPool
.acquire('./externalWorker.js', { workerData
: workerData
}, (err
, worker
) => {
35 worker
.on('error', reject
)
36 worker
.on('message', res
=> {
42 .add('PioardiDynamicPool', async
function () {
43 await dynamicPool
.execute(workerData
)
46 .on('cycle', function (event
) {
47 console
.log(String(event
.target
))
49 .on('complete', function () {
50 console
.log(executions
)
51 console
.log(executions1
)
52 this.filter('fastest').map('name')
53 console
.log('Fastest is ' + this.filter('fastest').map('name'))