使用cURL获得请求和响应时间

October 31, 2023

这篇文章介绍了如何使用cURL工具获取请求和响应时间。首先,它解释了cURL输出的各个时间参数的含义,包括DNS解析时间、TCP连接建立时间、上层协议连接时间、请求开始到响应开始的时间、请求开始到第一个字节传输的时间,以及整个请求的总时间。然后,文章详细介绍了如何使用cURL进行请求,包括创建一个格式化的输出文件,以及如何发起请求。最后,它解释了请求命令中的各个参数的作用。

一、文本输出示例

 time_namelookup:  0.001s
      time_connect:  0.037s
   time_appconnect:  0.000s
  time_pretransfer:  0.037s
     time_redirect:  0.000s
time_starttransfer:  0.092s
                   ----------
        time_total:  0.164s
  • time_namelookup:DNS域名解析的时间,就是把https://ianro.cn转换成ip地址的过程
  • time_connect:TCP 连接建立的时间,就是三次握手的时间
  • time_appconnect:SSL/SSH等上层协议建立连接的时间,比如 connect/handshake 的时间
  • time_pretransfer:从请求开始到响应开始传输的时间
  • time_starttransfer:从请求开始到第一个字节将要传输的时间
  • time_total:这次请求花费的全部时间

二、使用

2.1 创建一个文本文件curl-format.txt, 粘贴下面内容

   time_namelookup:  %{time_namelookup}s\n
        time_connect:  %{time_connect}s\n
     time_appconnect:  %{time_appconnect}s\n
    time_pretransfer:  %{time_pretransfer}s\n
       time_redirect:  %{time_redirect}s\n
  time_starttransfer:  %{time_starttransfer}s\n
                     ----------\n
          time_total:  %{time_total}s\n

2.2 发起请求

curl -w "@curl-format.txt" -o /dev/null -s "http://iarno.cn/"
  • -w "@curl-format.txt" 通知cURL使用格式化的输出文件
  • -o /dev/null 将请求的输出重定向到/dev/null
  • -s 通知cURL不显示进度条 "https://iarno.cn/" 是我们请求的URL,请使用引号包围(尤其当你的URL包含&查询字符串)

三、参考

https://cloud.tencent.com/developer/article/1919554

Linux

IARNO

服务端开发

golang数据库连接池参数设置