收集 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 等于 Http2Stream,则 PerformanceEntry 将包含以下附加属性:

¥If name is equal to Http2Stream, the PerformanceEntry will contain the following additional properties:

  • bytesRead <number> 为此 Http2Stream 接收的 DATA 帧字节数。

    ¥bytesRead <number> The number of DATA frame bytes received for this Http2Stream.

  • bytesWritten <number> 为此 Http2Stream 发送的 DATA 帧字节数。

    ¥bytesWritten <number> The number of DATA frame bytes sent for this Http2Stream.

  • id <number> 关联 Http2Stream 的标识符

    ¥id <number> The identifier of the associated Http2Stream

  • timeToFirstByte <number>PerformanceEntry startTime 到接收到第一个 DATA 帧之间经过的毫秒数。

    ¥timeToFirstByte <number> The number of milliseconds elapsed between the PerformanceEntry startTime and the reception of the first DATA frame.

  • timeToFirstByteSent <number>PerformanceEntry startTime 到发送的第一个 DATA 帧之间经过的毫秒数。

    ¥timeToFirstByteSent <number> The number of milliseconds elapsed between the PerformanceEntry startTime and sending of the first DATA frame.

  • timeToFirstHeader <number>PerformanceEntry startTime 到接收到第一个标头之间经过的毫秒数。

    ¥timeToFirstHeader <number> The number of milliseconds elapsed between the PerformanceEntry startTime and the reception of the first header.

如果 name 等于 Http2Session,则 PerformanceEntry 将包含以下附加属性:

¥If name is equal to Http2Session, the PerformanceEntry will contain the following additional properties:

  • bytesRead <number> 为此 Http2Session 接收的字节数。

    ¥bytesRead <number> The number of bytes received for this Http2Session.

  • bytesWritten <number> 为此 Http2Session 发送的字节数。

    ¥bytesWritten <number> The number of bytes sent for this Http2Session.

  • framesReceived <number> Http2Session 接收到的 HTTP/2 帧数。

    ¥framesReceived <number> The number of HTTP/2 frames received by the Http2Session.

  • framesSent <number> Http2Session 发送的 HTTP/2 帧数。

    ¥framesSent <number> The number of HTTP/2 frames sent by the Http2Session.

  • maxConcurrentStreams <number> Http2Session 生命周期内同时打开的最大流数。

    ¥maxConcurrentStreams <number> The maximum number of streams concurrently open during the lifetime of the Http2Session.

  • pingRTT <number> 从发送 PING 帧到接收到它的确认所经过的毫秒数。只有在 Http2Session 上发送了 PING 帧时才会出现。

    ¥pingRTT <number> The number of milliseconds elapsed since the transmission of a PING frame and the reception of its acknowledgment. Only present if a PING frame has been sent on the Http2Session.

  • streamAverageDuration <number> 所有 Http2Stream 实例的平均持续时间(以毫秒为单位)

    ¥streamAverageDuration <number> The average duration (in milliseconds) for all Http2Stream instances.

  • streamCount <number> Http2Session 处理的 Http2Stream 实例的数量。

    ¥streamCount <number> The number of Http2Stream instances processed by the Http2Session.

  • type <string> 'server''client' 来标识 Http2Session 的类型。

    ¥type <string> Either 'server' or 'client' to identify the type of Http2Session.