-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf_hooks: add HttpRequest statistics monitoring #28445
```js const { PerformanceObserver, performance } = require('perf_hooks'); const http = require('http'); const obs = new PerformanceObserver((items) => { const entry = items.getEntries()[0]; console.log(entry.name, entry.duration); }); obs.observe({ entryTypes: ['http'] }); const server = http.Server(function(req, res) { server.close(); res.writeHead(200); res.end('hello world\n'); }); server.listen(0, function() { const req = http.request({ port: this.address().port, path: '/', method: 'POST' }).end(); }); ``` PR-URL: #28486 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
- Loading branch information
Showing
7 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const { PerformanceObserver } = require('perf_hooks'); | ||
|
||
const obs = new PerformanceObserver(common.mustCall((items) => { | ||
const entry = items.getEntries()[0]; | ||
assert.strictEqual(entry.entryType, 'http'); | ||
assert.strictEqual(typeof entry.startTime, 'number'); | ||
assert.strictEqual(typeof entry.duration, 'number'); | ||
}, 2)); | ||
|
||
obs.observe({ entryTypes: ['http'] }); | ||
|
||
const expected = 'Post Body For Test'; | ||
const makeRequest = (options) => { | ||
return new Promise((resolve, reject) => { | ||
http.request(options, common.mustCall((res) => { | ||
resolve(); | ||
})).on('error', reject).end(options.data); | ||
}); | ||
}; | ||
|
||
const server = http.Server(common.mustCall((req, res) => { | ||
let result = ''; | ||
|
||
req.setEncoding('utf8'); | ||
req.on('data', function(chunk) { | ||
result += chunk; | ||
}); | ||
|
||
req.on('end', common.mustCall(function() { | ||
assert.strictEqual(result, expected); | ||
res.writeHead(200); | ||
res.end('hello world\n'); | ||
})); | ||
}, 2)); | ||
|
||
server.listen(0, common.mustCall(async () => { | ||
await Promise.all([ | ||
makeRequest({ | ||
port: server.address().port, | ||
path: '/', | ||
method: 'POST', | ||
data: expected | ||
}), | ||
makeRequest({ | ||
port: server.address().port, | ||
path: '/', | ||
method: 'POST', | ||
data: expected | ||
}) | ||
]); | ||
server.close(); | ||
})); |