ab(Apache Bench)命令详解以及压力测试模拟

发表于:2019-2-02 11:34

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:贾己人    来源:CSDN博客

  简介
  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.com,如下:
[root@jia1 ~]# ab -n 100 -c 10 http://www.baidu.com/
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 www.baidu.com (be patient).....done
Server Software:        BWS/1.1
Server Hostname:        www.baidu.com
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.com测试,错误率很高。这应该和www.baidu.com有关系。正常情况下,不会错误率这么高。
  不过了解了具体怎么分析即可。
  补充个测试www.sina.com的数据,总数100并发为20.失败为0.如下:
[root@jia1 ~]# ab -n 100 -c 20 http://www.sina.com/
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 www.sina.com (be patient).....done
Server Software:        nginx
Server Hostname:        www.sina.com
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

     上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号