先引用一段官方介绍,html
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等。nginx
要注意的是,ab的测试目标是基于URL的,因此基于TCP的服务接口时无法用ab测试的。apache
个人运行环境是macOS 10.14.6。tomcat
其实mac自己是带来了ab
工具的,你能够试试在终端窗口执行服务器
ab -v
能够看到ab的版本。并发
可是这个自带的版本会有些问题,好比我在实际测试的发现以下现象,app
$ 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版本太低引发的。因此要安装一个比较新的版本替换掉这个自带的。socket
首先要下载ab工具,咱们能够去下面这个地址找到最新的版本而后下载便可。工具
http://httpd.apache.org/download.cgipost
我这里下载的是 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)
我先解释下测试结果的各个指标都是啥意思。
基于这几个指标,qq的网站和个人博客站点哪一个性能好,就不用多说了吧,哈哈!
上面两个测试都是基于get请求的,基于post请求也很容易,这里就很少说了。有兴趣能够本身测试下。
参考: