Curl 请求输出网络耗时

本文介绍如何通过Curl命令在Linux环境中详细记录HTTP请求的网络耗时,包括生成打印字段配置文件、解析字段含义,以及提供命令行直接运行的示例,帮助诊断和优化网络性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的

curl 请求页面时打印请求过程中的耗时,方便识别网络过程中存在问题。

生成打印字段配置文件:

cat >./curl-format.txt  <<-EOF
   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
EOF

字段含义

cat ./curl-format.txt 

time_namelookup: DNS解析时间
time_connect:TCP 连接建立的时间,就是三次握手的时间 ,计算方式:time_connect - time_namelookup 
time_appconnect:SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间
time_redirect: 从开始到最后一个请求事务的时间
time_pretransfer:从请求开始到响应开始传输的时间
time_starttransfer: 从请求开始到第一个字节将要传输的时间
time_total:这次请求花费的全部时间

示例:

curl -w "@curl-format.txt" -o /dev/null -s -L "https://www.ptmind.com"

   time_namelookup:  0.529108
      time_connect:  0.688685
   time_appconnect:  1.201098
     time_redirect:  0.000000
  time_pretransfer:  1.201304
time_starttransfer:  1.418342
                    ----------
time_total:  1.540865

#循环多次执行
for i in $(seq 1000);do curl -w "@curl-format.txt" -o /dev/null -s -L "https://www.ptmind.com";echo ------$i---------;sleep 1;done

计算公式

DNS查询时间:0529108
TCP连接时间: time_connect - time_namelookup = 0.688685 - 0.529108 = 159ms
服务器处理时间:time_starttransfer - time_pretransfer = 1.418342 - 1.201304 = 217ms
内容传输时间:time_total - time_starttransfer = 1.540865 - 1.418342 = 122ms

命令行直接运行

curl -w "%{time_namelookup}\n%{time_connect}\n%{time_pretransfer}\n%{time_starttransfer}\n%{time_total}" -o /dev/null -s -L "https://www.ptmind.com"  

输出信息如下:
0.028
0.033
0.057
0.450
0.677

#循环执行
for i in $(seq 1000);do curl -w "%{time_namelookup}\n%{time_connect}\n%{time_pretransfer}\n%{time_starttransfer}\n%{time_total}" -o /dev/null -s -L "https://www.ptmind.com";echo ------$i---------;sleep 1;done

原文链接

https://www.itsiv.com/archives/682

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值