forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandled-rejections.js
More file actions
43 lines (34 loc) · 1.06 KB
/
handled-rejections.js
File metadata and controls
43 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const common = require('../common.js');
// Benchmarks the throughput of processing many promise rejections that are
// initially unhandled, get warned, and then handled asynchronously, exercising
// asyncHandledRejections + processPromiseRejections.
//
// Note: This benchmark uses --unhandled-rejections=warn to avoid crashing
// when promises are temporarily unhandled.
const bench = common.createBenchmark(main, {
n: [1e4, 5e4, 1e5],
}, {
flags: ['--unhandled-rejections=warn'],
});
function main({ n }) {
const rejections = [];
// Suppress warning output during the benchmark
process.removeAllListeners('warning');
for (let i = 0; i < n; i++) {
rejections.push(Promise.reject(i));
}
// Wait for them to be processed as unhandled and warned.
setImmediate(() => {
setImmediate(() => {
bench.start();
for (let i = 0; i < n; i++) {
rejections[i].catch(() => {});
}
// Let processPromiseRejections drain asyncHandledRejections.
setImmediate(() => {
bench.end(n);
});
});
});
}