Properly name and separate each steps at publication. (#199)
[poolifier.git] / benchmarks / myBench.js
CommitLineData
583a27ce
JB
1const { FixedThreadPool } = require('../lib/index')
2const { DynamicThreadPool } = require('../lib/index')
3const WorkerThreadsPool = require('worker-threads-pool')
4const workerpool = require('workerpool')
75b44e22 5const tasks = 1000
106744f7 6const size = 16
75b44e22 7
8// pools
583a27ce 9const workerThreadsPool = new WorkerThreadsPool({ max: size })
325f50bc 10const workerPool = workerpool.pool('./external/workerpoolWorker.js', {
583a27ce
JB
11 minWorkers: size / 2,
12 maxWorkers: size * 3,
13 workerType: 'thread'
14})
325f50bc 15const fixedPool = new FixedThreadPool(size, './thread/worker.js', {
cf9aa6c3 16 maxTasks: 10000
17})
18const dynamicPool = new DynamicThreadPool(
19 size / 2,
20 size * 3,
325f50bc 21 './thread/worker.js',
cf9aa6c3 22 { maxTasks: 10000 }
23)
75b44e22 24
25// data
26const workerData = { proof: 'ok' }
27
28// fixed pool proof
29async function fixedTest () {
30 let executions = 0
31 const time = Date.now()
8d9ce260 32 for (let i = 0; i <= tasks; i++) {
583a27ce
JB
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))
75b44e22 47 }
75b44e22 48}
49
50async function dynamicTest () {
51 let executions = 0
52 const time = Date.now()
8d9ce260 53 for (let i = 0; i <= tasks; i++) {
583a27ce
JB
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))
75b44e22 68 }
75b44e22 69}
70
583a27ce 71async function workerThreadsPoolTest () {
75b44e22 72 let executions = 0
73 const time = Date.now()
8d9ce260 74 for (let i = 0; i <= tasks; i++) {
75 new Promise((resolve, reject) => {
583a27ce 76 workerThreadsPool.acquire(
325f50bc 77 './external/workerThreadsWorker.js',
cf9aa6c3 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 })
75b44e22 88 }
cf9aa6c3 89 )
75b44e22 90 })
583a27ce
JB
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
105async 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++
583a27ce
JB
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))
75b44e22 123 }
75b44e22 124}
125
126async function test () {
583a27ce
JB
127 await fixedTest()
128 await dynamicTest()
129 await workerThreadsPoolTest()
130 await workerpoolTest()
75b44e22 131}
132
133test()