ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchmark testing,可以同时模拟多个并发请求。使用yum安装apache,ab工具在/usr/bin目录下:
[root@test ~]# ls /usr/bin/ab*
/usr/bin/ab
使用方法:
ab -n -c http://192.168.80.157/
若目标地址需用户认证,可加 -A 参数。
ab 命令的常用参数:
-n 数值: 发起测试的请求数
-c 数值: 发起测试的同时连接数
-A 用户名:密码
结果分析:
This is ApacheBench, Version 2.3
Copyright Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.80.157 (be patient)
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Completed requests
Finished requests Server Software: Apache/2.2.
Server Hostname: 192.168.80.157
Server Port: Document Path: /phpinfo.php
#测试的页面
Document Length: bytes
#页面大小 Concurrency Level:
#测试的并发数
Time taken for tests: 11.846 seconds
#整个测试持续的时间
Complete requests:
#完成的请求数量
Failed requests:
#失败的请求数量
Write errors:
Total transferred: bytes
#整个过程中的网络传输量
HTML transferred: bytes
#整个过程中的HTML内容传输量
Requests per second: 337.67 [#/sec] (mean)
#最重要的指标之一,相当于LR中的每秒事务数,后面括号中的mean表示这是一个平均值
Time per request: 2961.449 [ms] (mean)
#最重要的指标之二,相当于LR中的平均事务响应时间,后面括号中的mean表示这是一个平均值
Time per request: 2.961 [ms] (mean, across all concurrent requests)
#每个连接请求实际运行时间的平均值
Transfer rate: 16866.07 [Kbytes/sec] received
#平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1773.5
Processing: 1459.1
Waiting: 1459.8
Total: 2296.6
#网络上消耗的时间的分解,各项数据的具体算法还不是很清楚 Percentage of the requests served within a certain time (ms)
%
%
%
%
%
%
%
%
% (longest request)
#整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中50%的用户响应时间小于275毫秒,%的用户响应时间小于298毫秒,最大的响应时间小于11843毫秒。对于并发请求,cpu实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以基本上第一个Time per request时间约等于第二个Time per request时间乘以并发请求数。
测试过程遇到的问题:
1.apr_socket_recv: Connection reset by peer (104)
[root@test ~]# ab -n -c http://192.168.1.78/iwebshop/index.php
This is ApacheBench, Version 2.3 <$Revision: $>
Copyright Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.1.78 (be patient)
Completed requests
Completed requests
Completed requests
Completed requests
apr_socket_recv: Connection reset by peer ()
Total of requests completed
解决方法:增加参数 -r (Don't exit on socket receive errors.)
[root@test ~]# ab -n -c -r http://192.168.1.78/iwebshop/index.php
2.socket: Too many open files (24)
解决方法:查看当前要以打开的文件个数
[root@test ~]# ulimit -a
core file size (blocks, -c)
data seg size (kbytes, -d) unlimited
scheduling priority (-e)
file size (blocks, -f) unlimited
pending signals (-i)
max locked memory (kbytes, -l)
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size ( bytes, -p)
POSIX message queues (bytes, -q)
real-time priority (-r)
stack size (kbytes, -s)
cpu time (seconds, -t) unlimited
max user processes (-u)
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
调整可以打开的文件数
[root@test ~]# ulimit -n
重新执行该命令:
[root@test ~]# ab -n -c -r http://192.168.1.78/iwebshop/index.php
总结:在远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大),建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会
高很多。如果只有单独的一台服务器,可以直接本地测试,比远程测试效果要准确。