X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=benchmarks%2FmyBench.js;h=8835f1f5d4cec31feba03012fc7974df02810f91;hb=39deb558414d950b66aba79beabc20c90b85997a;hp=6ea0abfed5e92b28fc5a5e1e833111fe6a77ad37;hpb=75b44e22e0bc7bb88788c89cf9204f24c4a96360;p=poolifier.git diff --git a/benchmarks/myBench.js b/benchmarks/myBench.js index 6ea0abfe..8835f1f5 100644 --- a/benchmarks/myBench.js +++ b/benchmarks/myBench.js @@ -2,12 +2,19 @@ const FixedThreadPool = require('../lib/fixed') const DynamicThreadPool = require('../lib/dynamic') const Pool = require('worker-threads-pool') const tasks = 1000 -const size = 10 +const size = 16 // pools const externalPool = new Pool({ max: size }) -const fixedPool = new FixedThreadPool(size, './yourWorker.js', { maxTasks: 10000 }) -const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 }) +const fixedPool = new FixedThreadPool(size, './yourWorker.js', { + maxTasks: 10000 +}) +const dynamicPool = new DynamicThreadPool( + size / 2, + size * 3, + './yourWorker.js', + { maxTasks: 10000 } +) // data const workerData = { proof: 'ok' } @@ -16,47 +23,68 @@ const workerData = { proof: 'ok' } async function fixedTest () { let executions = 0 const time = Date.now() - for (let i = 0; i < tasks; i++) { - await fixedPool.execute(workerData) - executions++ + for (let i = 0; i <= tasks; i++) { + fixedPool.execute(workerData).then(res => { + executions++ + if (executions === tasks) { + console.log( + `Fixed pool take ${Date.now() - time} to work on ${executions} tasks` + ) + } + }) } - console.log(`Fixed pool take ${Date.now() - time} to work on ${executions} tasks`) } async function dynamicTest () { let executions = 0 const time = Date.now() - for (let i = 0; i < tasks; i++) { - await dynamicPool.execute(workerData) - executions++ + for (let i = 0; i <= tasks; i++) { + dynamicPool.execute(workerData).then(res => { + executions++ + if (executions === tasks) { + console.log( + `Dynamic pool take ${Date.now() - + time} to work on ${executions} tasks` + ) + } + }) } - console.log(`Dynamic pool take ${Date.now() - time} to work on ${executions} tasks`) } async function externalPoolTest () { let executions = 0 const time = Date.now() - for (let i = 0; i < tasks; i++) { - await new Promise((resolve, reject) => { - externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => { - if (err) { - return reject(err) + for (let i = 0; i <= tasks; i++) { + new Promise((resolve, reject) => { + externalPool.acquire( + './externalWorker.js', + { workerData: workerData }, + (err, worker) => { + if (err) { + return reject(err) + } + worker.on('error', reject) + worker.on('message', res => { + executions++ + resolve(res) + }) } - worker.on('error', reject) - worker.on('message', res => { - executions++ - resolve(res) - }) - }) + ) + }).then(res => { + if (tasks === executions) { + console.log( + `External pool take ${Date.now() - + time} to work on ${executions} tasks` + ) + } }) } - console.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`) } async function test () { - await fixedTest() - await dynamicTest() - await externalPoolTest() + fixedTest() + dynamicTest() + externalPoolTest() } test()