Merge branch 'master' of https://github.com/pioardi/node-pool
[poolifier.git] / benchmarks / bench.js
CommitLineData
57df5469 1const Benchmark = require('benchmark')
2const suite = new Benchmark.Suite()
3const FixedThreadPool = require('../lib/fixed')
75b44e22 4const DynamicThreadPool = require('../lib/dynamic')
57df5469 5const Pool = require('worker-threads-pool')
75b44e22 6const size = 40
57df5469 7const externalPool = new Pool({ max: size })
8
9const fixedPool = new FixedThreadPool(size,
10 './yourWorker.js', { maxTasks: 10000 })
75b44e22 11const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 })
57df5469 12const workerData = { proof: 'ok' }
13let executions = 0
75b44e22 14let executions1 = 0
57df5469 15
16// wait some seconds before start, my pools need to load threads !!!
17setTimeout(async () => {
18 test()
19}, 3000)
20
21async function test () {
22 // add tests
23 suite.add('PioardiStaticPool', async function () {
24 executions++
25 await fixedPool.execute(workerData)
26 })
75b44e22 27
57df5469 28 .add('ExternalPool', async function () {
29 await new Promise((resolve, reject) => {
30 externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
31 if (err) {
32 return reject(err)
33 }
75b44e22 34 executions1++
57df5469 35 worker.on('error', reject)
36 worker.on('message', res => {
37 resolve(res)
38 })
39 })
40 })
41 })
57df5469 42 .add('PioardiDynamicPool', async function () {
43 await dynamicPool.execute(workerData)
75b44e22 44 })
57df5469 45 // add listeners
46 .on('cycle', function (event) {
47 console.log(String(event.target))
48 })
49 .on('complete', function () {
50 console.log(executions)
75b44e22 51 console.log(executions1)
57df5469 52 this.filter('fastest').map('name')
53 console.log('Fastest is ' + this.filter('fastest').map('name'))
54 })
55 // run async
56 .run({ async: true })
57}