ab测试


安装

yum -y install httpd-tools # centos,redhat
apt-get install apache2-utils   #ubuntu Debian 

测试

ab -n1000 -c 10 http:/www.taoquan.ink/ # -n访问1000次, -c并发10个

参数:

//在测试会话中所执行的请求个数。默认时,仅执行一个请求
-n requests     Number of requests to perform
//一次产生的请求个数。默认是一次一个。
-c concurrency Number of multiple requests to make
//测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
-t timelimit    Seconds to max. wait for responses
//包含了需要POST的数据的文件.
-p postfile     File containing data to POST
//POST数据所使用的Content-type头信息。
-T content-type Content-type header for POSTing
//设置显示信息的详细程度 - 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
-v verbosity    How much troubleshooting info to print
//以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-w              Print out results in HTML tables
// 执行HEAD请求,而不是GET。
-i              Use HEAD instead of GET
 
//-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复。
-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 proxy-auth-username:password 对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。
-P attribute    Add Basic Proxy Authentication, the attributes
are a colon separated username and password.

常用

带参数和头的post

参数写到文件,头用-H

{"userId":"11","userKey":"11","giftId":"0","amount":"0","autoRefining":"0"}
ab -n 1000 -c 10  -p "post2.txt" -T "application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: d6xxs-sdf-sdf09d" -H "apiVersion:test" -H "phoneBrand:iPhone" -H "phoneSystem:iOS" -H "phoneModels:wgl-test" -H "appMarket:wgl-test" -H "appVersionCode:wgl-test" -H "appVersionName:wgl-test" "127.0.0.1:8668/user/action/my-backpack-gift-refining"

结果:

Server Software:        Apache          #服务器软件
Server Hostname:        www.taoquan.ink #域名
Server Port:            80              #请求端口号
Document Path:          /               #文件路径
Document Length:        40888 bytes     #页面字节数
Concurrency Level:      10              #请求的并发数
Time taken for tests:   27.300 seconds  #总访问时间
Complete requests:      1000            #请求成功数量
Failed requests:        0               #请求失败数量
Write errors:           0
Total transferred:      41054242 bytes  #请求总数据大小(包括header头信息)
HTML transferred:       40888000 bytes  #html页面实际总字节数
Requests per second:    36.63 [#/sec] (mean)  #每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request:       272.998 [ms] (mean)     #用户平均请求等待时间 
Time per request:       27.300 [ms] (mean, across all concurrent requests) # 服务器平均处理时间,也就是服务器吞吐量的倒数                  
Transfer rate:          1468.58 [Kbytes/sec] received  #每秒获取的数据长度
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       43   47   2.4     47      53
Processing:   189  224  40.7    215     895
Waiting:      102  128  38.6    118     794
Total:        233  270  41.3    263     945

Percentage of the requests served within a certain time (ms)
  50%    263    #50%用户请求在263ms内返回
  66%    271    #66%用户请求在271ms内返回
  75%    279    #75%用户请求在279ms内返回
  80%    285    #80%用户请求在285ms内返回
  90%    303    #90%用户请求在303ms内返回
  95%    320    #95%用户请求在320ms内返回
  98%    341    #98%用户请求在341ms内返回
  99%    373    #99%用户请求在373ms内返回
 100%    945 (longest request)