點(diǎn)擊鏈接加入QQ群229390571(免費(fèi)公開課、視頻應(yīng)有盡有):https://jq.qq.com/?_wv=1027&k=5rbudQa
概述
ab(Apache benchmark)是一款常用的壓力測試工具。簡單易用,ab的命令行一次只能支持一次測試。如果想要批量執(zhí)行不同的測試方式,并自動(dòng)對指標(biāo)進(jìn)行分析,那么單靠手工一條一條命令運(yùn)行ab是不可能的。下面介紹下批量模式怎么實(shí)現(xiàn)。
一、腳本說明
該腳本支持ab大多常用參數(shù),如果你需要更多參數(shù),可以通過修改本腳本,加入你想要的即可。
該腳本支持:
1)、批量測試。注意,并不是簡單的批量測試,你可以定測測試輪數(shù),間隔時(shí)間。
2)、階梯并發(fā)增長定制測試,如并發(fā)從100到1000,每輪測5次等。
3)、支持ab的post file模式,你只要在參數(shù)-P | --postfile中帶上你的數(shù)據(jù)文件即可。
4)、壓測完指標(biāo)分析顯示,本shell可以將ab中常用的指示即時(shí)分析出來。

二、腳本內(nèi)容
#!/bin/bash
echo '*===================================================*'
echo '| 本腳本工具基于ab(Apache benchmark),請先安裝好ab, awk |'
echo '| 注意: |'
echo '| shell默認(rèn)最大客戶端數(shù)為1024 |'
echo '| 如超出此限制,請執(zhí)行以下命令: |'
echo '| ulimit -n 655350 |'
echo '*===================================================*'
function usage() {
echo ' 命令格式:'
echo ' ab-test-tools.sh'
echo ' -N|--count 總請求數(shù),缺省 : 5w'
echo ' -C|--clients 并發(fā)數(shù), 缺省 : 100'
echo ' -R|--rounds 測試次數(shù), 缺省 : 10 次'
echo ' -S|-sleeptime 間隔時(shí)間, 缺省 : 10 秒'
echo ' -I|--min 最小并發(fā)數(shù), 缺省: 0'
echo ' -X|--max 最大并發(fā)數(shù),缺省: 0'
echo ' -J|--step 次遞增并發(fā)數(shù)'
echo ' -T|--runtime 總體運(yùn)行時(shí)間,設(shè)置此項(xiàng)時(shí)最大請求數(shù)為5w'
echo ' -P|--postfile post數(shù)據(jù)文件路徑'
echo ' -U|--url 測試地址'
echo ''
echo ' 測試輸出結(jié)果*.out文件'
exit;
}
# 定義默認(rèn)參數(shù)量
# 總請求數(shù)
count=50000
# 并發(fā)數(shù)
clients=100O
# 測試輪數(shù)
rounds=10
# 間隔時(shí)間
sleeptime=10
# 最小并發(fā)數(shù)
min=0
# 最大數(shù)發(fā)數(shù)
max=0
# 并發(fā)遞增數(shù)
step=0
# 測試地址
url=''
# 測試限制時(shí)間
runtime=0
# 傳輸數(shù)據(jù)
postfile=''
ARGS=`getopt -a -o N:C:R:S:I:X:J:U:T:P:h -l count:,client:,round:,sleeptime:,min:,max:,step:,runtime:,postfile:,help -- "$@"`
[ $? -ne 0 ] && usage
eval set -- "${ARGS}"
while true
do
case "$1" in
-N|--count)
count="$2"
shift
;;
-C|--client)
clients="$2"
shift
;;
-R|--round)
rounds="$2"
shift
;;
-S|--sleeptime)
sleeptime="$2"
shift
;;
-I|--min)
min="$2"
shift
;;
-X|--max)
max="$2"
shift
;;
-J|--step)
step="$2"
shift
;;
-U|--url)
url="$2"
shift
;;
-T|--runtime)
runtime="$2"
shift
;;
-P|--postfile)
postfile="$2"
shift
;;
-h|--help)
usage
;;
--)
shift
break
;;
esac
shift
done
# 參數(shù)檢查
if [ x$url = x ]
then
echo '請輸入測試url,非文件/以為結(jié)束'
exit
fi
flag=0
if [ $min != 0 -a $max != 0 ]
then
if [ $max -le $min ]
then
echo '最大并發(fā)數(shù)不能小于最小并發(fā)數(shù)'
exit
fi
if [ $step -le 0 ]
then
echo '并發(fā)遞增步長不能<=0'
exit
fi
if [ $min -lt $max ]
then
flag=1
fi
fi
# 生成ab命令串
cmd="ab -k -r"
# 數(shù)據(jù)文件
if [ x$postf != x ]
then
cmd="$cmd -p $postf"
fi
if [ x$tl != x -a $tl != 0 ]
then
max=50000;
cmd="$cmd -t$tl"
fi
cmd="$cmd -n$count"
echo '-----------------------------';
echo '測試參數(shù)';
echo " 總請求數(shù):$count";
echo " 并發(fā)數(shù):$clients";
echo " 重復(fù)次數(shù):$rounds 次";
echo " 間隔時(shí)間:$sleeptime 秒";
echo " 測試地址:$url";
if [ $min != 0 ];then
echo " 最小并發(fā)數(shù):$min";
fi
if [ $max != 0 ];then
echo " 最大并發(fā)數(shù):$max";
fi
if [ $step != 0 ];then
echo " 每輪并發(fā)遞增:$step"
fi
# 指定輸出文件名
datestr=`date +%Y%m%d%H%I%S`
outfile="$datestr.out";
# runtest $cmd $outfile $rounds $sleeptime
function runtest() {
# 輸出命令
echo "";
echo ' 當(dāng)前執(zhí)行命令:'
echo " $cmd"
echo '------------------------------'
# 開始執(zhí)行測試
cnt=1
while [ $cnt -le $rounds ];
do
echo "第 $cnt 輪 開始"
$cmd >> $outfile
echo "
" >> $outfile
echo "第 $cnt 輪 結(jié)束"
echo '----------------------------'
cnt=$(($cnt+1))
if [ $cnt -le $rounds ]; then
echo "等待 $sleeptime 秒"
sleep $sleeptime
fi
done
}
temp=$cmd;
if [ $flag != 0 ]; then
cur=$min
over=0
while [ $cur -le $max ]
do
cmd="$temp -c$cur $url"
runtest $cmd $outfile $rounds $sleeptime
cur=$(($cur+$step))
if [ $cur -ge $max -a $over != 1 ]; then
cur=$max
over=1
fi
done
else
cmd="$cmd -c$clients $url"
runtest $cmd $outfile $rounds $sleeptime
fi
# 分析結(jié)果
if [ -f $outfile ]; then
echo '本次測試結(jié)果如下:'
echo '+------+----------+----------+---------------+---------------+---------------+--------------------+--------------------+'
echo '| 序號 | 總請求數(shù) | 并發(fā)數(shù) | 失敗請求數(shù) | 每秒事務(wù)數(shù) | 平均事務(wù)(ms) | 并發(fā)平均事務(wù)數(shù)(ms) | 總體傳輸字節(jié)數(shù) |'
echo '+------+----------+----------+---------------+---------------+---------------+--------------------+--------------------+'
comp=(`awk '/Complete requests/{print $NF}' $outfile`)
concur=(`awk '/Concurrency Level:/{print $NF}' $outfile`)
fail=(`awk '/Failed requests/{print $NF}' $outfile`)
qps=(`awk '/Requests per second/{print $4F}' $outfile`)
tpr=(`awk '/^Time per request:(.*)(mean)$/{print $4F}' $outfile`)
tpr_c=(`awk '/Time per request(.*)(mean, across all concurrent requests)/{print $4F}' $outfile`)
trate=(`awk '/Transfer rate/{print $3F}' $outfile`)
for ((i=0; i<${#comp[@]}; i++))
do
echo -n "|"
printf '%6s' $(($i+1))
printf "|"
printf '%10s' ${comp[i]}
printf '|'
printf '%10s' ${concur[i]}
printf '|'
printf '%15s' ${fail[i]}
printf '|'
printf '%15s' ${qps[i]}
printf '|'
printf '%15s' ${tpr[i]}
printf '|'
printf '%20s' ${tpr_c[i]}
printf '|'
printf '%20s' ${trate[i]}
printf '|'
echo '';
echo '+-----+----------+----------+---------------+---------------+---------------+--------------------+--------------------+'
done
echo ''
fi
三、測試示例
sh ab-test-tool.sh -N 100000 -C 100 -R 2 -I 100 -X 500 -J 80 -S 5 -U 'http://...'

四、ab信息說明
Server Software:? ? Apache/2.2.19? ##apache版本
Server Hostname:? ? vm1.xxx.com? ##請求的機(jī)子
Server Port:? ? ? 80 ##請求端口
Document Path:? ? ?/xxx.html
Document Length:? ? 25 bytes ##頁面長度
Concurrency Level:? ?100 ##并發(fā)數(shù)
Time taken for tests:? 0.273 seconds ##共使用了多少時(shí)間
Complete requests:? ?1000? ##請求數(shù)
Failed requests:? ? 0? ##失敗請求
Write errors:? ? ? 0
Total transferred:? ?275000 bytes ##總共傳輸字節(jié)數(shù),包含http的頭信息等
HTML transferred:? ? 25000 bytes ##html字節(jié)數(shù),實(shí)際的頁面?zhèn)鬟f字節(jié)數(shù)
Requests per second:? 3661.60 [#/sec] (mean) ##每秒多少請求,這個(gè)是非常重要的參數(shù)數(shù)值,服務(wù)器的吞吐量
Time per request:? ? 27.310 [ms] (mean) ##用戶平均請求等待時(shí)間
Time per request:? ? 0.273 [ms] (mean, across all concurrent requests) ##服務(wù)器平均處理時(shí)間,也就是服務(wù)器吞吐量的倒數(shù)
Transfer rate:? ? ?983.34 [Kbytes/sec] received ##每秒獲取的數(shù)據(jù)長度
Connection Times (ms)
min mean[+/-sd] median? max
Connect:? ? 0? 1? 2.3? ?0? ?16
Processing:? ?6? 25? 3.2? ?25? ?32
Waiting:? ? 5? 24? 3.2? ?25? ?32
Total:? ? ?6? 25? 4.0? ?25? ?48
Percentage of the requests served within a certain time (ms)
50%? ?25 ## 50%的請求在25ms內(nèi)返回
66%? ?26 ## 60%的請求在26ms內(nèi)返回
75%? ?26
80%? ?26
90%? ?27
95%? ?31
98%? ?38
99%? ?43
100%? ?48 (longest request)