@abellsmythe/request-time 中文文档教程

发布于 5年前 浏览 17 项目主页 更新于 3年前

request-time

HTTP 请求的计时

了解和测量 HTTP 计时有助于我们发现性能。 客户端到服务器或服务器到服务器通信的瓶颈。

Request Timing

Timings explained

  • DNS Lookup:执行 DNS 查找所花费的时间。 DNS 查找将域名解析为 IP 地址。 每个新域都需要一个完整的往返行程来进行 DNS 查找。

    • > There is no DNS lookup when the destination is already an IP address.
  • TCP 连接:在源主机和目标主机之间建立 TCP 连接所花费的时间。 必须在多步握手过程中正确建立连接。 TCP 连接由操作系统管理,如果无法建立底层 TCP 连接,操作系统范围的 TCP 连接超时将否决我们应用程序的超时配置。

  • TLS 握手:完成一次 TLS 握手所花费的时间。 在握手过程中,端点交换身​​份验证和密钥以建立或恢复安全会话。

    • > There is no TLS handshake with a not HTTPS request.
  • Time to First Byte (TTFB):等待初始响应所花费的时间。 除了等待服务器处理请求和传递响应所花费的时间之外,此时间还捕获到服务器的往返延迟。

  • Content Transfer:接收响应数据所花费的时间。 响应数据的大小和可用的网络带宽决定了它的持续时间。

Installation

NPM:

npm install @abellsmythe/request-time

Usage

'use strict';

const https = require('https');
const timer = require('@abellsmythe/request-time').default;

const request = https.get('https://google.com');
const timings = timer(request);

request.on('response', response => {
    response.on('data', () => {
        // Do something
    });
    response.on('end', () => {
        console.log(timings);
    });
});
{
    start: 20019170.414002,
    socket: 20019171.943738,
    dnsLookup: 20019182.811348,
    connect: 20019194.637675,
    tlsHandshake: 20019245.32874,
    upload: 20019246.615841,
    response: 20019284.613933,
    end: 20019288.80115,
    error: undefined,
    phases:
    {
        wait: 1.5297359973192215,
        dns: 10.86761000007391,
        tcp: 11.826326999813318,
        request: 51.9781660027802,
        firstByte: 37.998091999441385,
        download: 4.187217000871897,
        total: 118.38714800029993
    }
}

API

timer(request)

返回:Object

  • start - Time when the request started.
  • socket - Time when a socket was assigned to the request.
  • dnsLookup - Time when the DNS lookup finished.
  • tlsHandshake - Time when the secure connection is done.
  • connect - Time when the socket successfully connected.
  • upload - Time when the request finished uploading.
  • response - Time when the request fired the response event.
  • end - Time when the response fired the end event.
  • error - Time when the request fired the error event.
  • phases
    • wait - timings.socket - timings.start
    • dns - timings.dnsLookup - timings.socket
    • tcp - timings.connect - timings.dnsLookup
    • request - timings.upload - timings.connect
    • firstByte - timings.response - timings.upload
    • download - timings.end - timings.response
    • total - timings.end - timings.start or timings.error - timings.start

如果尚未测量某些内容,它将是 undefined

注意:时间是一个数字,表示自 UNIX 纪元以来未经过的毫秒数。

注意request-time 使用来自节点的process.hrtime() 因为它不是时钟漂移的主题

request-time

Timings for HTTP requests

Understanding and measuring HTTP timings helps us to discover performance. bottlenecks in client to server or server to server communication.

Request Timing

Timings explained

  • DNS Lookup: Time spent performing the DNS lookup. DNS lookup resolves domain names to IP addresses. Every new domain requires a full round trip to do the DNS lookup.

    • > There is no DNS lookup when the destination is already an IP address.
  • TCP Connection: Time it took to establish TCP connection between a source host and destination host. Connections must be properly established in a multi-step handshake process. TCP connection is managed by an operating system, if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout config of our application.

  • TLS handshake: Time spent completing a TLS handshake. During the handshake process endpoints exchange authentication and keys to establish or resume secure sessions.

    • > There is no TLS handshake with a not HTTPS request.
  • Time to First Byte (TTFB): Time spent waiting for the initial response. This time captures the latency of a round trip to the server in addition to the time spent waiting for the server to process the request and deliver the response.

  • Content Transfer: Time spent receiving the response data. The size of the response data and the available network bandwidth determinates its duration.

Installation

NPM:

npm install @abellsmythe/request-time

Usage

'use strict';

const https = require('https');
const timer = require('@abellsmythe/request-time').default;

const request = https.get('https://google.com');
const timings = timer(request);

request.on('response', response => {
    response.on('data', () => {
        // Do something
    });
    response.on('end', () => {
        console.log(timings);
    });
});
{
    start: 20019170.414002,
    socket: 20019171.943738,
    dnsLookup: 20019182.811348,
    connect: 20019194.637675,
    tlsHandshake: 20019245.32874,
    upload: 20019246.615841,
    response: 20019284.613933,
    end: 20019288.80115,
    error: undefined,
    phases:
    {
        wait: 1.5297359973192215,
        dns: 10.86761000007391,
        tcp: 11.826326999813318,
        request: 51.9781660027802,
        firstByte: 37.998091999441385,
        download: 4.187217000871897,
        total: 118.38714800029993
    }
}

API

timer(request)

Returns: Object

  • start - Time when the request started.
  • socket - Time when a socket was assigned to the request.
  • dnsLookup - Time when the DNS lookup finished.
  • tlsHandshake - Time when the secure connection is done.
  • connect - Time when the socket successfully connected.
  • upload - Time when the request finished uploading.
  • response - Time when the request fired the response event.
  • end - Time when the response fired the end event.
  • error - Time when the request fired the error event.
  • phases
    • wait - timings.socket - timings.start
    • dns - timings.dnsLookup - timings.socket
    • tcp - timings.connect - timings.dnsLookup
    • request - timings.upload - timings.connect
    • firstByte - timings.response - timings.upload
    • download - timings.end - timings.response
    • total - timings.end - timings.start or timings.error - timings.start

If something is not measured yet, it will be undefined.

Note: The time is a number representing the milliseconds without elapsing since the UNIX epoch.

Note: request-time uses process.hrtime() from node as it's not a subject of clock drift

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文