curl 实战

  1. 耗时分析

耗时分析

$ man curl  # 查看 curl 的使用方式

-w, --write-out <format> # -w 参数配置输出格式

-w 参数中有部分时间相关的参数如下:

  • time_namelookup:从请求开始到 DNS 域名解析完成的耗时
  • time_connect:从请求开始到 TCP 连接建立耗时(三次握手)
  • time_appconnect:从请求开始到 SSL/SSH 等建立连接耗时( ssl handshake 等)
  • time_pretransfer:从请求开始到响应开始传输的时间
  • time_redirect:从请求开始到,包含前面四者耗时,且所有重定向的时间相加,直到最终访问目标服务前的耗时
  • time_starttransfer:从请求开始到第一个字节将要传输的耗时,包含了 time_pretransfer 耗时
  • time_total:请求的全部耗时

如何发起耗时分析请求

  1. 直接在命令行中拼写输出格式
    curl -w '\ntime_namelookup=%{time_namelookup}\ntime_connect=%{time_connect}\ntime_appconnect=%{time_appconnect}\ntime_redirect=%{time_redirect}\ntime_pretransfer=%{time_pretransfer}\ntime_starttransfer=%{time_starttransfer}\ntime_total=%{time_total}\n\n' -o /dev/null -s -L 'http://voidchen.com'
  1. 利用文件描述格式
    创建一个格式文件 format.txt
     time_namelookup:  %{time_namelookup}\n
        time_connect:  %{time_connect}\n
     time_appconnect:  %{time_appconnect}\n
       time_redirect:  %{time_redirect}\n
    time_pretransfer:  %{time_pretransfer}\n
    time_starttransfer:  %{time_starttransfer}\n
                     ----------\n
          time_total:  %{time_total}\n
    使用格式文件发起访问
    curl -w '@format.txt' -o /dev/null -s -L 'http://voidchen.com'

耗时计算

  1. DNS耗时 = time_namelookup
  2. TCP建连耗时 = time_connect - time_namelookup
  3. SSL握手耗时 = time_appconnect - time_connect
  4. 服务器处理请求耗时 = time_starttransfer - time_pretransfer
  5. TTFB耗时 = time_starttransfer - time_appconnect
  6. 服务器传输耗时 = time_total - time_starttransfer
  7. 总耗时 = time_total

TODO 分析脚本

参考


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nickchenyx@gmail.com

Title:curl 实战

Count:402

Author:nickChen

Created At:2022-10-14, 22:19:41

Updated At:2023-05-08, 23:27:10

Url:http://nickchenyx.github.io/2022/10/14/curl_usage/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.