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