Web性能压力测试工具之ApacheBench(ab)详解

本文详细介绍了ApacheBench(ab)压力测试工具的使用方法及各项参数含义,包括安装方式、基本使用实例及结果解读,并重点阐述了如何通过ab工具评估Web服务器性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、简介

ApacheBench 是apache中自带的一个指令列程式,专门用来执行网站服务器的运行效能,特别是针对Apache 网站服务器。这原本是用来检测 Apache 网站服务器能够提供的效能,特别是可以看出Apache能提供每秒能送出多少网页,常用来做网站性能压力测试。

网站性能压力测试是性能调优过程中必不可少的一环。只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题。

2、原理

ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

ab命令对发出负载的计算机要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也须注意,否则一次上太多的负载,可能造成目标服务器因资源耗完,严重时甚至导致死机。

3、安装

  • 方法一:安装apache时会自带安装ab工具;
  • 方法二:使用yum安装
yum install httpd-tools

4、参数说明

[root@localhost python]# 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                  (并发数,同一时间有多少强求发出去,默认是1)
    -t timelimit    Seconds to max. wait for responses                   (校验花费的最大时间,内部设置-n 50000 次。使用这个选项在特定时间内测试。默认不开启)
    -b windowsize   Size of TCP send/receive buffer, in bytes            (发送和接收的buffer大小,单位是 bytes)
    -p postfile     File containing data to POST. Remember also to set -T(包含POST数据文件)
    -u putfile      File containing data to PUT. Remember also to set -T (包含PUT数据文件)
    -T content-type Content-type header for POSTing, eg.
                    'application/x-www-form-urlencoded'
                    Default is 'text/plain'
    -v verbosity    How much troubleshooting info to print               (设置输出等级, 4 输出头信息,3 输出响应码(404,200) 2 输出警告和信息)
    -w              Print out results in HTML tables                     (输出结果到html里的table,默认两列,白色背景)
    -i              Use HEAD instead of GET                              (get请求中使用head信息)
    -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)           (使用用户cookie信息)
    -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.
    -g filename     Output collected data to gnuplot format file.
    				写所有有用的信息到TSV(Tab separate values)文件,可以轻松导入Excel等里面,label在文件第一行)
    -e filename     Output CSV file with percentages served
    				写一个逗号分隔的CSV文件,包含每个百分比(from 1% to 100%)服务器执行的时间(毫秒),这个文件一般比'gunplot'有用
    -r              Don't exit on socket receive errors.                 (在socket错误的时候不退出)
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)  (指定 SSL/TLS 协议 (SSL2, SSL3, TLS1, or ALL).)

5、基本使用实例

  • 并发10,请求100
    ab -n100 -c10 http://www.baidu.com
    

    备注:如果请求的url较长,可以用单引号把参数括起

  • 请求中带cookie
    ab -n100 -c10 -C 'BAIDUID=0538442B4178B209111B1E72875AFB93:FG=1; BIDUPSID=0538442B4178B209111B1E72875AFB93; PSTM=1462847291; BD_HOME=0; H_PS_PSSID=18880_1422_17758_18282_19805_19559_19808_19842_19902_19860_15939_12263; BD_UPN=12314753; BD_LAST_QID=10077090155228940401' 'http://www.baidu.com'
    

    备注:如果请求的cookie较长,可以用单引号把参数括起来

  • 结果以html的形式输出
    ab -n100 -c10 -w http://www.baidu.com
    

输出结果展示如下所以的html代码,把下列代码嵌入到一个html页面中,就可以在html页面中看到页面展示

<p>
 This is ApacheBench, Version 2.3 <i>&lt;$Revision: 655654 $&gt;</i><br>
 Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/<br>
 Licensed to The Apache Software Foundation, http://www.apache.org/<br>
</p>
<p>
..done


