Apache Benchmark(简称ab) 是Apache安装包中自带的压力测试工具 ,简单易用。
这玩意是个神器,之前一直有在用,但是没有仔细研究,最近可能会有并发量比较大的需求,使用ab来做压力。这里大概记录一下ab的一些使用规范。
我的服务器是腾讯云的,系统是centos7.6
1:安装ab
这里给出两种安装命令,分别是ubuntu和centos。
Centos:
yum -y install httpd-tools
Ubuntu:
sudo apt-get install apache2-utils
2:ab压力测试命令
Ab -c 100 -n 1000 https://guanchao.site/
下面部分,有注释的地方,需要重点关注一下。
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 guanchao.site (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/1.16.1 Server Hostname: guanchao.site Server Port: 80 Document Path: / Document Length: 169 bytes Concurrency Level: 100 //整个测试持续的时间 Time taken for tests: 13.053 seconds // 总请求次数 Complete requests: 1000 // 请求失败次数 Failed requests: 0 Write errors: 0 Non-2xx responses: 1000 // 一共发送数据量 Total transferred: 361000 bytes HTML transferred: 169000 bytes //平均每秒处理30个请求 Requests per second: 76.61 [#/sec] (mean) //平均每个请求处理时间为1305毫秒 注:这里将一次100个并发请求看成一个整体 Time per request: 1305.266 [ms] (mean) // 单次访问时间 Time per request: 13.053 [ms] (mean, across all concurrent requests) Transfer rate: 27.01 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 2 487 828.8 2 7010 Processing: 2 238 865.8 2 12040 Waiting: 2 228 864.6 2 12040 Total: 4 725 1259.1 16 13042 Percentage of the requests served within a certain time (ms) // 50%的请求在16ms中完成 50% 16 66% 1004 75% 1017 80% 1207 90% 2232 95% 3262 98% 3759 99% 5912 // 最长请求时间13042ms 100% 13042 (longest request)
ab -c 5000 -n 10000 'http://guanchao.site/'
5000次并发,请求10000次,服务器直接返回错误,说明,我刚刚测试的这个接口,并发支持5000的并发量,有待优化。
[root@VM_0_4_centos ~]# ab -c 5000 -n 10000 'http://guanchao.site/' 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 guanchao.site (be patient) Completed 1000 requests apr_socket_recv: Connection reset by peer (104) Total of 1199 requests completed 3:关于ab参数的解释 Usage: ab [options] [http[s]://]hostname[:port]/path Options are: //总的请求数 -n requests Number of requests to perform宅 //一次同时并发的请求数 总的请求数(n)=次数*一次并发数(c) -c concurrency Number of multiple requests to make -t timelimit Seconds to max. wait for responses -b windowsize Size of TCP send/receive buffer, in bytes // 带参数请求,参数放入一个文件中,但是一般不建议带参数,我觉得麻烦。 -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 for POSTing, 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. -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)
3:测试的几个原则
1、测试工具和测试数据时,使用到别人的网址时,-n和-c的参数不能太大。
2、测试当前的机器,最好用另一台机器测试。
3、测试修改结果,最好是某个功能完善后才测,否则会导致结果有差异。
一般的流量用ab测试基本上够用了,当然,ab也有windows版本的,好像是,我没用过,windows做并发测试我一般用的也是apache旗下的一款产品,jmeter。回头有时间,记录一下。
最后,大概说一下我在使用ab做压力测试的一个小总结
最开始用的时候,我这边就去调用正式的接口去做测试,但是有的接口他是做写入操作的,压力测试的话,还需要带数据,当然,这个ab是支持的,使用-p 然后将请求的参数写入文件中,
请求的命令大概是这样的
ab -c 5000 -n 10000 -p a.txt 'http://guanchao.site/'
但是个人觉得没必要那么麻烦,比如我测试的接口是学生选课的接口,不同的用户,同时并发写入,参数可能就不是那么容易。
我们完全可以将前端传递的参数在方法中写死,或者随机生成,然后,查询和写入依旧,其实就可以了。