【问题标题】:When does nginx $upstream_response_time start/stop specificallynginx $upstream_response_time 什么时候具体开始/停止
【发布时间】:2021-11-05 00:58:54
【问题描述】:

有谁知道$upstream_response_time 的时钟具体是什么时候开始和结束的?

文档似乎有点模糊:

保持从上游服务器接收响应所花费的时间;时间以毫秒为单位保存。多个响应的时间由逗号和冒号分隔,例如 $upstream_addr 变量中的地址。

还有一个$upstream_header_time 值,这增加了更多的混乱。

  1. 我假设$upstream_connect_time 在建立连接后停止,但它被上游接受之前?

  2. 在这之后$upstream_response_time 包括什么?

    • 等待上游接受所花费的时间?
    • 发送请求所花费的时间?
    • 发送响应头所花费的时间?

【问题讨论】:

    标签: performance nginx query-performance


    【解决方案1】:

    更具体的定义在他们的blog

    $request_time – 完整的请求时间,从 NGINX 读取第一个开始 来自客户端的字节并在 NGINX 发送最后一个字节时结束 响应正文

    $upstream_connect_time – 建立一个 与上游服务器的连接

    $upstream_header_time – 时间 在建立与上游服务器的连接和接收 响应头的第一个字节

    $upstream_response_time – 时间 在建立与上游服务器的连接和接收 响应正文的最后一个字节

    所以

    • $upstream_header_time 包含在 $upstream_response_time 中。
    • 两者都不包括连接到上游所花费的时间。
    • 两者都不包括向客户端发送响应所花费的时间。

    【讨论】:

      【解决方案2】:

      我已经调查和调试了这个行为,结果如下:

      start time end time
      $upstream_connect_time before Nginx establishes TCP connection with upstream server before Nginx sends HTTP request to upstream server
      $upstream_header_time before Nginx establishes TCP connection with upstream server after Nginx receives and processes headers in HTTP response from upstream server
      $upstream_response_time before Nginx establishes TCP connection with upstream server after Nginx receives and processes HTTP response from upstream server

      源代码

      我将解释 $upstream_connect_time 和 $upstream_response_time 之间的值有何不同,因为这是我主要感兴趣的。

      u->state->connect_time 的值(以毫秒为单位表示 $upstream_connect_time)被提取到以下部分:https://github.com/nginx/nginx/blob/3334585539168947650a37d74dd32973ab451d70/src/http/ngx_http_upstream.c#L2073

          if (u->state->connect_time == (ngx_msec_t) -1) {
              u->state->connect_time = ngx_current_msec - u->start_time;
          }
      

      u->state->repponse_time 的值(以毫秒为单位表示$upstream_response_time)在以下部分中设置:https://github.com/nginx/nginx/blob/3334585539168947650a37d74dd32973ab451d70/src/http/ngx_http_upstream.c#L4432

          if (u->state && u->state->response_time == (ngx_msec_t) -1) {
              u->state->response_time = ngx_current_msec - u->start_time;
      

      您可以注意到,这两个值都是基于u->start_time 计算的,这是建立连接之前的时间,在https://github.com/nginx/nginx/blob/3334585539168947650a37d74dd32973ab451d70/src/http/ngx_http_upstream.c#L1533 中定义 (注意ngx_event_connect_peer是一个在nginx工作进程和上游服务器之间建立TCP连接的函数)。

      因此,这两个值都包括建立 TCP 连接所用的时间。您可以通过使用例如 gdbserver 进行实时调试来检查这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-18
        • 2018-02-17
        • 2016-12-01
        • 2010-09-20
        • 2019-08-29
        • 2018-12-02
        • 2011-05-16
        • 1970-01-01
        相关资源
        最近更新 更多