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')
10 const externalPool
= new Pool({ max
: size
})
11 const fixedPool
= new FixedThreadPool(size
,
12 './yourWorker.js', { maxTasks
: 10000 })
13 const dynamicPool
= new DynamicThreadPool(size
/ 2, size
* 3, './yourWorker.js', { maxTasks
: 10000 })
14 const workerData
= { proof
: 'ok' }
16 // wait some seconds before start, my pools need to load threads !!!
17 setTimeout(async () => {
22 async
function fixedTest () {
23 return new Promise((resolve
, reject
) => {
25 for (let i
= 0; i
<= tasks
; i
++) {
26 fixedPool
.execute(workerData
).then(res
=> {
28 if (executions
=== tasks
) {
36 async
function dynamicTest () {
37 return new Promise((resolve
, reject
) => {
39 for (let i
= 0; i
<= tasks
; i
++) {
40 dynamicPool
.execute(workerData
).then(res
=> {
42 if (executions
=== tasks
) {
50 async
function externalPoolTest () {
51 return new Promise((resolve
, reject
) => {
53 for (let i
= 0; i
<= tasks
; i
++) {
54 new Promise((resolve
, reject
) => {
55 externalPool
.acquire('./externalWorker.js', { workerData
: workerData
}, (err
, worker
) => {
59 worker
.on('error', reject
)
60 worker
.on('message', res
=> {
66 if (tasks
=== executions
) {
74 async
function test () {
76 suite
.add('PioardiStaticPool', async
function () {
79 .add('PioardiDynamicPool', async
function () {
82 .add('ExternalPool', async
function () {
83 await
externalPoolTest()
86 .on('cycle', function (event
) {
87 console
.log(String(event
.target
))
89 .on('complete', function () {
90 this.filter('fastest').map('name')
91 console
.log('Fastest is ' + this.filter('fastest').map('name'))