性能测试工具ApacheBench

ApacheBench简介

ApacheBench是一个用来衡量http服务器性能的单线程命令行工具。原本针对Apache http服务器,但是也适用于其他http服务器。

ab工具与标准Apache源码一起发布,免费,开源,基于Apache许可证

ApacheBench安装

ubuntu执行

apt-get install apache2-utils

即可。

centos执行

yum install httpd-tools

即可

ApacheBench快速入门

发送1000个http get请求到[ http://imotao.com,并发为10]:

ab -n1000 -c10 https://imotao.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 imotao.com (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:        NWSs
Server Hostname:        imotao.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,AES256-GCM-SHA384,2048,256

Document Path:          /
Document Length:        162 bytes

Concurrency Level:      10
Time taken for tests:   207.862 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Non-2xx responses:      1000
Total transferred:      643000 bytes
HTML transferred:       162000 bytes
Requests per second:    4.81 [#/sec] (mean)
Time per request:       2078.621 [ms] (mean)
Time per request:       207.862 [ms] (mean, across all concurrent requests)
Transfer rate:          3.02 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      574 1406 1987.9    632   32496
Processing:   387  626 539.1    441    9950
Waiting:      387  626 539.1    441    9950
Total:        982 2032 2075.6   1641   32945

Percentage of the requests served within a certain time (ms)
  50%   1641
  66%   2040
  75%   2260
  80%   2338
  90%   3406
  95%   4730
  98%   8149
  99%   8743
 100%  32945 (longest request)

注意每个请求的时间:0.658 [ms](mean)是平均每批请求的完成时间,这里时10个请求完成的时间。每个请求的时间:0.066 [ms](意思是,跨所有并发请求)是平均每个请求的完成时间。

ApacheBench将只使用一个操作系统线程。很多情况下ApacheBench本身是瓶颈。当在硬件具有多个处理器核心使用ApacheBench,建议一个CPU核执行一个AB线程,但是现在CPU核数很多的情况下,比如48核,手工执行48个命令也是个体力活。

整体来说ab因为不能利用到多核,不能进行参数化,不支持http和https以外的协议等原因,只适合单个组件的粗略测试,比如Apache http。不合适业务级别的测试,如一旦后台有数据库等,AB的测试是几乎没有意义的。

ApacheBench参考

  • 命令行帮助
$ ab -h
  Usage: ab [options] [http[s]://]hostname[:port]/path
  Options are:
      -n requests     Number of requests to perform    -c concurrency  Number of multiple requests to make at a time    -t timelimit    Seconds to max. to spend on benchmarking
                      This implies -n 50000
      -s timeout      Seconds to max. wait for each response
                      Default is 30 seconds    -b windowsize   Size of TCP send/receive buffer, in bytes
      -B address      Address to bind to when making outgoing connections    -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 to use for POST/PUT data, 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.
      -q              Do not show progress when doing more than 150 requests    -l              Accept variable document length (use this for dynamic pages)
      -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                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)


  • 语法:
ab [ -A auth-username:password ] [ -b windowsize ] [ -c concurrency ] [ -C cookie-name=value ] [ -d ] [ -e csv-file ] [ -f protocol ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ -n requests ] [ -p POST-file ] [ -P proxy-auth-username:password ] [ -q ] [ -r ] [ -s ] [ -S ] [ -t timelimit ] [ -T content-type ] [ -u PUT-file ] [ -v verbosity] [ -V ] [ -w ] [ -x <table>-attributes ] [ -X proxy[:port] ] [ -y <tr>-attributes ] [ -z <td>-attributes ] [ -Z ciphersuite ] [http[s]://]hostname[:port]/path
  • 命令行选项
-A auth-username:password    向服务器提供基本认证信息。用户名和密码之间":"分割,以base64编码形式发送。无论服务器是否需要(即是否发送了401)都发送。 

-b windowsize    TCP发送/接收缓冲区大小,以字节为单位。

-c concurrency    并发数,默认为1。

-C cookie-name=value    添加Cookie。典型形式是name=value对。name参数可以重复。 

-d不显示"percentage served within XX [ms] table"消息(兼容以前的版本)。 

-e csv-file    输出百分率和对应的时间,格式为逗号份额的csv。由于这种格式已经"二进制化",所以比"gnuplot"格式更有用。

-f protocol    SSL/TLS protocol (SSL2, SSL3, TLS1, 或ALL).

-g gnuplot-file    把所有测试结果写入"gnuplot"或者TSV(以Tab分隔)文件。该文件可以方便地导入到Gnuplot, IDL, Mathematica甚至Excel中,第一行为标题。

-h    显示使用方法。

-H custom-header    附加额外头信息。典型形式有效的头信息行,包含冒号分隔的字段和值(如:"Accept-Encoding: zip/zop;8bit")。

-i    执行HEAD请求,而不是GET 。

-k    启用KeepAlive功能,即在HTTP会话中执行多个请求。默认关闭。

-n requests    会话执行的请求数。默认为1。 

-p POST-file    附加包含POST数据的文件。注意和-T一起使用。

-P proxy-auth-username:password    代理认证。用户名和密码之间":"分割,以base64编码形式发送。无论服务器是否需要(即是否发送了407)都发送。

-q    quiet,静默模式。不在stderr输出进度条。

-r    套接字接收错误时不退出。

-s timeout     超时,默认为30秒。

-S    不显示中值和标准偏差值,而且在均值和中值为标准偏差值的1到2倍时,也不显示警告或出错信息。默认显示最小值/均值/最大值。(兼容以前的版本)-t timelimit
    测试进行的最大秒数。内部隐含值是"-n 50000"。默认没有时间限制。

-T content-type    POST/PUT的"Content-type"头信息。比如“application/x-www-form-urlencoded”,默认“text/plain”。

-v verbosity    详细模式,4以上会显示头信息,3以上显示响应代码(404,200等),2以上显示告警和info。

-V    显示版本号并退出。

-w    以HTML表格形式输出。默认是白色背景的两列。

-x <table>-attributes    设置<table>属性。此属性填入<table 这里 > 。

-X proxy[:port]    使用代理服务器。

-y <tr>-attributes    设置<tr>属性。

-z <td>-attributes    设置<td>属性。 

-Z ciphersuite    设置SSL/TLS加密
  • 结果分析字段
Server Software
    返回的第一次成功的服务器响应的HTTP头。

Server Hostname
    命令行中给出的域名或IP地址

Server Port
    命令行中给出端口。如果没有80(HTTP)和443(HTTPS)。

SSL/TLS Protocol
    使用SSL打印。

Document Path
    命令行请求的路径。

Document Length
    第一次成功地返回文档的字节大小。后面接受的文档长度变化时,会认为是错误。

Concurrency Level
    并发数

Time taken for tests
    测试耗时

Complete requests
    收到成功响应数

Failed requests
    失败请求数。如果有会打印错误原因

Write errors
   写错误数 (broken pipe)Non-2xx responses
    非2**响应数量。如果有打印。

Keep-Alive requests
    Keep-Alive请求的连接数

Total body sent:
    传输的body的数据量,比如POST的数据。

Total transferred:    总传输数据量

HTML transferred:       
    累计html传输数据量

Time per request:      
    每批平均请求时间

Time per request: 
    每次平均请求时间。计算公式:Time per request/Concurrency Level。

Transfer rate
    数据传输速率。计算公式:otalread / 1024 / timetaken。

原创文章,作者:陌涛,如若转载,请注明出处:https://imotao.com/445.html

(0)
陌涛的头像陌涛
上一篇 2019年7月10日
下一篇 2019年7月11日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据