收集 HTTP/2 性能指标


🌐 Collecting HTTP/2 performance metrics

性能监视器 API 可用于收集每个 Http2SessionHttp2Stream 实例的基本性能指标。

🌐 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'] });

PerformanceEntryentryType 属性将等于 'http2'

🌐 The entryType property of the PerformanceEntry will be equal to 'http2'.

PerformanceEntryname 属性将等于 'Http2Stream''Http2Session'

🌐 The name property of the PerformanceEntry will be equal to either 'Http2Stream' or 'Http2Session'.

如果 name 等于 Http2StreamPerformanceEntry 将包含以下附加属性:

🌐 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 等于 Http2SessionPerformanceEntry 将包含以下附加属性:

🌐 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 的类型。