curl -o /dev/null -H "Cache-Control: no-cache" -s -w time_namelookup:"\t"%{time_namelookup}"\n"time_connect:"\t\t"%{time_connect}"\n"time_pretransfer:"\t"%{time_pretransfer}"\n"time_starttransfer:"\t"%{time_starttransfer}"\n"time_total:"\t\t"%{time_total}"\n"time_redirect:"\t\t"%{time_redirect}"\n" https://www.baidu.com
time_namelookup: 0.004
time_connect: 0.008
time_pretransfer: 0.119
time_starttransfer: 0.143
time_total: 0.143
time_redirect: 0.000
Cache-Control: no-cache? ? ?标头,让服务器不使用缓存来满足请求始终提供最新的数据
time_namelookup:从开始计算,域名解析完成的耗时
time_connect:从开始计算,TCP建立完成(握手)的耗时
time_pretransfer:从开始计算,准备开始传输数据的耗时
time_starttransfer:从开始计算,开始传输数据的耗时(libcurl接收到第一个字节)
time_total:总的耗时
time_redirect:整个过程重定向的耗时,如果整个过程没有重定向,这个时间为0
curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出。输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{variable_name}的格式。
例如,仅获取http状态码,可以这样做:
复制代码
curl -w %{http_code} www.baidu.com -o /dev/null -s |
获取整个请求的时间,可以这样做:
复制代码
curl -w %{time_total} www.baidu.com -o /dev/null -s |
获取域名解析时间,可以这样做:
复制代码
curl -w %{time_namelookup} www.baidu.com -o /dev/null -s |
获取TCP连接耗时,可以这样做:
复制代码
curl -w %{time_connect} www.baidu.com -o /dev/null -s |
此外,还有其他一些变量可以使用,例如url_effective、response_code等。更多变量可以在curl的官方文档中查看。