采集 HTTP/2 性能指标
性能观察 API 可用于收集每个 Http2Session 和 Http2Stream 实例的基本性能指标。
const { PerformanceObserver } = require('node:perf_hooks');
const obs = new PerformanceObserver((items) => {
const entry = items.getEntries()[0];
console.log(entry.entryType); // 打印 'http2'
if (entry.name === 'Http2Session') {
// 条目包含有关 Http2Session 的统计信息
} else if (entry.name === 'Http2Stream') {
// 条目包含有关 Http2Stream 的统计信息
}
});
obs.observe({ entryTypes: ['http2'] });PerformanceEntry 的 entryType 属性将等于 'http2'。
PerformanceEntry 的 name 属性将等于 'Http2Stream' 或 'Http2Session'。
如果 name 等于 Http2Stream,则 PerformanceEntry 将包含以下附加属性:
bytesRead<number> 为此Http2Stream接收的DATA帧字节数。bytesWritten<number> 为此Http2Stream发送的DATA帧字节数。id<number> 关联Http2Stream的标识符timeToFirstByte<number> 从PerformanceEntrystartTime到接收到第一个DATA帧之间经过的毫秒数。timeToFirstByteSent<number> 从PerformanceEntrystartTime到发送的第一个DATA帧之间经过的毫秒数。timeToFirstHeader<number> 从PerformanceEntrystartTime到接收到第一个标头之间经过的毫秒数。
如果 name 等于 Http2Session,则 PerformanceEntry 将包含以下附加属性:
bytesRead<number> 为此Http2Session接收的字节数。bytesWritten<number> 为此Http2Session发送的字节数。framesReceived<number>Http2Session接收到的 HTTP/2 帧数。framesSent<number>Http2Session发送的 HTTP/2 帧数。maxConcurrentStreams<number>Http2Session生命周期内同时打开的最大流数。pingRTT<number> 从发送PING帧到接收到它的确认所经过的毫秒数。 只有在Http2Session上发送了PING帧时才会出现。streamAverageDuration<number> 所有Http2Stream实例的平均持续时间(以毫秒为单位)streamCount<number>Http2Session处理的Http2Stream实例的数量。type<string>'server'或'client'来标识Http2Session的类型。
The Performance Observer API can be used to collect basic performance
metrics for each Http2Session and Http2Stream instance.
const { PerformanceObserver } = require('node: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 ofDATAframe bytes received for thisHttp2Stream.bytesWritten<number> The number ofDATAframe bytes sent for thisHttp2Stream.id<number> The identifier of the associatedHttp2StreamtimeToFirstByte<number> The number of milliseconds elapsed between thePerformanceEntrystartTimeand the reception of the firstDATAframe.timeToFirstByteSent<number> The number of milliseconds elapsed between thePerformanceEntrystartTimeand sending of the firstDATAframe.timeToFirstHeader<number> The number of milliseconds elapsed between thePerformanceEntrystartTimeand 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 aPINGframe and the reception of its acknowledgment. Only present if aPINGframe has been sent on theHttp2Session.streamAverageDuration<number> The average duration (in milliseconds) for allHttp2Streaminstances.streamCount<number> The number ofHttp2Streaminstances processed by theHttp2Session.type<string> Either'server'or'client'to identify the type ofHttp2Session.