Error handling and unit tests
[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')
106744f7 6const size = 30
7const tasks = 1
57df5469 8
106744f7 9// pools
10const externalPool = new Pool({ max: size })
57df5469 11const fixedPool = new FixedThreadPool(size,
12 './yourWorker.js', { maxTasks: 10000 })
106744f7 13const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './yourWorker.js', { maxTasks: 10000 })
57df5469 14const workerData = { proof: 'ok' }
57df5469 15
16// wait some seconds before start, my pools need to load threads !!!
17setTimeout(async () => {
18 test()
19}, 3000)
20
106744f7 21// fixed pool proof
22async function fixedTest () {
23 return new Promise((resolve, reject) => {
24 let executions = 0
25 for (let i = 0; i <= tasks; i++) {
26 fixedPool.execute(workerData).then(res => {
27 executions++
28 if (executions === tasks) {
29 resolve('FINISH')
30 }
31 })
32 }
57df5469 33 })
106744f7 34}
75b44e22 35
106744f7 36async function dynamicTest () {
37 return new Promise((resolve, reject) => {
38 let executions = 0
39 for (let i = 0; i <= tasks; i++) {
40 dynamicPool.execute(workerData).then(res => {
41 executions++
42 if (executions === tasks) {
43 resolve('FINISH')
44 }
45 })
46 }
47 })
48}
49
50async function externalPoolTest () {
51 return new Promise((resolve, reject) => {
52 let executions = 0
53 for (let i = 0; i <= tasks; i++) {
54 new Promise((resolve, reject) => {
57df5469 55 externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
56 if (err) {
57 return reject(err)
58 }
59 worker.on('error', reject)
60 worker.on('message', res => {
106744f7 61 executions++
57df5469 62 resolve(res)
63 })
64 })
106744f7 65 }).then(res => {
66 if (tasks === executions) {
67 resolve('FINISH')
68 }
57df5469 69 })
106744f7 70 }
71 })
72}
73
74async function test () {
75 // add tests
76 suite.add('PioardiStaticPool', async function () {
77 await fixedTest()
78 })
57df5469 79 .add('PioardiDynamicPool', async function () {
106744f7 80 await dynamicTest()
81 })
82 .add('ExternalPool', async function () {
83 await externalPoolTest()
75b44e22 84 })
57df5469 85 // add listeners
86 .on('cycle', function (event) {
87 console.log(String(event.target))
88 })
89 .on('complete', function () {
57df5469 90 this.filter('fastest').map('name')
91 console.log('Fastest is ' + this.filter('fastest').map('name'))
92 })
93 // run async
94 .run({ async: true })
95}