## Apache Bench——ab性能测试工具
ab是一种用于测试Apache超文本传输协议(HTTP)服务器的工具。apache自带ab工具,可以测试
apache、IIs、tomcat、nginx等服务器
但是ab没有Jmeter、Loadrunner那样有各种场景设计、各种图形报告和监控,只需一个命令即可,有输出描述
可以简单的进行一些压力测试
ab命令同时满足http及https的请求
常用请求参数:-n请求次数,-c并发数
get请求测试 :
Ps:ab -n 1000 -c 100 http://127.0.0.1:8080/api/login/channel/hulai/name/lbb/age/23
post请求测试:
Ps:ab -n 1000 -c 100 -p ./login.json -T 'application/json' http://127.0.0.1:8080/api/login
以post的测试,结果分析如下:
~~~
This is ApacheBench, Version 2.3
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ #apache版本信息
Benchmarking 127.0.0.1 (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
Completed 1000 requests
Finished 1000 requests
Server Software: nginx #请求返回header类型,可能是nginx、apache、IIs等
Server Hostname: 127.0.0.1 #请求ip或者域名
Server Port: 8080 #请求端口,当前请求为https所以端口为443,请求https端口80
Document Path: /api/login #路径
Document Length: 1732 bytes #第一个成功返回的文档的字节大小
Concurrency Level: 100 #并发数!!!
Time taken for tests: 20.071 seconds #从建立连接到最后接受完成总时间
Complete requests: 1000 #总请求数成功的
Failed requests: 0 #失败的请求数
Write errors: 0
Total transferred: 1911000 bytes #从服务器接收的字节总数
Total POSTed: 631631
HTML transferred: 1732000 bytes #HTML接收字节数
Requests per second: 49.82 [#/sec] (mean) #————每秒请求数(总请求数/总时间)
Time per request: 2007.106 [ms] (mean) #————用户平均请求等待时间=concurrency * timetaken * 1000 / done
Time per request: 20.071 [ms] (mean, across all concurrent requests) #————服务器处理每个请求平均响应时间=timetaken * 1000 / done
Transfer rate: 92.98 [Kbytes/sec] received
30.73 kb/s sent
123.71 kb/s total
#网络连接情况
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 1
Processing: 85 1916 365.8 1838 3299
Waiting: 84 1916 365.8 1838 3299
Total: 85 1916 365.8 1838 3300
Percentage of the requests served within a certain time (ms)
50% 1838
66% 1947
75% 1980
80% 2020
90% 2293
95% 2626
98% 3032
99% 3166
100% 3300 (longest request)
~~~
### 高并发测试的注意点:
**1、Mysql数据库并发调整**
如果需要连接mysql数据库,先把mysql的并发连接数提高,my.cnf里面:
[mysqld]
~~~
thread_cache_size = 256
max_connections = 10000
~~~
否则在高并发时,mysql就会报错:
`Error 1040: Too many connections`
**2、ab错误:apr_socket_recv:Connection reset by peer (104)**
**解决方法一**:使用参数 -r,如 ab -r -n 10000 -c 1000 http://localhost:9090/hello
**解决方法二**:
apr_socket_recv这个是操作系统内核的一个参数,在高并发的情况下,内核会认为系统受到了SYN flood攻击,会发送cookies(possible SYN flooding on port 80. Sending cookies),这样会减慢影响请求的速度,所以在应用服务武器上设置下这个参数为0禁用系统保护就可以进行大并发测试了:
`# vim /etc/sysctl.conf`
net.ipv4.tcp_syncookies = 0
`# sysctl -p`
然后就可以超过1000个并发测试了。
**解决方法三:**
重新编译ab,修改ab.c文件,大约1380行处:
}else {
//apr_err("apr_socket_recv", status); // 从这开始
bad++;
close_connection(c);
return; // 到这里
}
**3、ab错误:apr_socket_recv:Connection timed out (110)**
`vi /etc/sysctl.conf`
在kernel2.6之前的添加项:
~~~
net.ipv4.netfilter.ip_conntrack_max =655360
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established= 180
kernel2.6之后的添加项:
net.nf_conntrack_max = 655360
net.netfilter.nf_conntrack_tcp_timeout_established= 1200
[long@longhao etc]# sysctl -p/etc/sysctl.conf
~~~
如果报错:error: “net.nf_conntrack_max” is an unknown key 则需要使用modprobe载入ip_conntrack模块,lsmod查看模块已载入。
`[long@longhao etc]# modprobe ip_conntrack`
**4、ab错误:apr_poll: Thetimeout specified has expired (70007)**
由于某些框架太大(比如laravel框架),超时了,可以增加 -k 参数,保持连接进行测试。
**5、 ab的-c选项的参数最大到20000,如果想再大,只能修改源码再安装**
**6、-c选项的参数不能大于-n的参数**
**7、Failed requests(失败的请求)**
在用ab测试的时候,只要出现Failed requests(失败的请求),就会出现三种失败的类型统计:Connect、Length、Exception。
~~~
Concurrency Level: 1000
Time taken for tests: 2.629 seconds
Complete requests: 100000
Failed requests: 10018
(Connect: 0, Receive: 0, Length: 10018, Exceptions: 0)
Write errors: 0
Non-2xx responses: 7
~~~
Connect:向服务器发送请求失败;服务器连接失败;请求过程连接中断等。
Length:服务器返回的数据长度不一致,一般是对比Content-Length的值。
Exception:与服务器连接过程中发生意外错误。
**注意:** 这里主要说明一下Length,ab会把第一次成功返回的content-length作为基准,如果后面的请求返回的content-length跟第一次的不一样,它就会把这次请求当成是失败了。对于动态类型(如PHP)的网站,每次服务器返回的数据都不一定相同,所以如果ab提示的是Length错误,基本都可以忽略掉。
**8、gzip压缩功能**
默认情况下,ab没有启用gzip压缩功能,所以压力测试的结果会跟实际情况有很大的偏差。要想让ab使用gzip压缩功能,得添加参数 -H 'Accept-Encoding: gzip'
`ab -H 'Accept-Encoding: gzip' www.xxx.com/`
**9、带参数的压力测试示例**
ab 'www.xxx.com/?a=1&b=2&c=3'