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