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