收集 HTTP/2 性能指标
【Collecting HTTP/2 performance metrics】
性能监视器 API 可用于收集每个 Http2Session 和 Http2Stream 实例的基本性能指标。
【The Performance Observer API can be used to collect basic performance
metrics for each Http2Session and Http2Stream instance.】
import { PerformanceObserver } from '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'] });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'] });PerformanceEntry 的 entryType 属性将等于 'http2'。
【The entryType property of the PerformanceEntry will be equal to 'http2'.】
PerformanceEntry 的 name 属性将等于 'Http2Stream' 或 'Http2Session'。
【The name property of the PerformanceEntry will be equal to either
'Http2Stream' or 'Http2Session'.】
如果 name 等于 Http2Stream,PerformanceEntry 将包含以下附加属性:
【If name is equal to Http2Stream, the PerformanceEntry will contain the
following additional properties:】
bytesRead<number> 接收到的此Http2Stream的DATA帧字节数。bytesWritten<number> 此Http2Stream发送的DATA帧字节数。id<number> 关联Http2Stream的标识符timeToFirstByte<number> 从PerformanceEntry的startTime到接收到第一个DATA帧所经过的毫秒数。timeToFirstByteSent<number> 从PerformanceEntry的startTime到发送第一个DATA帧所经过的毫秒数。timeToFirstHeader<number> 从PerformanceEntry的startTime到接收到第一个响应头经过的毫秒数。
如果 name 等于 Http2Session,PerformanceEntry 将包含以下附加属性:
【If name is equal to Http2Session, the PerformanceEntry will contain the
following additional properties:】
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的类型。