<table >
<tr ><th colspan=2 bgcolor=white>Server Software:</th><td colspan=2 bgcolor=white>nginx</td></tr>
<tr ><th colspan=2 bgcolor=white>Server Hostname:</th><td colspan=2 bgcolor=white>xxx.xx.com</td></tr>
<tr ><th colspan=2 bgcolor=white>Server Port:</th><td colspan=2 bgcolor=white>80</td></tr>
<tr ><th colspan=2 bgcolor=white>Document Path:</th><td colspan=2 bgcolor=white>/xx/xxx</td></tr>
<tr ><th colspan=2 bgcolor=white>Document Length:</th><td colspan=2 bgcolor=white>117946 bytes</td></tr>
<tr ><th colspan=2 bgcolor=white>Concurrency Level:</th><td colspan=2 bgcolor=white>10</td></tr>
<tr ><th colspan=2 bgcolor=white>Time taken for tests:</th><td colspan=2 bgcolor=white>3.984 seconds</td></tr>
<tr ><th colspan=2 bgcolor=white>Complete requests:</th><td colspan=2 bgcolor=white>100</td></tr>
<tr ><th colspan=2 bgcolor=white>Failed requests:</th><td colspan=2 bgcolor=white>1</td></tr>
<tr ><td colspan=4 bgcolor=white >   (Connect: 0, Length: 1, Exceptions: 0)</td></tr>
<tr ><th colspan=2 bgcolor=white>Total transferred:</th><td colspan=2 bgcolor=white>11873961 bytes</td></tr>
<tr ><th colspan=2 bgcolor=white>HTML transferred:</th><td colspan=2 bgcolor=white>11794561 bytes</td></tr>
<tr ><th colspan=2 bgcolor=white>Requests per second:</th><td colspan=2 bgcolor=white>25098.85</td></tr>
<tr ><th colspan=2 bgcolor=white>Transfer rate:</th><td colspan=2 bgcolor=white>2980227.88 kb/s received</td></tr>
<tr ><th bgcolor=white colspan=4>Connnection Times (ms)</th></tr>
<tr ><th bgcolor=white>&nbsp;</th> <th bgcolor=white>min</th>   <th bgcolor=white>avg</th>   <th bgcolor=white>max</th></tr>
<tr ><th bgcolor=white>Connect:</th><td bgcolor=white>    1</td><td bgcolor=white>    2</td><td bgcolor=white>   18</td></tr>
<tr ><th bgcolor=white>Processing:</th><td bgcolor=white>  111</td><td bgcolor=white>  373</td><td bgcolor=white>  501</td></tr>
<tr ><th bgcolor=white>Total:</th><td bgcolor=white>  112</td><td bgcolor=white>  375</td><td bgcolor=white>  519</td></tr>
</table>

html页面展示结果如下

<pre> This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ ..done Server Software: nginx Server Hostname: xxx.xx.com Server Port: 80 Document Path: /xx/xxx Document Length: 117946 bytes Concurrency Level: 10 Time taken for tests: 3.984 seconds Complete requests: 100 Failed requests: 1 (Connect: 0, Length: 1, Exceptions: 0) Total transferred: 11873961 bytes HTML transferred: 11794561 bytes Requests per second: 25098.85 Transfer rate: 2980227.88 kb/s received Connnection Times (ms) min avg max Connect: 1 2 18 Processing: 111 373 501 Total: 112 375 519 </pre>

  • 把所有请求中的相应时间记录下来并展示到csv文件(excel可以直接打开)中
    ab -n100 -c10 -e result.csv http://www.baidu.com
    

6、ab展示结果说明

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

Benchmarking xxx.xx.com (be patient).....done

Server Software:        nginx
Server Hostname:        xxx.xx.com
Server Port:            80

Document Path:          /xx/xxx
Document Length:        117946 bytes (测试页面大小)

Concurrency Level:      10 (并发数)
Time taken for tests:   0.590 seconds (全过程耗时)
Complete requests:      10 (完成请求数)
Failed requests:        0  (失败的请求数)
Write errors:           0
Total transferred:      1187400 bytes (整个过程中的网络传输量 )
HTML transferred:       1179460 bytes (整个过程中的HTML内容传输量 )
Requests per second:    16.96 [#/sec] (mean) (QPS/TPS)
Time per request:       589.700 [ms] (mean) (平均请求时间)
Time per request:       58.970 [ms] (mean, across all concurrent requests) (每个连接请求实际运行时间的平均值 )
Transfer rate:          1966.37 [Kbytes/sec] received (平均每秒网络上的流量)

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        3    5   1.0      5       6
Processing:   115  270 130.9    292     468
Waiting:      110  266 130.8    288     463
Total:        120  275 131.7    297     474

Percentage of the requests served within a certain time (ms)
  50%    297
  66%    328
  75%    389
  80%    445
  90%    474
  95%    474
  98%    474
  99%    474
 100%    474 (longest request)
表示小于某一时间的请求数在全过程中的占比

7、需要重点关注的指标

  • 完成请求数
  • 失败请求数 ==> 失败的不能太多
  • 并发数
  • 平均请求时间 ==> 一般同机房下不超过200ms
  • QPS ==> 一般用这个参数评估接口的性能,同并发下,QPS越大,性能越好
  • 90%的请求的最大响应时间 ==> 一般同机房下不超过200ms

转载于:https://my.oschina.net/rasine/blog/675196

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值