X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmarks%2Fbench.js;h=0dde5055f466161ca6c9863ddb2ed4b4436b8859;hb=90378059f6a8699aaf43293f920e7cc4ba008431;hp=39d743ad1293b7c851cd6e4180ae2c8ad6047ff5;hpb=57df5469cb6f9ffd7c3501f026f2659b3cf88f06;p=poolifier.git diff --git a/benchmarks/bench.js b/benchmarks/bench.js index 39d743ad..0dde5055 100644 --- a/benchmarks/bench.js +++ b/benchmarks/bench.js @@ -1,58 +1,66 @@ const Benchmark = require('benchmark') const suite = new Benchmark.Suite() const FixedThreadPool = require('../lib/fixed') -const Pool = require('worker-threads-pool') -const size = 80 -const externalPool = new Pool({ max: size }) +const DynamicThreadPool = require('../lib/dynamic') +const size = 30 +const tasks = 1 +// pools const fixedPool = new FixedThreadPool(size, './yourWorker.js', { maxTasks: 10000 }) -// const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 }) +const dynamicPool = new DynamicThreadPool(size / 2, size * 3, './yourWorker.js', { maxTasks: 10000 }) const workerData = { proof: 'ok' } -let executions = 0 // wait some seconds before start, my pools need to load threads !!! setTimeout(async () => { test() }, 3000) +// fixed pool proof +async function fixedTest () { + return new Promise((resolve, reject) => { + let executions = 0 + for (let i = 0; i <= tasks; i++) { + fixedPool.execute(workerData).then(res => { + executions++ + if (executions === tasks) { + resolve('FINISH') + } + }) + } + }) +} + +async function dynamicTest () { + return new Promise((resolve, reject) => { + let executions = 0 + for (let i = 0; i <= tasks; i++) { + dynamicPool.execute(workerData).then(res => { + executions++ + if (executions === tasks) { + resolve('FINISH') + } + }) + } + }) +} + async function test () { // add tests suite.add('PioardiStaticPool', async function () { - executions++ - await fixedPool.execute(workerData) + await fixedTest() }) - .add('ExternalPool', async function () { - await 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 => { - resolve(res) - }) - }) - }) - }) - /* .add('PioardiDynamicPool', async function () { - await dynamicPool.execute(workerData) - }) */ + await dynamicTest() + }) // add listeners .on('cycle', function (event) { console.log(String(event.target)) }) .on('complete', function () { - console.log(executions) this.filter('fastest').map('name') console.log('Fastest is ' + this.filter('fastest').map('name')) }) // run async .run({ async: true }) } - -process.on('SIGKILL', () => { - fixedPool.destroy() - externalPool.destroy() -})