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') |
57df5469 |
5 | const Pool = require('worker-threads-pool') |
75b44e22 |
6 | const size = 40 |
57df5469 |
7 | const externalPool = new Pool({ max: size }) |
8 | |
9 | const fixedPool = new FixedThreadPool(size, |
10 | './yourWorker.js', { maxTasks: 10000 }) |
75b44e22 |
11 | const dynamicPool = new DynamicThreadPool(size, size * 2, './yourWorker.js', { maxTasks: 10000 }) |
57df5469 |
12 | const workerData = { proof: 'ok' } |
13 | let executions = 0 |
75b44e22 |
14 | let executions1 = 0 |
57df5469 |
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 | }) |
75b44e22 |
27 | |
57df5469 |
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 | } |
75b44e22 |
34 | executions1++ |
57df5469 |
35 | worker.on('error', reject) |
36 | worker.on('message', res => { |
37 | resolve(res) |
38 | }) |
39 | }) |
40 | }) |
41 | }) |
57df5469 |
42 | .add('PioardiDynamicPool', async function () { |
43 | await dynamicPool.execute(workerData) |
75b44e22 |
44 | }) |
57df5469 |
45 | // add listeners |
46 | .on('cycle', function (event) { |
47 | console.log(String(event.target)) |
48 | }) |
49 | .on('complete', function () { |
50 | console.log(executions) |
75b44e22 |
51 | console.log(executions1) |
57df5469 |
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 | } |