先引用一段官方介绍,
ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.
意思是ab是apache的http服务测试工具,它可以测试你的http server的性能,尤其是每秒处理请求数。ab不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。
要注意的是,ab的测试目标是基于URL的,所以基于TCP的服务接口时没法用ab测试的。
我的运行环境是macOS 10.14.6。
其实mac本身是带来了ab
工具的,你可以试试在终端窗口执行
ab -v
可以看到ab的版本。
但是这个自带的版本会有些问题,比如我在实际测试的发现如下现象,
$ ab -n 1000 -c 200 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
apr_socket_recv: Connection reset by peer (54)
Total of 524 requests completed
上网查了下,了解到这个报错是因为ab版本过低引起的。所以要安装一个比较新的版本替换掉这个自带的。
首先要下载ab工具,我们可以去下面这个地址找到最新的版本然后下载即可。
http://httpd.apache.org/download.cgi
我这里下载的是 httpd-2.4.41.tar.gz
这个版本。大家只要是下载一个最新的文档版本就行了。
还需要安装几个依赖,apr
和apr-uitl
,还有pcre
。这个可以在下面这个地址找到最新的版本下载。
http://apr.apache.org/download.cgi
https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz
安装的过程如下。
安装apr,进入源码目录
./configure --prefix=/usr/local/apr
sudo make
sudo make install
安装apr-uitl,进入源码目录
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
sudo make
sudo make install
安装pcre,进入源码目录
./configure --prefix=/usr/local/pcre
安装httpd,进入源码目录
./configure --prefix=/usr/local/httpd/ --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/
sudo make
sudo make install
然后拷贝/usr/local/httpd目录下的ab执行文件替换/usr/sbin下的。
另外,根据自己的实际情况,还可以调整下ulimit -n
参数,否则测试的时候可能会报too many open fiels
。
ponydeMacBook-Pro:httpd ponyma$ ulimit -n 5000
ponydeMacBook-Pro:httpd ponyma$ ulimit -n
5000
ab的命令很多,这里给出比较常用的几个,等下的示例也会用到。
-n 请求的总数,默认是1次。
-c 并发数,同一时间请求数。
-p 如果后面接的是文件,表示POST请求,文件中包含请求数据,根据数据格式,设置-T参数。
-T 设置请求头中的Content-type(针对POST/PUT),比如:application/x-www-form-urlencoded。默认是text/plain。
-w 将测试结果打印到HTML表格中。
更多的命令可以通过 man ab
查询。
我们先不测试自己的服务,先用qq的网站看看测试结果,一会也好做对比。
$ ./bin/ab -n 200 -c 100 http://www.qq.com/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.qq.com (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software: nginx
Server Hostname: www.qq.com
Server Port: 80
Document Path: /
Document Length: 154 bytes
Concurrency Level: 100
Time taken for tests: 0.387 seconds
Complete requests: 200
Failed requests: 0
Non-2xx responses: 200
Total transferred: 67200 bytes
HTML transferred: 30800 bytes
Requests per second: 516.36 [#/sec] (mean)
Time per request: 193.661 [ms] (mean)
Time per request: 1.937 [ms] (mean, across all concurrent requests)
Transfer rate: 169.43 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 14 29 7.7 28 49
Processing: 16 66 72.6 34 315
Waiting: 16 50 45.8 34 315
Total: 39 95 73.6 62 350
Percentage of the requests served within a certain time (ms)
50% 62
66% 81
75% 85
80% 129
90% 185
95% 310
98% 341
99% 349
100% 350 (longest request)
然后我再测试下我自己的博客站点,
我的博客
$ ab -n 200 -c 100 http://www.machengyu.net/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.machengyu.net (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software: GitHub.com
Server Hostname: www.machengyu.net
Server Port: 80
Document Path: /
Document Length: 18750 bytes
Concurrency Level: 100
Time taken for tests: 1.863 seconds
Complete requests: 200
Failed requests: 0
Total transferred: 3877599 bytes
HTML transferred: 3750000 bytes
Requests per second: 107.33 [#/sec] (mean)
Time per request: 931.721 [ms] (mean)
Time per request: 9.317 [ms] (mean, across all concurrent requests)
Transfer rate: 2032.11 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 97 118 17.6 116 199
Processing: 205 434 259.1 282 1218
Waiting: 100 259 173.9 159 661
Total: 308 552 258.2 403 1328
Percentage of the requests served within a certain time (ms)
50% 403
66% 599
75% 632
80% 690
90% 986
95% 1183
98% 1285
99% 1310
100% 1328 (longest request)
我先解释下测试结果的各个指标都是啥意思。
Concurrency Level 并发数多少,等于-c后面的数值
Time taken for tests 测试总耗时
Complete requests 成功收到返回的数目
Failed requests 请求失败数目
Non-2xx responses 表示返回的HTTP status code不是2xx的数目(比如404,401,500…),如果都是2xx,这个指标不显示在结果里面
Requests per second 每秒请求数
Time per request 每一个请求平均花费时间。第一个Time per request等于concurrency * timetaken * 1000 / done,第二个Time per request(mean, across all concurrency requests)等于timetaken * 1000 / done,第一个可以理解为用户平均请求等待时间,第二可以理解为服务器平均请求等待时间
基于这几个指标,qq的网站和我的博客站点哪个性能好,就不用多说了吧,哈哈!
上面两个测试都是基于get请求的,基于post请求也很容易,这里就不多说了。有兴趣可以自己测试下。
参考:
https://www.cnblogs.com/cjsblog/p/9038838.html