Skip to main content

curl 请求时间测试

How do I measure request and response times at once using cURL?

https://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl

获取总时间

curl -o /dev/null -s -w 'Total: %{time_total}s\n'  https://www.google.com

其中 -o /dev/null -s标识不展示额外的信息和返回

获取详细时间

curl -o /dev/null -s -w 'Establish Connection: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n'  https://www.google.com

批量测试计算时间脚本

咨询chatgpt得到的一个脚本, 平均时间没搞出来, 但可以快速看到每次请求耗费的时间.

# !/bin/bash

num_requests=10 # Change this to change the number of requests made

total_time=0

for i in $(seq 1 $num_requests); do

start=$(date +%s%N)

curl -u username:password -H "Accept: application/json" -H "Content-Type: application/json" -s -o /dev/null -w 'Total: %{time_total}s\n' -X GET http://1.2.3.4:6080/service/plugins/policies/service/name/tdw_hive

end=$(date +%s%N)

timetaken=$(( ($end-$start)/1000000 ))

total_time+=$timetaken

echo -e "\rRequest ${i} took ${timetaken} ms."

done

echo ""

echo "total time" $total_time