Linux服务器下Apache 压力测试工具AB
ab是Apache自带的压力测试工具,全称是ApacheBench,ab是Apache的一个安装组件,所以需要安装Apache后才可以使用,该命令位于Apache安装目录下的bin文件夹中,ab是专门用于HTTP Server的benchmark testing,可以同时模拟多个并发请求,ab的设计意图是描绘当前所安装的Apache的执行性能,主要是显示所安装的Apache每秒可以处理多少个请求,ab同微软的WAST、HP的LoadRunner、QALoad等比起来,它要方便易用得多,虽然ab不像LR那么强大,但它足够轻便,做一些场景比较简单的测试,ab是个不错的选择,嘿嘿!(说明:本篇博文是紧接着上一篇博文写,上一篇我们说了一LAMP的最新架构,同时安装最新的 Discuz 3.0,接下来我们来做一下压力测试)
一、安装Apache服务器
httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。
(1). 编译安装apr
[root@web src]# tar xf apr-1.4.6.tar.bz2
[root@web src]# cd apr-1.4.6
[root@web src]# ./configure –help | less #(可以查看帮助文件)
[root@web src]# ./configure –prefix=/usr/local/apr #(指定安装路径)
[root@web src]# make
[root@web src]# make install
(2). 编译安装apr-util
[root@web src]# tar xf apr-util-1.5.2.tar.bz2
[root@web src]# cd apr-util-1.5.2
[root@web src]# ./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr
#(–prefix指定安装路径;–with-apr指定apr的安装路径,apr-util依赖于apr)
[root@web src]# make && make install
附:apache官方对APR的介绍:
The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to whichsoftware developers may code and be assured of predictable ifnot identical behaviour regardless of the platform on whichtheir software is built, relieving them of the need to code special-caseconditions to work around or take advantage of platform-specific deficiencies or features.
(3). httpd-2.4.4编译过程也要依赖于pcre-devel软件包,需要事先安装。
[root@web ~]# yum install pcre-devel -y
3. 编译安装httpd-2.4.4
[root@web src]# tar xf httpd-2.4.4.tar.gz
[root@web src]# cd httpd-2.4.4
[root@web httpd-2.4.4]# ./configure –prefix=/usr/local/apache –sysconfdir=/etc/httpd –enable-so –enable-ssl –enable-cgi –enable-cgid –enable-modules=most –enable-mods-shared=most –enable-rewrite –with-zlib –with-pcre –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –enable-mpms-shared=all –with-mpm=event
–sysconfdir=/etc/httpd#指定配置文件安装位置
—enable-so #支持动态共享模块如果没有这个模块PHP将无法与apache结合工作
—enable-ssl #启用支持ssl
—enable-cgi #支持cgi
—enable-rewrite #支持URL重写
–with-zlib #压缩库,在互联网上传播时可节约带宽
–with-apr=/usr/local/apr#指定apr路径
–with-apr-util=/usr/local/apr-util#指定apr-util路径
—enable-mpms-shared=all #支持多道处理模块
–with-mpm=event #设定默认的模块
错误:
checking whether to enablemod_ssl… configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
解决方法:
[root@web httpd-2.4.4]# yum install -y openssl-devel
4. 修改httpd的主配置文件,设置其Pid文件的路径
说明:手动编译安装后,httpd.pid文件是存放在/usr/local/apache/logs/目录下的,这个位置未免有些不方便。
#更改配置文件
[root@web httpd-2.4.4]# cd
[root@web ~]# cd /etc/httpd/
[root@web httpd]# ls
conf conf.d extra httpd.conf logs magic mime.types modules original run
[root@web httpd]# cp httpd.conf httpd.conf.bak
[root@web httpd]# vim httpd.conf
#在配置文件中找一个位置定义一下Pid文件路径就可以了
Pidfile “/var/run/httpd.pid”
5. 提供SysV服务脚本/etc/rc.d/init.d/httpd
[root@web httpd]# vim /etc/init.d/httpd
#!/bin/bash
#
# chkconfig: – 85 15
# description: Apache is a World Wide Web server. It is used to serve /
. /etc/rc.d/init.d/functions—–读取函数
if[ -f /etc/sysconfig/httpd]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-“C”}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=””
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based “worker” MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl#指定apachectl程序位置
httpd=${HTTPD-/usr/local/apache/bin/httpd} #httpd程序位置
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid} #如果文件存在就使用存在文件路径,如果不存在就使用/var/rum/httpd.pid
lockfile=${LOCKFILE-/var/lock/subsys/httpd} #创建的锁文件
RETVAL=0
start() {
echo-n $”Starting $prog: “
LANG=$HTTPD_LANG daemon –pidfile=${pidfile} $httpd $OPTIONS #以$pidfile文件执行httpd 并且使用选项start
RETVAL=$? #定义执行状态返回值
echo
[ $RETVAL = 0 ] && touch${lockfile} #成功时创建锁文件
return$RETVAL
}
stop() {
echo-n $”Stopping $prog: “
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm-f ${lockfile} ${pidfile}
}
reload() {
echo-n $”Reloading $prog: “
if! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo$”not reloading due to configuration syntax error”
failure $”not reloading $httpd due to configuration syntax error”
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case“$1″in
start)
start
;;
stop)
stop
;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if[ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo$”Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}”
exit1
esac
exit$RETVAL
[root@web httpd]# chmod +x /etc/init.d/httpd #添加执行权限
#设置开机自启动
[root@web httpd]# chkconfig httpd –add
[root@web httpd]# chkconfig httpd on
[root@web httpd]# chkconfig httpd –list
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@web httpd]#
6. 启动apache并测试
[root@web httpd]# service httpd start
Starting httpd: AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using web.test.com. Set the ‘ServerName’ directive globally to suppress this message
[ OK ]
[root@web httpd]#
[root@web httpd]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Programname
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 990/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1066/master
tcp 0 0 :::80 :::* LISTEN 36920/httpd
tcp 0 0 :::22 :::* LISTEN 990/sshd
tcp 0 0 ::1:25 :::* LISTEN 1066/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 890/dhclient
[root@web httpd]#
启动成功了,但似乎有警告,我们看一下!
Starting httpd: AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using web.test.com. Set the ‘ServerName’ directive globally to suppress this message
解决方案:
编辑httpd.conf文件,搜索“/ServerName”,添加ServerName localhost:80
[root@web httpd]# vim /etc/httpd/httpd.conf
再重新启动apache 即可。
[root@web httpd]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@web httpd]#
二、查询ab版本
[root@web ~]# ab -V
This is ApacheBench, Version 2.3 <$Revision: 1430300 $> #这是最新的版本 2.3
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
[root@web ~]#
三、查询ab参数
[root@web ~]# ab -v
ab: option requires an argument — v
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
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
-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.
-h Display usage information (this message)
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(SSL2, SSL3, TLS1 or ALL)
[root@web ~]#
参数详解:
-n 测试会话中所执行的请求个数,默认仅执行一个请求
-c 一次产生的请求个数,即同一时间发出多少个请求,默认为一次一个
-t 测试所进行的最大秒数,默认为无时间限制….其内部隐含值是[-n 50000],它可以使对服务器的测试限制在一个固定的总时间以内
-p 包含了需要POST的数据的文件
-T POST数据所使用的Content-type头信息
–v 设置显示信息的详细程度
-w 以HTML表格的形式输出结果,默认是白色背景的两列宽度的一张表
-i 以HTML表格的形式输出结果,默认是白色背景的两列宽度的一张表
-x 设置<table>属性的字符串,此属性被填入<table 这里>
-y 设置<tr>属性的字符串
-z 设置<td>属性的字符串
-C 对请求附加一个Cookie行,其典型形式是name=value的参数对,此参数可以重复
-H 对请求附加额外的头信息,此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如“Accept-Encoding: zip/zop;8bit”)
-A HTTP验证,用冒号:分隔传递用户名及密码
-P 无论服务器是否需要(即是否发送了401认证需求代码),此字符串都会被发送
-X 对请求使用代理服务器
-V 显示版本号并退出
-k 启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求,默认为不启用KeepAlive功能
-d 不显示“percentage served within XX [ms] table”的消息(为以前的版本提供支持)
-S 不显示中值和标准背离值,且均值和中值为标准背离值的1到2倍时,也不显示警告或出错信息,默认会显示最小值/均值/最大值等(为以前的版本提供支持)
-g 把所有测试结果写入一个‘gnuplot’或者TSV(以Tab分隔的)文件
-e 产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间
-h 显示使用方法
-k 发送keep-alive指令到服务器端
说明:我们平时用的最多的其实只有两个选项,-n,-c,更多请参考官网http://www.itlearner.com/code/apache2.2/programs/ab.html
四、测试过程
(1).选定测试页面
http://192.168.1.108/forum.php
(2).压力测试(案例1)
[root@web ~]# ab -n 1000 -c 10 http://192.168.1.108/forum.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.1.108 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
apr_pollset_poll: The timeout specified has expired (70007) #表示响应超负荷
Total of 991 requests completed #我这里只响应了991个,由于我这里测试使用的是虚拟机,所以测试时能接受的请求连接较少!
说明:
-n 表示总共发送的请求数
-c 表示每次并发的请求数
其实,使用ab测试时,其支持的最大并发数是没有限制的,但实际值要取决于Linux允许每个进程打开的最大文件数,即[ulimit -n],好了,下面我们调整一下参数,重新测试一下!
(3).案例2
[root@web ~]# ab -n 100 -c 10 -k http://192.168.18.41/forum.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.18.41 (be patient)…..done
Server Software: Apache/2.4.4 #被测试Web服务软件名称
Server Hostname: 192.168.18.41 #服务器主机名,即请求的URL中的主机部分名称
Server Port: 80 #被测试Web服务器软件的监听端口
Document Path: /forum.php #请求URL的绝问文件路径,即请求的资源
Document Length: 12428 bytes #请求文件的大小
Concurrency Level: 10 #并发数(-c属性来设置)
Time taken for tests: 1.014 seconds #执行完所有的请求所花费的时间,即整个测试持续的时间
Complete requests: 100 #完成的请求数量
Failed requests: 96 #失败的请求数量
(Connect: 0, Receive: 0, Length: 96, Exceptions: 0)
Write errors: 0
Keep-Alive requests: 0
Total transferred: 1243660 bytes #整个场景中的网络传输量,即所有请求的响应数据的总和,包含头信息和正文长度
HTML transferred: 1180016 bytes #整个场景中的HTML内容传输量,即所有请求中响应数据的正文长度,不包含头信息的长度
Requests per second: 98.61 [#/sec] (mean) #吞吐率:即每秒处理的请求数,相当于LR中的每秒事务数,括号中的mean表示这是一个平均值,其值为Complete requests/Time taken for tests
Time per request: 101.407 [ms] (mean) #服务器平均请求处理的时间,即每个请求实际运行时间的平均值,其值为Time per request/Concurrency Level
Time per request: 10.141 [ms] (mean, across all concurrent requests) #平均每秒网络上的流量,即这些请求在单位内从服务器获取的数据长度,其值为(Total transferred/Time taken for tests)/1024
Transfer rate: 1197.65 [Kbytes/sec] received #这个统计选项可以很好的说明服务器在处理能力达到极限时其出口带宽的需求量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
Connection Times (ms) #网络上消耗的时间的分解
min mean[+/-sd] median max
Connect: 0 1 0.7 1 4
Processing: 24 99 93.1 68 482
Waiting: 19 92 92.1 59 475
Total: 25 100 93.3 69 482
#整个场景中所有请求的响应情况,在场景中每个请求都有一个响应时间
#下面结果表明,50%的用户响应时间(即请求处理时间,这里处理时间是指Time per request)小于69毫秒,66%的用户响应时间小于97毫秒,而最大的响应时间小于1268毫秒
#对于并发请求,实际上CPU并不是同时处理的,而是按照每个请求获得的时间片而逐个轮转处理的,所以基本上第一个Time per request时间约等于第二个Time per request时间乘以并发请求数
Percentage of the requests served within a certain time (ms)
50% 69
66% 97
75% 117
80% 123
90% 224
95% 330
98% 452
99% 482
100% 482 (longest request)
[root@web ~]#
正文到此结束