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 |
cf9aa6c3 |
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 | ) |
57df5469 |
18 | const workerData = { proof: 'ok' } |
57df5469 |
19 | |
20 | // wait some seconds before start, my pools need to load threads !!! |
21 | setTimeout(async () => { |
22 | test() |
23 | }, 3000) |
24 | |
106744f7 |
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 | } |
57df5469 |
37 | }) |
106744f7 |
38 | } |
75b44e22 |
39 | |
106744f7 |
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 | |
106744f7 |
54 | async function test () { |
55 | // add tests |
cf9aa6c3 |
56 | suite |
57 | .add('PioardiStaticPool', async function () { |
58 | await fixedTest() |
59 | }) |
57df5469 |
60 | .add('PioardiDynamicPool', async function () { |
106744f7 |
61 | await dynamicTest() |
62 | }) |
cf9aa6c3 |
63 | // add listeners |
57df5469 |
64 | .on('cycle', function (event) { |
65 | console.log(String(event.target)) |
66 | }) |
67 | .on('complete', function () { |
57df5469 |
68 | this.filter('fastest').map('name') |
69 | console.log('Fastest is ' + this.filter('fastest').map('name')) |
70 | }) |
cf9aa6c3 |
71 | // run async |
57df5469 |
72 | .run({ async: true }) |
73 | } |