Bump expect from 25.2.3 to 25.2.4
[poolifier.git] / benchmarks / myBench.js
1 const FixedThreadPool = require('../lib/fixed')
2 const DynamicThreadPool = require('../lib/dynamic')
3 const Pool = require('worker-threads-pool')
4 const tasks = 1000
5 const size = 16
6
7 // pools
8 const externalPool = new Pool({ max: size })
9 const fixedPool = new FixedThreadPool(size, './yourWorker.js', { maxTasks: 10000 })
10 const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './yourWorker.js', { maxTasks: 10000 })
11
12 // data
13 const workerData = { proof: 'ok' }
14
15 // fixed pool proof
16 async function fixedTest () {
17 let executions = 0
18 const time = Date.now()
19 for (let i = 0; i <= tasks; i++) {
20 fixedPool.execute(workerData).then(res => {
21 executions++
22 if (executions === tasks) console.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`)
23 })
24 }
25 }
26
27 async function dynamicTest () {
28 let executions = 0
29 const time = Date.now()
30 for (let i = 0; i <= tasks; i++) {
31 dynamicPool.execute(workerData).then(res => {
32 executions++
33 if (executions === tasks) console.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`)
34 })
35 }
36 }
37
38 async function externalPoolTest () {
39 let executions = 0
40 const time = Date.now()
41 for (let i = 0; i <= tasks; i++) {
42 new Promise((resolve, reject) => {
43 externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => {
44 if (err) {
45 return reject(err)
46 }
47 worker.on('error', reject)
48 worker.on('message', res => {
49 executions++
50 resolve(res)
51 })
52 })
53 }).then(res => {
54 if (tasks === executions) console.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`)
55 })
56 }
57 }
58
59 async function test () {
60 fixedTest()
61 dynamicTest()
62 externalPoolTest()
63 }
64
65 test()