http: trace http client by perf_hooks

PR-URL: https://github.com/nodejs/node/pull/42345
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
This commit is contained in:
theanarkh 2022-03-18 23:15:08 +08:00 committed by GitHub
parent 44131ad638
commit 3f3fa6d990
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 9 deletions

View file

@ -5,13 +5,9 @@ const assert = require('assert');
const http = require('http');
const { PerformanceObserver } = require('perf_hooks');
const entries = [];
const obs = new PerformanceObserver(common.mustCallAtLeast((items) => {
items.getEntries().forEach((entry) => {
assert.strictEqual(entry.entryType, 'http');
assert.strictEqual(typeof entry.startTime, 'number');
assert.strictEqual(typeof entry.duration, 'number');
});
entries.push(...items.getEntries());
}));
obs.observe({ type: 'http' });
@ -57,3 +53,20 @@ server.listen(0, common.mustCall(async () => {
]);
server.close();
}));
process.on('exit', () => {
let numberOfHttpClients = 0;
let numberOfHttpRequests = 0;
entries.forEach((entry) => {
assert.strictEqual(entry.entryType, 'http');
assert.strictEqual(typeof entry.startTime, 'number');
assert.strictEqual(typeof entry.duration, 'number');
if (entry.name === 'HttpClient') {
numberOfHttpClients++;
} else if (entry.name === 'HttpRequest') {
numberOfHttpRequests++;
}
});
assert.strictEqual(numberOfHttpClients, 2);
assert.strictEqual(numberOfHttpRequests, 2);
});