Properly name and separate each steps at publication. (#199)
[poolifier.git] / benchmarks / myBench.js
1 const { FixedThreadPool } = require('../lib/index')
2 const { DynamicThreadPool } = require('../lib/index')
3 const WorkerThreadsPool = require('worker-threads-pool')
4 const workerpool = require('workerpool')
5 const tasks = 1000
6 const size = 16
7
8 // pools
9 const workerThreadsPool = new WorkerThreadsPool({ max: size })
10 const workerPool = workerpool.pool('./external/workerpoolWorker.js', {
11 minWorkers: size / 2,
12 maxWorkers: size * 3,
13 workerType: 'thread'
14 })
15 const fixedPool = new FixedThreadPool(size, './thread/worker.js', {
16 maxTasks: 10000
17 })
18 const dynamicPool = new DynamicThreadPool(
19 size / 2,
20 size * 3,
21 './thread/worker.js',
22 { maxTasks: 10000 }
23 )
24
25 // data
26 const workerData = { proof: 'ok' }
27
28 // fixed pool proof
29 async function fixedTest () {
30 let executions = 0
31 const time = Date.now()
32 for (let i = 0; i <= tasks; i++) {
33 fixedPool
34 .execute(workerData)
35 .then(res => {
36 executions++
37 if (executions === tasks) {
38 return console.log(
39 `Fixed pool take ${
40 Date.now() - time
41 }ms to work on ${executions} tasks`
42 )
43 }
44 return null
45 })
46 .catch(err => console.error(err))
47 }
48 }
49
50 async function dynamicTest () {
51 let executions = 0
52 const time = Date.now()
53 for (let i = 0; i <= tasks; i++) {
54 dynamicPool
55 .execute(workerData)
56 .then(res => {
57 executions++
58 if (executions === tasks) {
59 return console.log(
60 `Dynamic pool take ${
61 Date.now() - time
62 }ms to work on ${executions} tasks`
63 )
64 }
65 return null
66 })
67 .catch(err => console.error(err))
68 }
69 }
70
71 async function workerThreadsPoolTest () {
72 let executions = 0
73 const time = Date.now()
74 for (let i = 0; i <= tasks; i++) {
75 new Promise((resolve, reject) => {
76 workerThreadsPool.acquire(
77 './external/workerThreadsWorker.js',
78 { workerData: workerData },
79 (err, worker) => {
80 if (err) {
81 return reject(err)
82 }
83 worker.on('error', reject)
84 worker.on('message', res => {
85 executions++
86 resolve(res)
87 })
88 }
89 )
90 })
91 .then(res => {
92 if (tasks === executions) {
93 return console.log(
94 `worker threads pool take ${
95 Date.now() - time
96 }ms to work on ${executions} tasks`
97 )
98 }
99 return null
100 })
101 .catch(err => console.error(err))
102 }
103 }
104
105 async function workerpoolTest () {
106 let executions = 0
107 const time = Date.now()
108 for (let i = 0; i <= tasks; i++) {
109 workerPool
110 .exec('yourFunction', [workerData])
111 .then(res => {
112 executions++
113 if (tasks === executions) {
114 return console.log(
115 `workerpool take ${
116 Date.now() - time
117 }ms to work on ${executions} tasks`
118 )
119 }
120 return null
121 })
122 .catch(err => console.error(err))
123 }
124 }
125
126 async function test () {
127 await fixedTest()
128 await dynamicTest()
129 await workerThreadsPoolTest()
130 await workerpoolTest()
131 }
132
133 test()