Linux压力测试工具Apache Bench的安装和使用

Apache Bench的介绍

​  Apache Bench,简称ab,是Apache服务器自带的基准性能测试的工具。可用于压力测试,用来测试系统的吞吐量、服务端请求平均处理时间、用户请求平均等待时间等性能指标。

  ab命令上手很简单,通过ab命令可以创建并发线程模拟多用户访问或调用某一URL地址。ab命令的选项、输出的介绍可以参考其当前版本官网文档

  首先,我们介绍下ab检测的性能指标:

吞吐量

  吞吐量(Requests per second)是在某个并发度下服务器每秒处理的请求数。它是服务器并发处理能力的量化描述,单位是reqs/s。计算公式为:总请求数/处理请求的总耗时。 吞吐量越大说明服务器的性能越好。

请求平均处理时间

  请求平均处理时间(Time per request,across all concurrent requests)是服务器处理请求的平均时间,计算公式为:处理请求的总耗时/总请求数。它是服务器吞吐量的倒数。也等于,用户请求平均等待时间/并发用户数。

请求平均等待时间

  请求平均等待时间(Time per request)是用户等待请求响应的平均时间,计算公式为:处理请求的总耗时/(总请求数/并发用户数)。

  “请求平均处理时间”和“请求平均等待时间”两个概念非常容易混淆,举一个例子进行说明:比如100个用户同时执行上传文档的操作,那么并发用户数为100,假设服务器可以同时处理这100个请求,并且每个文件上传操作的耗时都是1s。那么请求总耗时时间为1s,吞吐量为100reqs/s。请求平均处理时间为0.01s,请求平均等待时间为1s。也就是说,请求平均处理时间是从服务器的角度出发的,请求平均等待时间是从用户的角度出发的。

Apache Bench的安装

执行sudo yum -y install httpd-tools 安装apache httpd的工具包,这个工具包中包含压力测试工具Apache Bench

#安装apache httpd的工具包
[root@Server ~]# yum install httpd-tools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed
--> Finished Dependency Resolution
#中间省略
Installed:
  httpd-tools.x86_64 0:2.4.6-80.el7.centos.1
Complete!

执行命令ab -V,如果成功打印ab的版本信息,则表明ab安装成功。

[ec2-user@ip-172-31-34-245 ~]$ ab
ab: wrong number of arguments
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.
    -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
                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
    -E certfile     Specify optional client certificate chain and private key

Apache Bench的使用和结果解读

  执行ab -n 100 -c 5 http://www.baidu.com/,对百度进行ab测试。命令中的-n表示总共请求100次,-c表示并发用户数为5,即使用5个并发线程同时请求。

  注意,此处填写的url不能只是域名,还要包含请求的路径,如果请求根目录,必须以"/"结尾。因此执行ab -n 100 -c 5 http://www.baidu.com是不行的。

#使用5个并发线程,对百度进行100次请求。
[user1@Server ~]$ ab -n 100 -c 5 http://www.baidu.com/
#打印ab的版本信息
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
​
#测试结果
#HTTP服务器信息
Server Software:       BWS/1.1
#服务器域名
Server Hostname:       www.baidu.com
#服务器端口号
Server Port:            80
#请求资源的标识符uri
Document Path:         /
#第一次成功请求响应的页面大小,如果后续请求响应的页面大小发生变化,会认为请求出错。
Document Length:        118155 bytes
#并发数
Concurrency Level:      5
#整个请求的总耗时
Time taken for tests:   1.244 seconds
#成功收到响应的请求数
Complete requests:      100
#失败请求数,失败包括连接,读取,不正确的内容长度或异常4种情况。
Failed requests:        98
   (Connect: 0, Receive: 0, Length: 98, Exceptions: 0)
#写入错误数
Write errors:           0
#测试过程从服务器接收到的数据总量(包含Header信息)
Total transferred:      11968154 bytes
#测试过程从服务器接收到的HTML页面的大小(不包含Hreader信息)
HTML transferred:       11872694 bytes
#服务器的吞吐量:平均每秒处理的请求数
Requests per second:    80.39 [#/sec] (mean)
#???
Time per request:       62.197 [ms] (mean)
#请求的平均处理时间,是服务器吞吐量的倒数
Time per request:       12.439 [ms] (mean, across all concurrent requests)
#每秒数据流量的大小
Transfer rate:          9395.67 [Kbytes/sec] received
#连接耗时
Connection Times (ms)
            #最小 平均                 最大
             min  mean[+/-sd] median   max
Connect:        6   10   5.3      7      34
Processing:    23   43  44.7     29     390
Waiting:        9   14   5.7     11      32
Total:         29   53  47.0     37     408
#特定耗时的请求数占比
Percentage of the requests served within a certain time (ms)
  50%     37            #50%的请求在37ms以内
  66%     55
  75%     60
  80%     63
  90%     76
  95%    102
  98%    273
  99%    408            #99%的请求在408ms以内
 100%    408 (longest request)

使用实例:

Linux压力测试工具Apache Bench的安装和使用
Linux压力测试工具Apache Bench的安装和使用
[ec2-user@ip-172-31-34-245 ~]$ ab -n 1000000 -c 5000 https://wawlsl.top/

//表示对https://wawlsl.top/使用5000个进程请求1000000次

部分信息来自:https://www.cnblogs.com/lonelyJay/p/9994920.html

原创文章,作者:Rosmontics,如若转载,请注明出处:https://rosmontis.com/archives/162

(0)
RosmonticsRosmontics网站管理员
上一篇 2022年3月20日 下午12:38
下一篇 2022年3月21日 上午11:01

相关推荐

发表回复

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

评论审核已启用。您的评论可能需要一段时间后才能被显示。