Apache Benchmark 简称ab,是apache里一个压力测试工具。安装ab一种直接安装apache server即可里面自带ab服务;另外一种不安装apache server 直接安装ab;
这里是通过安装apache server 环境是centos7
1:先安装基础依赖环境:
1.1:依赖基础环境
yum install -y gcc gcc-c++
yum install -y openssl-devel expat-devel
1.2:安装 pcre tar下载地址ftp://ftp.pcre.org/pub/pcre/
##安装基础依赖包
# yum -y install openssl-devel
##安装pcre 这里使用版本是8.43
tar -vxf pcre-8.43.tar.gz
cd pcre-8.43
##配置
./configure --prefix=/usr/local/pcre-8.43
##编译安装
make && make install
1.3:安装apr tar包下载地址http://archive.apache.org/dist/apr/
##先安装APR 这里使用apr版本为1.7.0
tar -vxf apr-1.7.0.tar.gz
cd apr-1.7.0
##配置
./configure --prefix=/usr/local/apr-1.7.0
##编译安装
make && make install
##安装
##这里使用apr-utils版本为1.6.1
tar -vxf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
##配置
./configure --prefix=/usr/local/apr-util-1.6.1 --with-apr=/usr/local/apr-1.7.0/
##编译安装
make && make install
1.4:安装 apache server
直接在官网中下载最新版的tar包路径为:http://httpd.apache.org/download.cgi#apache24
##apache 版本为httpd-2.4.41
tar -vxf httpd-2.4.41.tar.gz
cd httpd-2.4.41
###配置 参数--prefix指定安装路径,--enable-mods-shared启用所有支持的动态加载模块,--with-apr指定APR路径,--with-apr-util指定APR-util路径 --with-pcre指定pcre路径
./configure --prefix=/usr/local/apache --enable-mods-shared=all --with-apr=/usr/local/apr-1.7.0/ --with-apr-util=/usr/local/apr-util-1.6.1/ --with-pcre=/usr/local/pcre-8.43
##安装
make && make install
2:使用apache ab模块
2.1:ab 基础设置参数说明:
-n requests 要执行的请求总数
-c concurrency 并发数
-t timelimit 所有请求数共花最大时间
-s timeout 最大超时时间 默认30s
-T Content Type 设置Content Type 例如 -T‘application/json’
-p postfile POST请求时请求参数可以放在文件中由postfile指定路径. Remember also to set -T
ab命令支持所有参数
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)
-I Disable TLS Server Name Indication (SNI) extension
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(TLS1, TLS1.1, TLS1.2 or ALL)
-E certfile Specify optional client certificate chain and private key
2.2:ab使用DEMO
使用Demo: ab [options] [http[s]://]hostname[:port]/path
例如:
- ab -n500 -c10 "http://localhost:18082/test" 命令:通过10个并发共500个请求访问接口 GET请求
- ab -c10 -t5 "http://localhost:18082/test" 命令:10个并发5s内请求访问接口 GET请求
- ab -n500 -c10 -p paras.txt -T ‘application/json’ "http://localhost:18082/test1" POST请求时,将json参数放入paras.txt(文件)文件中,用-p去解析
2.3:ab结果分析:
执行ab -n500 -c10 'http://localhost:18082/test' 后得到结果
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests
Server Software: ##服务器系统
Server Hostname: localhost ##服务器域名
Server Port: 18082 ##服务器端口
Document Path: /test ##请求路径
Document Length: 27 bytes ##访问内容(页面,接口)大小
Concurrency Level: 10 ##请求登记 -也就是请求并发数
Time taken for tests: 0.467 seconds ##测试数据总响应时间
Complete requests: 500 ##测试总请求数
Failed requests: 0 ##失败请求数 这里的失败是指请求的连接服务器、发送数据、接收数据等环节发生异常,以及无响应后超时的情况。对于超时时间的设置可以用ab的-t参数。如果接受到的http响应数据的头信息中含有2xx以外的状态码,则会在测试结果显示另一个名
Total transferred: 66000 bytes ##总共传输字节数,包含http的头信息等。使用ab的-v参数即可查看详细的http头信息。
HTML transferred: 13500 bytes ##html字节数,实际的页面传递字节数。也就是减去了Total transferred中http响应数据中头信息的长度。
Requests per second: 1070.57 [#/sec] (mean) ##服务器吞吐量 每秒处理请求数 等于请求总数/测试请求总响应时间
Time per request: 9.341 [ms] (mean)## 平均数,用户平均请求等待时间
Time per request: 0.934 [ms] (mean, across all concurrent requests) ##服务器平均处理时间
Transfer rate: 138.00 [Kbytes/sec] received ## 平均传输速率 可以说明服务器在处理能力达到限制时,可以设置带宽的需求量
Connection Times (ms) ## 压力测试时的连接处理时间。(毫秒)
min mean[+/-sd] median max
Connect: 0 1 0.9 0 7
Processing: 2 8 12.9 5 128
Waiting: 1 7 11.2 4 123
Total: 2 9 12.9 6 129
WARNING: The median and mean for the initial connection time are not within a normal deviation
These results are probably not that reliable.
##下面的内容为整个场景中所有请求的响应情况
Percentage of the requests served within a certain time (ms)
50% 6 ##50%请求时间小于6ms
66% 7 ##66%请求时间小于7ms
75% 9 ##75%请求时间小于9ms
80% 10 ##80%请求时间小于10ms
90% 15 ##90%请求时间小于15ms
95% 20 ##95%请求时间小于20ms
98% 34 ##98%请求时间小于34ms
99% 92 ##99%请求时间小于92ms
100% 129 (longest request) ##100%请求时间小于129ms
3:通过apache 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
编写测试demo:sh ab-test-tool.sh -N 1000 -C 10 -R 2 -I 10 -X 50 -J 8 -S 5 -U 'http://host.docker.internal:18082/test'
测试结果:


224



被折叠的 条评论
为什么被折叠?



