Commit | Line | Data |
---|---|---|
57df5469 | 1 | const Benchmark = require('benchmark') |
2 | const suite = new Benchmark.Suite() | |
583a27ce JB |
3 | const { FixedThreadPool } = require('../lib/index') |
4 | const { DynamicThreadPool } = require('../lib/index') | |
106744f7 | 5 | const size = 30 |
6 | const tasks = 1 | |
57df5469 | 7 | |
583a27ce JB |
8 | const LIST_FORMATTER = new Intl.ListFormat('en-US', { |
9 | style: 'long', | |
10 | type: 'conjunction' | |
11 | }) | |
12 | ||
106744f7 | 13 | // pools |
583a27ce | 14 | const fixedPool = new FixedThreadPool(size, './threadWorker.js', { |
cf9aa6c3 | 15 | maxTasks: 10000 |
16 | }) | |
17 | const dynamicPool = new DynamicThreadPool( | |
18 | size / 2, | |
19 | size * 3, | |
583a27ce | 20 | './threadWorker.js', |
cf9aa6c3 | 21 | { maxTasks: 10000 } |
22 | ) | |
57df5469 | 23 | const workerData = { proof: 'ok' } |
57df5469 | 24 | |
25 | // wait some seconds before start, my pools need to load threads !!! | |
26 | setTimeout(async () => { | |
27 | test() | |
28 | }, 3000) | |
29 | ||
106744f7 | 30 | // fixed pool proof |
31 | async function fixedTest () { | |
32 | return new Promise((resolve, reject) => { | |
33 | let executions = 0 | |
34 | for (let i = 0; i <= tasks; i++) { | |
583a27ce JB |
35 | fixedPool |
36 | .execute(workerData) | |
37 | .then(res => { | |
38 | executions++ | |
39 | if (executions === tasks) { | |
40 | return resolve('FINISH') | |
41 | } | |
42 | return null | |
43 | }) | |
44 | .catch(err => { | |
45 | console.error(err) | |
46 | }) | |
106744f7 | 47 | } |
57df5469 | 48 | }) |
106744f7 | 49 | } |
75b44e22 | 50 | |
106744f7 | 51 | async function dynamicTest () { |
52 | return new Promise((resolve, reject) => { | |
53 | let executions = 0 | |
54 | for (let i = 0; i <= tasks; i++) { | |
583a27ce JB |
55 | dynamicPool |
56 | .execute(workerData) | |
57 | .then(res => { | |
58 | executions++ | |
59 | if (executions === tasks) { | |
60 | return resolve('FINISH') | |
61 | } | |
62 | return null | |
63 | }) | |
64 | .catch(err => console.error(err)) | |
106744f7 | 65 | } |
66 | }) | |
67 | } | |
68 | ||
106744f7 | 69 | async function test () { |
70 | // add tests | |
cf9aa6c3 | 71 | suite |
72 | .add('PioardiStaticPool', async function () { | |
73 | await fixedTest() | |
74 | }) | |
57df5469 | 75 | .add('PioardiDynamicPool', async function () { |
106744f7 | 76 | await dynamicTest() |
77 | }) | |
cf9aa6c3 | 78 | // add listeners |
57df5469 | 79 | .on('cycle', function (event) { |
583a27ce | 80 | console.log(event.target.toString()) |
57df5469 | 81 | }) |
82 | .on('complete', function () { | |
583a27ce JB |
83 | console.log( |
84 | 'Fastest is ' + | |
85 | LIST_FORMATTER.format(this.filter('fastest').map('name')) | |
86 | ) | |
87 | // eslint-disable-next-line no-process-exit | |
88 | process.exit() | |
57df5469 | 89 | }) |
cf9aa6c3 | 90 | // run async |
57df5469 | 91 | .run({ async: true }) |
92 | } |