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