admin管理员组

文章数量:1558075

简介

ApacheBench 是 Apache服务器自带的一个web压力测试工具,简称ab。ab又是一个命令行工具,对发起负载的本机要求很低,根据ab命令可以创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问,因此可以用来测试目标服务器的负载压力。总的来说ab工具小巧简单,上手学习较快,可以提供需要的基本性能指标,但是没有图形化结果,不能监控。

语法及参数

Usage: ab [options] [http[s]://]hostname[:port]/path
用法:ab [选项] 地址

选项:
Options are:
    -n requests    #执行的请求数,即一共发起多少请求。
    -c concurrency    #请求并发数。
    -t timelimit    #测试所进行的最大秒数。其内部隐含值是-n 50000,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
    -s timeout    #指定每个请求的超时时间,默认是30秒。
    -b windowsize    #指定tcp窗口的大小,单位是字节。
    -B address    #指定在发起连接时绑定的ip地址是什么。
    -p postfile    #指定要POST的文件,同时要设置-T参数。
    -u putfile    #指定要PUT的文件,同时要设置-T参数。
    -T content-type    #指定使用POST或PUT上传文本时的文本类型,默认是'text/plain'。
    -v verbosity    #设置详细模式等级。
    -w    #将结果输出到html的表中。
    -i    #使用HEAD方式代替GET发起请求。
    -y attributes    #以表格方式输出时,设置html表格tr属性。 
    -z attributes    #以表格方式输出时,设置html表格th或td属性。
    -C attribute    #添加cookie,比如'Apache=1234'。(可重复)
    -H attribute    #为请求追加一个额外的头部,比如'Accept-Encoding: gzip'。(可重复)
    -A attribute    #对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即,是否发送了401认证需求代码),此字符串都会被发送。
    -P attribute    #对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。
    -X proxy:port   #指定代理服务器的IP和端口。
    -V              #打印版本信息。
    -k              #启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。
    -d              #不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)。
    -q              #如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。
    -g filename     #把所有测试结果写入一个'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。
    -e filename     #产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比'gnuplot'格式更有用。
    -r              #当收到错误时不要退出。
    -h              #输出帮助信息
    -Z ciphersuite  指定SSL/TLS密码套件
    -f protocol     指定SSL/TLS协议(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

示例以及结果分析
发起并发为10,总量为100的请求www.baidu,如下:

[root@jia1 ~]# ab -n 100 -c 10 http://www.baidu/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech/
Licensed to The Apache Software Foundation, http://www.apache/

Benchmarking www.baidu (be patient).....done


Server Software:        BWS/1.1
Server Hostname:        www.baidu
Server Port:            80


Document Path:          /      #测试页面
Document Length:        112439 bytes     #测试页面大小

Concurrency Level:      10     #并发数
Time taken for tests:   1.256 seconds    #整个测试话费的时间
Complete requests:      100    #完成请求的总量
Failed requests:        96     #失败的请求次数
   (Connect: 0, Receive: 0, Length: 96, Exceptions: 0)
Write errors:           0
Total transferred:      11348660 bytes    #传输数据总大小
HTML transferred:       11253726 bytes    #传输页面总大小
Requests per second:    79.62 [#/sec] (mean)    #平均每秒请求数
Time per request:       125.593 [ms] (mean)     #平均每次并发10个请求的处理时间
Time per request:       12.559 [ms] (mean, across all concurrent requests)   #平均每个请求处理时间,所有并发的请求加一起
Transfer rate:          8824.29 [Kbytes/sec] received  #平均每秒网络流量

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        4   20   7.7     18      38
Processing:    18   90  50.5     82     356
Waiting:        4   22   7.9     22      41
Total:         22  111  50.7    101     384
#花费在连接Connect,处理Processing,等待Waiting的时间的最小min,平均值mean,标准差[+/-sd],中值median,最大表max的一个表。

Percentage of the requests served within a certain time (ms)
  50%    101   #50%请求的响应时间在101ms内
  66%    103   #66%请求的响应时间在103ms内
  75%    104   #...以此类推
  80%    105
  90%    111
  95%    267
  98%    311
  99%    384
 100%    384 (longest request)

可以看到用www.baidu测试,错误率很高。这应该和www.baidu有关系。正常情况下,不会错误率这么高。
不过了解了具体怎么分析即可。
补充个测试www.sina的数据,总数100并发为20.失败为0.如下:

[root@jia1 ~]# ab -n 100 -c 20 http://www.sina/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech/
Licensed to The Apache Software Foundation, http://www.apache/

Benchmarking www.sina (be patient).....done


Server Software:        nginx
Server Hostname:        www.sina
Server Port:            80

Document Path:          /
Document Length:        178 bytes

Concurrency Level:      20
Time taken for tests:   0.081 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Non-2xx responses:      100
Total transferred:      71200 bytes
HTML transferred:       17800 bytes
Requests per second:    1231.15 [#/sec] (mean)
Time per request:       16.245 [ms] (mean)
Time per request:       0.812 [ms] (mean, across all concurrent requests)
Transfer rate:          856.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2    6   2.5      6      14
Processing:     3    7   3.2      5      14
Waiting:        3    6   2.7      5      14
Total:          7   13   3.8     12      23

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     14
  75%     15
  80%     16
  90%     17
  95%     21
  98%     21
  99%     23
 100%     23 (longest request)

还有就是ab属于一个轻量级的压测工具,结果不会特别准确,可以用作参考。
追求准确的话,推荐使用jmeter或者LoadRunner。

本文标签: 详解命令压力测试ABApache