Use prettierx and eslint native (#85)
[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 ${
47 Date.now() - time
48 } to work on ${executions} tasks`
49 )
50 }
51 })
52 }
53 }
54
55 async function externalPoolTest () {
56 let executions = 0
57 const time = Date.now()
58 for (let i = 0; i <= tasks; i++) {
59 new Promise((resolve, reject) => {
60 externalPool.acquire(
61 './externalWorker.js',
62 { workerData: workerData },
63 (err, worker) => {
64 if (err) {
65 return reject(err)
66 }
67 worker.on('error', reject)
68 worker.on('message', res => {
69 executions++
70 resolve(res)
71 })
72 }
73 )
74 }).then(res => {
75 if (tasks === executions) {
76 console.log(
77 `External pool take ${
78 Date.now() - time
79 } to work on ${executions} tasks`
80 )
81 }
82 })
83 }
84 }
85
86 async function test () {
87 fixedTest()
88 dynamicTest()
89 externalPoolTest()
90 }
91
92 test()