Bump expect from 25.2.3 to 25.2.4
[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')
106744f7 5const size = 30
6const tasks = 1
57df5469 7
106744f7 8// pools
57df5469 9const fixedPool = new FixedThreadPool(size,
10 './yourWorker.js', { maxTasks: 10000 })
106744f7 11const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './yourWorker.js', { maxTasks: 10000 })
57df5469 12const workerData = { proof: 'ok' }
57df5469 13
14// wait some seconds before start, my pools need to load threads !!!
15setTimeout(async () => {
16 test()
17}, 3000)
18
106744f7 19// fixed pool proof
20async function fixedTest () {
21 return new Promise((resolve, reject) => {
22 let executions = 0
23 for (let i = 0; i <= tasks; i++) {
24 fixedPool.execute(workerData).then(res => {
25 executions++
26 if (executions === tasks) {
27 resolve('FINISH')
28 }
29 })
30 }
57df5469 31 })
106744f7 32}
75b44e22 33
106744f7 34async function dynamicTest () {
35 return new Promise((resolve, reject) => {
36 let executions = 0
37 for (let i = 0; i <= tasks; i++) {
38 dynamicPool.execute(workerData).then(res => {
39 executions++
40 if (executions === tasks) {
41 resolve('FINISH')
42 }
43 })
44 }
45 })
46}
47
106744f7 48async function test () {
49 // add tests
50 suite.add('PioardiStaticPool', async function () {
51 await fixedTest()
52 })
57df5469 53 .add('PioardiDynamicPool', async function () {
106744f7 54 await dynamicTest()
55 })
57df5469 56 // add listeners
57 .on('cycle', function (event) {
58 console.log(String(event.target))
59 })
60 .on('complete', function () {
57df5469 61 this.filter('fastest').map('name')
62 console.log('Fastest is ' + this.filter('fastest').map('name'))
63 })
64 // run async
65 .run({ async: true })
66}