“我报名参与金石方案1期应战——瓜分10万奖池,这是我的第2篇文章,点击检查活动概况”

Apache Benchmark Tool

ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.

ab 是 apache http server 基准测验东西,它能够帮助我们去了解当时服务的体现,如每秒恳求数等。下面是 ab 对应的一切 options 摘要信息:

ab [ -A auth-username:password ] [ -b windowsize ] [ -B local-address ] [ -c
concurrency ] [ -C cookie-name=value ] [ -d ] [ -e csv-file ] [ -E client-certificate 
file ] [ -f protocol ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ 
-l ] [ -m HTTP-method ] [ -n requests ] [ -p POST-file ] [ -P proxy-auth-
username:password ] [ -q ] [ -r ] [ -s timeout ] [ -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

下面是各参数的解说

options keys exlpain
-A auth-username : password 向服务器供给 BASIC 身份验证凭证。用户名和密码由一个:分隔,并经过 wire base64编 码发送。不管服务器是否需求该字符串(即,是否发送了 401 身份验证),都将发送该字符串。
-b windowsize TCP发送/接纳缓冲区巨细,以字节为单位。
-B local-address 进行传出衔接时要绑定的地址。
-c concurrency 单次履行的并发 request 数量,默许一次履行一个恳求
-C cookie-name = value 给恳求添加 Cookie
-d Do not display the “percentage served within XX [ms] table”. (legacy support).不重要
-e csv-file 编写一个逗号分隔值 (CSV) 文件,其间包含每个百分比(从 1% 到 100%)处理该百分比恳求所需的时刻(以毫秒为单位)。这一般比“gnuplot”文件更有用;由于成果现已被“装箱”了。
-E client-certificate-file 衔接到 SSL 网站时,运用供给的 PEM 格式的客户端证书向服务器进行身份验证。该文件应包含客户端证书,然后是中间证书,然后是私钥。在 2.4.36 及更高版别中可用。
-f protocol 指定 SSL/TLS 协议(SSL2、SSL3、TLS1、TLS1.1、TLS1.2 或 ALL)。 TLS1.1 和 TLS1.2 支撑在 2.4.4 及更高版别中可用。
-H custom-header 支撑添加额外的 headers 到 request ;如 “Accept-Encoding: zip/zop;8bit”
-i 履行 HEAD 恳求而不是 GET 恳求。
-k 敞开 http keep-alive 特性,比如在一个 HTTP会话中履行多个恳求。默许为无 KeepAlive。
-m HTTP-method 自定义 http method,2.4.10 之后版别可用
-n requests 基准测验会话履行的恳求数。默许状况下只履行单个恳求,这一般会导致不具有代表性的基准测验成果。
-p POST-file 包含要POST数据的文件。记住还要设置-T。
-P proxy-auth-username : password 在途中向代理供给BASIC身份验证凭证。用户名和密码由一个:分隔,并经过 wire base64编码发送。不管代理是否需求该字符串(即,是否发送了需求的407代理身份验证),都将发送该字符串。
-q 当处理超过150个恳求时,ab每10%或大约100个恳求输出一个stderr进度计数。-q标志将抑制这些音讯。
-r 不要在套接字接纳过错时退出。
-s timeout 在套接字超时之前等待的最大秒数。默许为30秒。在2.4.4及今后版别中可用。
-S 不要显示中值和标准偏差值,也不要在均匀值和中值相差超过标准偏差的一倍或两倍时显示正告/过错音讯。默许为min/avg/max值。(留传支撑)
-t timelimit 用于基准测验的最大秒数。这在内部意味着-n 50000。运用它能够在固定的总时刻内对服务器进行基准测验。默许状况下没有时刻限制。
-T content-type 用于 POST/PUT 数据 数据的内容类型标头;如application/x-www-form-urlencoded. 默许 text/plain
-u PUT-file 包含要PUT的数据的文件。记住还要设置-T。
-v verbosity 设置具体级别- 4及以大将打印标题信息,3及以大将打印呼应代码(404、200等),2及以大将打印正告和信息。

运用 ab 进行测验

下面是一个十分简略的 api 代码,发动 web server 之后经过 http://localhost:8080/api/test 即可恳求。

@RestController
@RequestMapping("api")
public class PressureService {
    @RequestMapping("test")
    public String test() throws InterruptedException {
        Thread.sleep(3000);
        return "abc" + new String("bcd");
    }
}

履行如下命令 ab -n 200 -c 100 http://localhost:8080/api/test;一共发送 200 个恳求,每次恳求并发 100,得到的成果如下:

➜  ~ ab -n 200 -c 100  http://localhost:8080/api/test
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
apr_pollset_poll: The timeout specified has expired (70007)

能够看到这儿没有得到正常的测验反馈成果;报了 apr_pollset_poll: The timeout specified has expired (70007);呈现这个 maessage 的原因是 timeout 衔接超时了。 经过上面那个表中的 options 来看,能够经过加 -k 和 -s 来分别指定 下 keep-alive 和 超时时刻,也能够经过 -r 来躲避遇到socket接纳过错后,不退出测验。

运用 -k 和 -s

  ~ ab -n 200 -c 10 -s 5000 -k  http://localhost:8080/api/test
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software:
Server Hostname:        localhost
Server Port:            8080
Document Path:          /api/test
Document Length:        6 bytes
Concurrency Level:      10
Time taken for tests:   352.210 seconds
Complete requests:      200
Failed requests:        8
   (Connect: 0, Receive: 0, Length: 8, Exceptions: 0)
Keep-Alive requests:    192
Total transferred:      27840 bytes
HTML transferred:       1152 bytes
Requests per second:    0.57 [#/sec] (mean)
Time per request:       17610.485 [ms] (mean)
Time per request:       1761.049 [ms] (mean, across all concurrent requests)
Transfer rate:          0.08 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:  3002 15972 44971.8   3006  218613
Waiting:        0 11492 40519.5   3006  218613
Total:       3002 15972 44971.9   3006  218613
Percentage of the requests served within a certain time (ms)
  50%   3006
  66%   3006
  75%   3007
  80%   3007
  90%   3008
  95%  112001
  98%  209597
  99%  215607
 100%  218613 (longest request)

运用 -r

在不适用 -r 的恳求下,

  ~ ab -n 200 -c 100 -s 5000 -k -r  http://localhost:8080/api/test
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software:
Server Hostname:        localhost
Server Port:            8080
Document Path:          /api/test
Document Length:        6 bytes
Concurrency Level:      100
Time taken for tests:   107.100 seconds
Complete requests:      200
Failed requests:        147
   (Connect: 0, Receive: 0, Length: 147, Exceptions: 0)
Keep-Alive requests:    53
Total transferred:      7685 bytes
HTML transferred:       318 bytes
Requests per second:    1.87 [#/sec] (mean)
Time per request:       53549.816 [ms] (mean)
Time per request:       535.498 [ms] (mean, across all concurrent requests)
Transfer rate:          0.07 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   2.1      3       8
Processing:  3002 43074 19044.3  49998   75116
Waiting:        0 5502 15736.0      0   75116
Total:       3002 43077 19045.5  50002   75117
Percentage of the requests served within a certain time (ms)
  50%  50002
  66%  50003
  75%  54088
  80%  54089
  90%  54090
  95%  54092
  98%  66104
  99%  72112
 100%  75117 (longest request)

Connection reset by peer

➜  ~ ab -n 200 -c 100 -s 5000 -k  http://localhost:8080/api/test
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
apr_socket_recv: Connection reset by peer (54)

Connection reset by peer也会导致 ab 半途退出,从本地测验来看,当并发数增加时,呈现该问题的比率会增加;Connection Reset by peer 意味着远端终止了会话,即当操作体系接纳到远端服务器的RST (TCP Reset)通知时会发生此过错。在运用 ab 时,能够经过 -r 来疏忽这种过错,在遇到socket接纳过错后,不退出测验。

ab 的成果解析

版别相关

This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

这部分信息是 ab 的版别信息,能够疏忽。

测验完结度

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests

以上内容显示测验完结度,经过分行显示当时完结数量。

测验服务器信息

Server Software:
Server Hostname:        localhost
Server Port:            8080

Server Software 一栏没有数据,由于本地发动的 server 容器;其他两个便是地址和端口。

地址和包巨细

Document Path:          /api/test
Document Length:        6 bytes

测验数据概况

Concurrency Level:      100
Time taken for tests:   107.100 seconds
Complete requests:      200
Failed requests:        147
   (Connect: 0, Receive: 0, Length: 147, Exceptions: 0)
Keep-Alive requests:    53
Total transferred:      7685 bytes
HTML transferred:       318 bytes
Requests per second:    1.87 [#/sec] (mean)
Time per request:       53549.816 [ms] (mean)
Time per request:       535.498 [ms] (mean, across all concurrent requests)
Transfer rate:          0.07 [Kbytes/sec] received
  • Concurrency Level:并发数,-c 参数值
  • Time taken for tests:本次测验一共花费的时刻
  • Complete requests:本次恳求履行的恳求数总量
  • Failed requests:失败的恳求数量。因网络原因或服务器性能原因,建议的恳求并不一定悉数成功,经过该数值和Complete requests相除能够核算恳求的失败率,作为测验成果的重要参阅。
  • Keep-Alive requests:运用长衔接的恳求数量
  • Total transferred:一共传输的数据量,指的是 ab 从被测服务器接纳到的总数据量,包含文本内容和恳求头信息。
  • HTML transferred:从服务器接纳到的 html 文件的总巨细,等于 Document Length*Complete requests
  • Requests per second:均匀每秒完结的恳求数:QPS,是一个均匀值,等于 Complete requests/Time taken for tests
  • Time per request:从用户视点看,完结一个恳求所需求的时刻(因用户数量不止一个,服务器完结 10 个恳求,均匀每个用户才接纳到一个完整的回来,所以该值是下一项数值的10倍。)
  • Time per request:服务器完结一个恳求的时刻。
  • Transfer rate:网络传输速度,对于大文件的恳求测验,这个值很简单成为体系瓶颈所在。要确认该值是不是瓶颈,需求了解客户端和被测服务器之间的网络状况,包含网络带宽和网卡速度等信息。

Time per request 计算信息

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   2.1      3       8
Processing:  3002 43074 19044.3  49998   75116
Waiting:        0 5502 15736.0      0   75116
Total:       3002 43077 19045.5  50002   75117

对上面第一个 Time per request 进行细分和计算。一个恳求的呼应时刻能够分成网络链接(Connect),体系处理(Processing)和等待(Waiting)三个部分。表中 min 表明最小值; mean 表明均匀值;[+/-sd] 表明标准差(Standard Deviation) ,也称均方差(mean square error),该数值越大表明数据越涣散,体系呼应时刻越不稳定。 median 表明中位数; max 当表明最大值。

Total 是从整个恳求所需求的时刻的视点来计算的。这儿能够看到最慢的一个恳求花费了 75117 ms,这个数据能够在下面的表中得到验证。

Percentage of the requests served within a certain time (ms)
  50%  50002
  66%  50003
  75%  54088
  80%  54089
  90%  54090
  95%  54092
  98%  66104
  99%  72112
 100%  75117 (longest request)

这个表第一行表明有 50% 的恳求都是在 50002 ms 内完结的,这个值是比较接近均匀体系呼应时刻(第一个 Time per request: 53549.816 ms ); 以此类推。

前面看到呼应时刻最长的那个恳求是 75117 ms,那么显然一切恳求(100%)的时刻都是小于等于 75117 ms 的,也便是表中最后一行的数据便是时刻最长的那个恳求(longest request)。

参阅

  • www.cnblogs.com/gumuzi/p/56…
  • knowledge.broadcom.com/external/ar…
  • github.com/vibora-io/v…
  • httpd.apache.org/docs/2.4/pr…