ab命令原理
Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx、lighthttp、IIS等其它Web服务器的压力。
ab命令对发出负载的计算机要求很低,既不会占用很多CPU,也不会占用太多的内存,但却会给目标服务器造成巨大的负载,因此是某些DDOS攻击之必备良药,老少皆宜。自己使用也须谨慎。否则一次上太多的负载,造成目标服务器直接因内存耗光死机,而不得不硬重启,得不偿失。
在带宽不足的情况下,最好是本机进行测试,建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)
ab安装于使用
这部分较为简单,从官网上进行下载即可,或者你如果在Windows下使用集成环境,在apache/bin/ab.exe 可直接使用
ab使用
1) GET测试
ab -c 2 -n 10 http://domain/optimalServer.do?username=zhang&sex=nan
-n发出10个请求,-c模拟2并发,相当2人同时访问,后面是测试url
2) POST测试
.\abs.exe -c 5 -n 5 -T "application/x-www-form-urlencoded" -p ab.txt https://api.example.com/api/activity181212/assist?shopid=1
post中提交的内容保存在一个文件中:ab.txt
activityId=7&auth_access_token=8f94acd6428576be6296151091cc1c88Nh1ITRXIPL36A6Xejf15435618192&inviterUid=l2iYaJpo
This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking twapi.feekr.com (be patient).....done
Server Software: nginx/1.6.0
Server Hostname: twapi.feekr.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
Document Path: /api/activity181212/assist?shopid=PK
Document Length: 63 bytes
Concurrency Level: 5
Time taken for tests: 0.211 seconds
Complete requests: 5
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Total transferred: 2857 bytes
Total body sent: 1505
HTML transferred: 372 bytes
Requests per second: 23.65 [#/sec] (mean)
Time per request: 211.434 [ms] (mean)
Time per request: 42.287 [ms] (mean, across all concurrent requests)
Transfer rate: 13.20 [Kbytes/sec] received
6.95 kb/s sent
20.15 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 17 23 6.4 23 33
Processing: 57 118 45.6 131 178
Waiting: 57 118 45.6 131 178
Total: 74 141 45.9 153 199
Percentage of the requests served within a certain time (ms)
50% 148
66% 158
75% 158
80% 199
90% 199
95% 199
98% 199
99% 199
100% 199 (longest request)
参数说明:
-p: 文件完整路径
-n: 请求的个数
-c: 并发的数量
-t: 时间限制,默认没有限制
-T: post数据所使用的Content-type头信息