Merge pull request #69 from pioardi/feature/prettier
[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', {
10 maxTasks: 10000
11 })
12 const dynamicPool = new DynamicThreadPool(
13 size / 2,
14 size * 3,
15 './yourWorker.js',
16 { maxTasks: 10000 }
17 )
18
19 // data
20 const workerData = { proof: 'ok' }
21
22 // fixed pool proof
23 async function fixedTest () {
24 let executions = 0
25 const time = Date.now()
26 for (let i = 0; i <= tasks; i++) {
27 fixedPool.execute(workerData).then(res => {
28 executions++
29 if (executions === tasks) {
30 console.log(
31 `Fixed pool take ${Date.now() - time} to work on ${executions} tasks`
32 )
33 }
34 })
35 }
36 }
37
38 async function dynamicTest () {
39 let executions = 0
40 const time = Date.now()
41 for (let i = 0; i <= tasks; i++) {
42 dynamicPool.execute(workerData).then(res => {
43 executions++
44 if (executions === tasks) {
45 console.log(
46 `Dynamic pool take ${Date.now() -
47 time} to work on ${executions} tasks`
48 )
49 }
50 })
51 }
52 }
53
54 async function externalPoolTest () {
55 let executions = 0
56 const time = Date.now()
57 for (let i = 0; i <= tasks; i++) {
58 new Promise((resolve, reject) => {
59 externalPool.acquire(
60 './externalWorker.js',
61 { workerData: workerData },
62 (err, worker) => {
63 if (err) {
64 return reject(err)
65 }
66 worker.on('error', reject)
67 worker.on('message', res => {
68 executions++
69 resolve(res)
70 })
71 }
72 )
73 }).then(res => {
74 if (tasks === executions) {
75 console.log(
76 `External pool take ${Date.now() -
77 time} to work on ${executions} tasks`
78 )
79 }
80 })
81 }
82 }
83
84 async function test () {
85 fixedTest()
86 dynamicTest()
87 externalPoolTest()
88 }
89
90 test()