From: aardizio Date: Tue, 21 Jan 2020 19:01:07 +0000 (+0100) Subject: New benchmarking without any library to better understand how the bench is executed... X-Git-Tag: v0.0.1~12 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=75b44e22e0bc7bb88788c89cf9204f24c4a96360;p=poolifier.git New benchmarking without any library to better understand how the bench is executed, the project is now dependencies free! --- diff --git a/benchmarks/bench.js b/benchmarks/bench.js index 39d743ad..a9515bff 100644 --- a/benchmarks/bench.js +++ b/benchmarks/bench.js @@ -1,15 +1,17 @@ const Benchmark = require('benchmark') const suite = new Benchmark.Suite() const FixedThreadPool = require('../lib/fixed') +const DynamicThreadPool = require('../lib/dynamic') const Pool = require('worker-threads-pool') -const size = 80 +const size = 40 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 dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 }) const workerData = { proof: 'ok' } let executions = 0 +let executions1 = 0 // wait some seconds before start, my pools need to load threads !!! setTimeout(async () => { @@ -22,12 +24,14 @@ async function test () { executions++ await fixedPool.execute(workerData) }) + .add('ExternalPool', async function () { await new Promise((resolve, reject) => { externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => { if (err) { return reject(err) } + executions1++ worker.on('error', reject) worker.on('message', res => { resolve(res) @@ -35,24 +39,19 @@ async function test () { }) }) }) - /* .add('PioardiDynamicPool', async function () { await dynamicPool.execute(workerData) - }) */ + }) // add listeners .on('cycle', function (event) { console.log(String(event.target)) }) .on('complete', function () { console.log(executions) + console.log(executions1) 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() -}) diff --git a/benchmarks/myBench.js b/benchmarks/myBench.js new file mode 100644 index 00000000..6ea0abfe --- /dev/null +++ b/benchmarks/myBench.js @@ -0,0 +1,62 @@ +const FixedThreadPool = require('../lib/fixed') +const DynamicThreadPool = require('../lib/dynamic') +const Pool = require('worker-threads-pool') +const tasks = 1000 +const size = 10 + +// 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 }) + +// data +const workerData = { proof: 'ok' } + +// fixed pool proof +async function fixedTest () { + let executions = 0 + const time = Date.now() + for (let i = 0; i < tasks; i++) { + await fixedPool.execute(workerData) + executions++ + } + 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++ + } + 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) + } + worker.on('error', reject) + worker.on('message', res => { + executions++ + resolve(res) + }) + }) + }) + } + console.log(`External pool take ${Date.now() - time} to work on ${executions} tasks`) +} + +async function test () { + await fixedTest() + await dynamicTest() + await externalPoolTest() +} + +test() diff --git a/examples/dynamicExample.js b/examples/dynamicExample.js index d483684d..68c6b6f1 100644 --- a/examples/dynamicExample.js +++ b/examples/dynamicExample.js @@ -1,7 +1,7 @@ const DynamicThreadPool = require('../lib/dynamic') let resolved = 0 let maxReached = 0 -const pool = new DynamicThreadPool(100, 200, './yourWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') }) +const pool = new DynamicThreadPool(10, 20, './yourWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') }) pool.emitter.on('FullPool', () => maxReached++) const start = Date.now() diff --git a/lib/util.js b/lib/util.js deleted file mode 100644 index f894202b..00000000 --- a/lib/util.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Contains utility functions - * @author Alessandro Pio Ardizio - * @since 0.0.1 - */ - -const uuid = require('uuid/v1') -/** - * Return an id to be associated to a node. - */ -module.exports.generateID = () => { - return uuid() -} diff --git a/package.json b/package.json index fe32dfd3..20856725 100644 --- a/package.json +++ b/package.json @@ -36,9 +36,6 @@ "url": "https://github.com/pioardi/node-thread-pool/issues" }, "homepage": "https://github.com/pioardi/node-thread-pool#readme", - "dependencies": { - "uuid": "^3.4.0" - }, "devDependencies": { "benchmark": "^2.1.4", "coveralls": "^3.0.9", diff --git a/tests/util.test.js b/tests/util.test.js deleted file mode 100644 index 71782ea1..00000000 --- a/tests/util.test.js +++ /dev/null @@ -1,10 +0,0 @@ -const expect = require('expect') -const { generateID } = require('../lib/util') - -describe('Utility Tests ', () => { - it('Generate an id', () => { - const res = generateID() - expect(res).toBeTruthy() - expect(typeof res).toBe('string') - }) -})