The [Performance Observer][] API can be used to collect basic performance
metrics for each Http2Session
and Http2Stream
instance.
const { PerformanceObserver } = require('perf_hooks');
const obs = new PerformanceObserver((items) => {
const entry = items.getEntries()[0];
console.log(entry.entryType); // prints 'http2'
if (entry.name === 'Http2Session') {
// Entry contains statistics about the Http2Session
} else if (entry.name === 'Http2Stream') {
// Entry contains statistics about the Http2Stream
}
});
obs.observe({ entryTypes: ['http2'] });
The entryType
property of the PerformanceEntry
will be equal to 'http2'
.
The name
property of the PerformanceEntry
will be equal to either
'Http2Stream'
or 'Http2Session'
.
If name
is equal to Http2Stream
, the PerformanceEntry
will contain the
following additional properties:
bytesRead
{number} The number ofDATA
frame bytes received for thisHttp2Stream
.bytesWritten
{number} The number ofDATA
frame bytes sent for thisHttp2Stream
.id
{number} The identifier of the associatedHttp2Stream
timeToFirstByte
{number} The number of milliseconds elapsed between thePerformanceEntry
startTime
and the reception of the firstDATA
frame.timeToFirstByteSent
{number} The number of milliseconds elapsed between thePerformanceEntry
startTime
and sending of the firstDATA
frame.timeToFirstHeader
{number} The number of milliseconds elapsed between thePerformanceEntry
startTime
and the reception of the first header.
If name
is equal to Http2Session
, the PerformanceEntry
will contain the
following additional properties:
bytesRead
{number} The number of bytes received for thisHttp2Session
.bytesWritten
{number} The number of bytes sent for thisHttp2Session
.framesReceived
{number} The number of HTTP/2 frames received by theHttp2Session
.framesSent
{number} The number of HTTP/2 frames sent by theHttp2Session
.maxConcurrentStreams
{number} The maximum number of streams concurrently open during the lifetime of theHttp2Session
.pingRTT
{number} The number of milliseconds elapsed since the transmission of aPING
frame and the reception of its acknowledgment. Only present if aPING
frame has been sent on theHttp2Session
.streamAverageDuration
{number} The average duration (in milliseconds) for allHttp2Stream
instances.streamCount
{number} The number ofHttp2Stream
instances processed by theHttp2Session
.type
{string} Either'server'
or'client'
to identify the type ofHttp2Session
.