判断网站响应时间 脚本
只适用与域名访问:
#!/bin/bash
#requesturl.sh
usage="
Usage: $0 [options...] <url>\n
Options:\n
-h This help text\n
-n <num> The numbers to request\n
"
if [ $# -lt 1 ]
then
echo -e $usage
exit 1
fi
num=10
while getopts "n:h" arg
do
case $arg in
n)
num=$OPTARG
if [ $num -lt 1 ]
then
num=1
fi
;;
h)
echo -e $usage
exit 1
;;
?)
echo "Unknow argument"
exit 1
;;
esac
done
url=$(eval echo "\$$#")
if [ "http://" != ${url:0:7} ]
then
echo "The url need to add the http:// prefix"
exit 1
fi
echo "Request url: "$url
echo "Request number: "$num
i=1
while [ $i -le $num ]
do
c=`curl -o /dev/null -s -w \
"http_code:%{http_code} time_namelookup:%{time_namelookup} \
time_connect:%{time_connect} time_total:%{time_total}" $url`
s=$s$c"\n"
i=$[$i+1]
done
#echo -e $s
echo -e $s | \
awk '{OFS="\n"}{if($1) for(i=1;i<=NF;i++)print $i}' | \
awk -F: -v num=$num -v failnum=0 \
'{if($1 != "http_code")result[$1]+=$2;if($1=="http_code" && $2 != 200)failnum++} \
END{print "Request Failed: " failnum "\n------Average Value------"; \
for(i in result) {print i ": " result[i]/num;} \
print "-------------------------"}'
验证: sh 1.sh -n3 http://www.baidu.com