| 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 size = 30 |
| 6 | const tasks = 1 |
| 7 | |
| 8 | // pools |
| 9 | const fixedPool = new FixedThreadPool(size, './yourWorker.js', { |
| 10 | maxTasks: 10000 |
| 11 | }) |
| 12 | const dynamicPool = new DynamicThreadPool( |
| 13 | size / 2, |
| 14 | size * 3, |
| 15 | './yourWorker.js', |
| 16 | { maxTasks: 10000 } |
| 17 | ) |
| 18 | const workerData = { proof: 'ok' } |
| 19 | |
| 20 | // wait some seconds before start, my pools need to load threads !!! |
| 21 | setTimeout(async () => { |
| 22 | test() |
| 23 | }, 3000) |
| 24 | |
| 25 | // fixed pool proof |
| 26 | async function fixedTest () { |
| 27 | return new Promise((resolve, reject) => { |
| 28 | let executions = 0 |
| 29 | for (let i = 0; i <= tasks; i++) { |
| 30 | fixedPool.execute(workerData).then(res => { |
| 31 | executions++ |
| 32 | if (executions === tasks) { |
| 33 | resolve('FINISH') |
| 34 | } |
| 35 | }) |
| 36 | } |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | async function dynamicTest () { |
| 41 | return new Promise((resolve, reject) => { |
| 42 | let executions = 0 |
| 43 | for (let i = 0; i <= tasks; i++) { |
| 44 | dynamicPool.execute(workerData).then(res => { |
| 45 | executions++ |
| 46 | if (executions === tasks) { |
| 47 | resolve('FINISH') |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | async function test () { |
| 55 | // add tests |
| 56 | suite |
| 57 | .add('PioardiStaticPool', async function () { |
| 58 | await fixedTest() |
| 59 | }) |
| 60 | .add('PioardiDynamicPool', async function () { |
| 61 | await dynamicTest() |
| 62 | }) |
| 63 | // add listeners |
| 64 | .on('cycle', function (event) { |
| 65 | console.log(String(event.target)) |
| 66 | }) |
| 67 | .on('complete', function () { |
| 68 | this.filter('fastest').map('name') |
| 69 | console.log('Fastest is ' + this.filter('fastest').map('name')) |
| 70 | }) |
| 71 | // run async |
| 72 | .run({ async: true }) |
| 73 | } |