鼠年全马铁人挑战 WEEK 28:性能测试工具 Apache Bench

           Photo on Morioh by Francisco Barrows


前言

在各式各样的测试中 免不了会遇到不同的测试
而这週所要提的就是性能测试的方式
那关于性能测试的部分 应该怎么测又该如何测
那就看看这週所要介绍的内容吧~

如果想要进一步了解性能测试的大哥大姐们
可以回头看看小弟之前分享的文章
鼠年全马铁人挑战 WEEK 05:软体测试的种类 - 番外篇


介绍 Apache Bench

Apache Bench 又被简称为 ab
是由 Apache 开发的其中一个开源测试工具
主要用来测试 server (网站或 API) 的执行效能
并藉此查看执行后 server 的性能测试结果

安装 Apache Bench

Mac

MacOS 在默认情况下已经预先安装了 Apache Bench

Linux

在 Ubuntu 中可以直接透过 apt-get 来进行安装
$ apt-get update
$ apt-get install apache2-utils

Windows

如果使用的是 Windows 作业系统的话
可以到 Apache 的官网页面中进行下载
Apache VS16 binaries and modules download
而在官网所提供的版本是 MSI 的封装档 (Windows Installer)
换句话说就是连 Apache HTTP Server 也一併安装了
如果不想多执行 Apache HTTP Server 的话
可以在安装完后进入 Apache Group\Apache2\bin 目录
将 ab.exe 执行档额外拉出单独来使用

完成安装后 可以透过指令 $ ab -V 来确认 Apache Bench 的版本

使用 Apache Bench

使用 Apache Bench 的最简单方法是运行 ab <url>
例如 $ ab https://www.google.com/
该指令将执行一个 http request
但是只执行一个 http request 就无法达到开场所说的性能测试了
那是因为 Apache Bench 的功能远远不止这些

因此可以透过 $ ab -h 来查看 Apache Bench 的使用方式

-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.-m method       Method name-h              Display usage information (this message)-I              Disable TLS Server Name Indication (SNI) extension-Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)-f protocol     Specify SSL/TLS protocol                (TLS1, TLS1.1, TLS1.2 or ALL)-E certfile     Specify optional client certificate chain and private key

而通常被较常使用的是 -n 与 -c

-n:测试执行的总请求数,默认值为1
-c:一次执行多请求的数量,也就是模拟连线使用者数,默认值为1

因此举个例子 下方的指令就是 对 google 的连结进行请求
在短时间内请求 10 笔 request,并且模拟 2 位使用者同时进行
$ ab -n 10 -c 2 https://www.google.com/

cURL to ab 的工具介绍

另外这边小弟想要补充分享一下好用的工具
这个工具就是 网页版的 Curl2ab
这个网站可以透过已知的 curl 来快速转换成 ab 的形式
真的是非常的方便

Paste your cURL command:贴上你的 cURL 内容
Iteration:输入测试执行的总请求数
Concurrency:输入模拟连线使用者数

输入完资料后 网页下方就会同步的生成 ab 的指令

查看执行结果

藉由 ab 执行刚刚所说的指令 $ ab -n 10 -c 2 https://www.google.com/
就可以直接看到执行结果 大概长成这个样子

Benchmarking www.google.com (be patient).....doneServer Software:        gwsServer Hostname:        www.google.comServer Port:            443SSL/TLS Protocol:       TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256Server Temp Key:        ECDH X25519 253 bitsTLS Server Name:        www.google.comDocument Path:          /Document Length:        12502 bytesConcurrency Level:      2Time taken for tests:   0.675 secondsComplete requests:      10Failed requests:        9   (Connect: 0, Receive: 0, Length: 9, Exceptions: 0)Total transferred:      134189 bytesHTML transferred:       124849 bytesRequests per second:    14.82 [#/sec] (mean)Time per request:       134.946 [ms] (mean)Time per request:       67.473 [ms] (mean, across all concurrent requests)Transfer rate:          194.22 [Kbytes/sec] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:       46   66  25.5     56     126Processing:    43   54   9.9     53      75Waiting:       40   51   9.5     51      68Total:         97  120  28.0    110     184Percentage of the requests served within a certain time (ms)  50%    110  66%    129  75%    131  80%    146  90%    184  95%    184  98%    184  99%    184 100%    184 (longest request)

依照执行的结果 来一个个看 到底都回传了什么东西吧~

Server Software:Web主机的作业系统与版本 (Google Web Server > gws)
Server Hostname:Web主机的IP位址 (www.google.com)
Server Port:Web主机的连接埠 (443)

Document Path:网址的路径部分 (/)
Document Length:网页回应的网页大小 (12502 bytes)
Concurrency Level:同时进行压力测试的人数 (2)
Time taken for tests:本次压力测试所花费的总秒数 (0.675 seconds)
Complete requests:完成的要求数 (10)
Failed requests:失败的要求数 (9)
Total transferred:包括 HTTP Header 的测试总数据传输量 (134189 bytes)
HTML transferred:仅回传的 html 资料的测试总数据传输量(124849 bytes)
Requests per second:平均每秒可回应多少要求 (14.82 [#/sec])
Time per request:平均每个要求的时间 (134.946 [ms])
Time per request:平均每个要求的时间,所有同时连线数的平均值 (67.473 [ms])
Transfer rate:ab 到 Web Server 的网路传输速度 (194.22 [Kbytes/sec])
Connection Times (ms):试时的连线处理时间

横轴栏位的部分:

min: 最小值mean: 平均值(正、负标準差)median: 平均值(中间值)max: 最大值

纵轴栏位的部分:

Connect: ab 发 TCP 要求到 Web 主机所花费的建立时间Processing: TCP 连线建立后到收到全部 HTTP 回应资料的时间Waiting: HTTP 回应第一个 Byte 所等待的时间Total: Connect + Processing 的时间

结尾

以上是这週小弟所分享的内容
藉由 Apache Bench 来进行 server 的性能测试
并且查看执行玩的测试数据

如果有疑问或是有错误,还请各位大哥大姐提点。
小弟将继续往下週迈进。 ─=≡Σ((( つ•̀ω•́)つ


参考文献

官方网站

ab - Apache HTTP server benchmarking toolApache VS16 binaries and modules download

相关文章

网页伺服器监测站!!不能不知道的ApacheBench使用方法

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章