| 1 | const Benchmark = require('benchmark') |
| 2 | const suite = new Benchmark.Suite() |
| 3 | const FixedThreadPool = require('../lib/fixed') |
| 4 | const DynamicThreadPool = require('../lib/dynamic') |
| 5 | const Pool = require('worker-threads-pool') |
| 6 | const size = 40 |
| 7 | const externalPool = new Pool({ max: size }) |
| 8 | |
| 9 | const fixedPool = new FixedThreadPool(size, |
| 10 | './yourWorker.js', { maxTasks: 10000 }) |
| 11 | const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 }) |
| 12 | const workerData = { proof: 'ok' } |
| 13 | let executions = 0 |
| 14 | let executions1 = 0 |
| 15 | |
| 16 | // wait some seconds before start, my pools need to load threads !!! |
| 17 | setTimeout(async () => { |
| 18 | test() |
| 19 | }, 3000) |
| 20 | |
| 21 | async function test () { |
| 22 | // add tests |
| 23 | suite.add('PioardiStaticPool', async function () { |
| 24 | executions++ |
| 25 | await fixedPool.execute(workerData) |
| 26 | }) |
| 27 | |
| 28 | .add('ExternalPool', async function () { |
| 29 | await new Promise((resolve, reject) => { |
| 30 | externalPool.acquire('./externalWorker.js', { workerData: workerData }, (err, worker) => { |
| 31 | if (err) { |
| 32 | return reject(err) |
| 33 | } |
| 34 | executions1++ |
| 35 | worker.on('error', reject) |
| 36 | worker.on('message', res => { |
| 37 | resolve(res) |
| 38 | }) |
| 39 | }) |
| 40 | }) |
| 41 | }) |
| 42 | .add('PioardiDynamicPool', async function () { |
| 43 | await dynamicPool.execute(workerData) |
| 44 | }) |
| 45 | // add listeners |
| 46 | .on('cycle', function (event) { |
| 47 | console.log(String(event.target)) |
| 48 | }) |
| 49 | .on('complete', function () { |
| 50 | console.log(executions) |
| 51 | console.log(executions1) |
| 52 | this.filter('fastest').map('name') |
| 53 | console.log('Fastest is ' + this.filter('fastest').map('name')) |
| 54 | }) |
| 55 | // run async |
| 56 | .run({ async: true }) |
| 57 | } |