参考这篇文章:https://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl
得出了以下的解决方案:
# 创建文件 curl-format.txt time_namelookup: %{time_namelookup} time_connect: %{time_connect} time_appconnect: %{time_appconnect} time_pretransfer: %{time_pretransfer} time_redirect: %{time_redirect} time_starttransfer: %{time_starttransfer} ---------- time_total: %{time_total}
然后适用如下命令:
curl -w "@curl-format.txt" -o /dev/null -s "http://www.baidu.com/"
会打印出:
time_namelookup: 0.001 time_connect: 0.037 time_appconnect: 0.000 time_pretransfer: 0.037 time_redirect: 0.000 time_starttransfer: 0.092 ---------- time_total: 0.164
对参数的解释如下:
-w "@curl-format.txt" # 告诉cURL使用我们自己的format的文件 -o /dev/null # 输出结果到/dev/null -s # 设定访问的URL