概述
ab(Apache benchmark)是一款常用的壓力測試工具。簡單易用,ab的指令行一次隻能支援一次測試。如果想要批量執行不同的測試方式,并自動對名額進行分析,那麼單靠手工一條一條指令運作ab是不可能的。下面介紹下批量模式怎麼實作。
一、腳本說明
該腳本支援ab大多常用參數,如果你需要更多參數,可以通過修改本腳本,加入你想要的即可。
該腳本支援:
1)、批量測試。注意,并不是簡單的批量測試,你可以定測測試輪數,間隔時間。
2)、階梯并發增長定制測試,如并發從100到1000,每輪測5次等。
3)、支援ab的post file模式,你隻要在參數-P | --postfile中帶上你的資料檔案即可。
4)、壓測完名額分析顯示,本shell可以将ab中常用的訓示即時分析出來。
二、腳本内容
#!/bin/bash
echo '*===================================================*'
echo '| 本腳本工具基于ab(Apache benchmark),請先安裝好ab, awk |'
echo '| 注意: |'
echo '| shell預設最大用戶端數為1024 |'
echo '| 如超出此限制,請執行以下指令: |'
echo '| ulimit -n 655350 |'
echo '*===================================================*'
function usage() {
echo ' 指令格式:'
echo ' ab-test-tools.sh'
echo ' -N|--count 總請求數,預設 : 5w'
echo ' -C|--clients 并發數, 預設 : 100'
echo ' -R|--rounds 測試次數, 預設 : 10 次'
echo ' -S|-sleeptime 間隔時間, 預設 : 10 秒'
echo ' -I|--min 最小并發數, 預設: 0'
echo ' -X|--max 最大并發數,預設: 0'
echo ' -J|--step 次遞增并發數'
echo ' -T|--runtime 總體運作時間,設定此項時最大請求數為5w'
echo ' -P|--postfile post資料檔案路徑'
echo ' -U|--url 測試位址'
echo ''
echo ' 測試輸出結果*.out檔案'
exit;
}
# 定義預設參數量
# 總請求數
count=50000
# 并發數
clients=100O
# 測試輪數
rounds=10
# 間隔時間
sleeptime=10
# 最小并發數
min=0
# 最大數發數
max=0
# 并發遞增數
step=0
# 測試位址
url=''
# 測試限制時間
runtime=0
# 傳輸資料
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
# 參數檢查
if [ x$url = x ]
then
echo '請輸入測試url,非檔案/以為結束'
exit
fi
flag=0
if [ $min != 0 -a $max != 0 ]
then
if [ $max -le $min ]
then
echo '最大并發數不能小于最小并發數'
exit
fi
if [ $step -le 0 ]
then
echo '并發遞增步長不能<=0'
exit
fi
if [ $min -lt $max ]
then
flag=1
fi
fi
# 生成ab指令串
cmd="ab -k -r"
# 資料檔案
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 '測試參數';
echo " 總請求數:$count";
echo " 并發數:$clients";
echo " 重複次數:$rounds 次";
echo " 間隔時間:$sleeptime 秒";
echo " 測試位址:$url";
if [ $min != 0 ];then
echo " 最小并發數:$min";
fi
if [ $max != 0 ];then
echo " 最大并發數:$max";
fi
if [ $step != 0 ];then
echo " 每輪并發遞增:$step"
fi
# 指定輸出檔案名
datestr=`date +%Y%m%d%H%I%S`
outfile="$datestr.out";
# runtest $cmd $outfile $rounds $sleeptime
function runtest() {
# 輸出指令
echo "";
echo ' 目前執行指令:'
echo " $cmd"
echo '------------------------------'
# 開始執行測試
cnt=1
while [ $cnt -le $rounds ];
do
echo "第 $cnt 輪 開始"
$cmd >> $outfile
echo "
" >> $outfile
echo "第 $cnt 輪 結束"
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
# 分析結果
if [ -f $outfile ]; then
echo '本次測試結果如下:'
echo '+------+----------+----------+---------------+---------------+---------------+--------------------+--------------------+'
echo '| 序号 | 總請求數 | 并發數 | 失敗請求數 | 每秒事務數 | 平均事務(ms) | 并發平均事務數(ms) | 總體傳輸位元組數 |'
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資訊說明