千家信息网

apache中ab压力测试工具怎么用

发表于:2024-10-19 作者:千家信息网编辑
千家信息网最后更新 2024年10月19日,这篇文章主要介绍apache中ab压力测试工具怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、脚本说明该脚本支持ab大多常用参数,如果你需要更多参数,可以通过修改本脚本
千家信息网最后更新 2024年10月19日apache中ab压力测试工具怎么用

这篇文章主要介绍apache中ab压力测试工具怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、脚本说明

该脚本支持ab大多常用参数,如果你需要更多参数,可以通过修改本脚本,加入你想要的即可。

该脚本支持:

1)、批量测试。注意,并不是简单的批量测试,你可以定测测试轮数,间隔时间。2)、阶梯并发增长定制测试,如并发从100到1000,每轮测5次等。3)、支持ab的post file模式,你只要在参数-P | --postfile中带上你的数据文件即可。4)、压测完指标分析显示,本shell可以将ab中常用的指示即时分析出来。


二、脚本内容

#!/bin/bashecho '*===================================================*'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 ] && usageeval 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 ;; esacshiftdone# 参数检查if [ x$url = x ]then echo '请输入测试url,非文件/以为结束' exitfiflag=0if [ $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 fifi# 生成ab命令串cmd="ab -k -r"# 数据文件if [ x$postf != x ]then cmd="$cmd -p $postf"fiif [ x$tl != x -a $tl != 0 ]then  max=50000; cmd="$cmd -t$tl"ficmd="$cmd -n$count"echo '-----------------------------';echo '测试参数';echo " 总请求数:$count";echo " 并发数:$clients";echo " 重复次数:$rounds 次";echo " 间隔时间:$sleeptime 秒";echo " 测试地址:$url";if [ $min != 0 ];thenecho " 最小并发数:$min";fiif [ $max != 0 ];thenecho " 最大并发数:$max";fiif [ $step != 0 ];thenecho " 每轮并发递增:$step" fi# 指定输出文件名datestr=`date +%Y%m%d%H%I%S`outfile="$datestr.out";# runtest $cmd $outfile $rounds $sleeptimefunction 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 doneelse  cmd="$cmd -c$clients $url" runtest $cmd $outfile $rounds $sleeptime fi# 分析结果if [ -f $outfile ]; thenecho '本次测试结果如下:'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 's' ${comp[i]} printf '|'  printf 's' ${concur[i]} printf '|' printf 's' ${fail[i]} printf '|' printf 's' ${qps[i]} printf '|' printf 's' ${tpr[i]} printf '|' printf '%20s' ${tpr_c[i]} printf '|' printf '%20s' ${trate[i]} printf '|' echo ''; echo '+-----+----------+----------+---------------+---------------+---------------+--------------------+--------------------+'doneecho ''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 ##请求的机子
Server Port: 80 ##请求端口

Document Path: /xxx.html
Document Length: 25 bytes ##页面长度

Concurrency Level: 100 ##并发数
Time taken for tests: 0.273 seconds ##共使用了多少时间
Complete requests: 1000 ##请求数
Failed requests: 0 ##失败请求
Write errors: 0
Total transferred: 275000 bytes ##总共传输字节数,包含http的头信息等
HTML transferred: 25000 bytes ##html字节数,实际的页面传递字节数
Requests per second: 3661.60 [#/sec] (mean) ##每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request: 27.310 [ms] (mean) ##用户平均请求等待时间
Time per request: 0.273 [ms] (mean, across all concurrent requests) ##服务器平均处理时间,也就是服务器吞吐量的倒数
Transfer rate: 983.34 [Kbytes/sec] received ##每秒获取的数据长度

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内返回
66% 26 ## 60%的请求在26ms内返回
75% 26
80% 26
90% 27
95% 31
98% 38
99% 43
100% 48 (longest request)

以上是"apache中ab压力测试工具怎么用"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

0