Skip to content

Apache Bench 压测工具

Ab(ApacheBench) 测试工具是 Apache 提供的一款测试工具,具有简单易上手的特点,在测试 Web 服务时非常实用。ab 一般都是在 Linux 上用。

安装非常简单,只需要在 Linux 系统中输入 yum -y install httpd-tools 命令,就可以了。安装成功后,输入 ab 命令,可以看到以下信息:

bash
[root@aa81067c5a5b /]# 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
    -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 工具用来测试 post/get 接口请求非常便捷,可以通过参数指定请求数、并发数、请求参数等

测试 get 请求接口

bash
[root@aa81067c5a5b /]# ab -c 10 -n 100 http://localhost:8081/goods?user=aaa

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 localhost (be patient).....done

Server Software:
Server Hostname:        localhost
Server Port:            8081

Document Path:          /goods
Document Length:        2784 bytes

Concurrency Level:      10
Time taken for tests:   1.883 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      297800 bytes
HTML transferred:       278400 bytes
Requests per second:    53.10 [#/sec] (mean)                                # 吞吐率,指某个并发用户数下单位时间内处理的请求数;
Time per request:       188.307 [ms] (mean)                                 # 用户平均请求等待时间,指处理完成所有请求数所花费的时间 /(总请求数 / 并发用户数);
Time per request:       18.831 [ms] (mean, across all concurrent requests)  # 服务器平均请求处理时间,指处理完成所有请求数所花费的时间 / 总请求数;
Transfer rate:          154.44 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.8      0      23
Processing:    35  174  66.7    167     406
Waiting:       30  163  63.7    150     332
Total:         35  175  66.8    170     406

# 下面:每秒请求时间分布情况,指在整个请求中,每个请求的时间长度的分布情况,例如有 50% 的请求响应在 8ms 内,66% 的请求响应在 10ms 内,说明有 16% 的请求在 8ms~10ms 之间。
Percentage of the requests served within a certain time (ms)
  50%    170
  66%    201
  75%    230
  80%    233
  90%    266
  95%    291
  98%    329
  99%    406
 100%    406 (longest request)

测试 post 请求接口

bash
[root@aa81067c5a5b /]# ab -c 10 -n 100 -p 'post.txt' -T 'application/x-www-form-urlencoded' http://localhost:8081/goods

#post.txt 为存放 post 参数的文档,存储格式如: usernanme=test&password=test&sex=1

参数的含义:

  • -n:总请求次数(最小默认为 1);
  • -c:并发次数(最小默认为 1 且不能大于总请求次数,例如:10 个请求,10 个并发,实际就是 1 人请求 1 次);
  • -p:post 参数文档路径(-p 和 -T 参数要配合使用);
  • -T:header 头内容类型(此处切记是大写英文字母 T);

性能指标参考

  • Requests per second:吞吐率,指某个并发用户数下单位时间内处理的请求数;
  • Time per request:上面的是用户平均请求等待时间,指处理完成所有请求数所花费的时间 /(总请求数 / 并发用户数);
  • Time per request:下面的是服务器平均请求处理时间,指处理完成所有请求数所花费的时间 / 总请求数;
  • Percentage of the requests served within a certain time:每秒请求时间分布情况,指在整个请求中,每个请求的时间长度的分布情况,例如有 50% 的请求响应在 8ms 内,66% 的请求响应在 10ms 内,说明有 16% 的请求在 8ms~10ms 之间。