Adding a changelog file , removing other library dev dependencies and bench, add...
[poolifier.git] / benchmarks / bench.js
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 size = 30
6 const tasks = 1
7
8 // pools
9 const fixedPool = new FixedThreadPool(size,
10 './yourWorker.js', { maxTasks: 10000 })
11 const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './yourWorker.js', { maxTasks: 10000 })
12 const workerData = { proof: 'ok' }
13
14 // wait some seconds before start, my pools need to load threads !!!
15 setTimeout(async () => {
16 test()
17 }, 3000)
18
19 // fixed pool proof
20 async 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 }
31 })
32 }
33
34 async 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
48 async function test () {
49 // add tests
50 suite.add('PioardiStaticPool', async function () {
51 await fixedTest()
52 })
53 .add('PioardiDynamicPool', async function () {
54 await dynamicTest()
55 })
56 // add listeners
57 .on('cycle', function (event) {
58 console.log(String(event.target))
59 })
60 .on('complete', function () {
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 }