开机即用的便捷性能测试工具-ab

ab简介

ab是Apache Web服务器自带的性能测试工具,对于web开发者基本上就是开机即用(安装有Apache服务就会带有ab),
它能模拟并发请求,对web服务器进行压力性能测试。平时可用快速的拿来做性能评估,或者对接口进行模拟并发测试。

安装

一般情况下,安装了Apache服务,就会带有ab工具,无需特别安装。
如果没有,

  1. 在Ubuntu下:

    sudo apt install apache2-utils
  2. Windows下,直接下载Apache程序包安装即可,将apache2.4.41\bin路径加入环境变量path中。

参数说明

常用参数
-n:总请求次数(最小默认为1)
-c:并发次数(最小默认为1且不能大于总请求次数,如:10个请求,10个并发,实际就是1人请求1次)
-p:post参数文档路径(-p和-T参数要配合使用)
-T:header头内容类型(此处切记是大写英文字母T)
-C:对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。
-H:对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")

可用ab -h 查看详细参数,如下:

xiaojun1024@test-pc:~$ ab -h
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
                    (SSL2, TLS1, TLS1.1, TLS1.2 or ALL)
    -E certfile     Specify optional client certificate chain and private key

实例测试

对给定的url进行测试:

  1. GET请求:

    #请求URL要使用双引号括起来,否则ab无法识别&符号
    xiaojun1024@test-pc:~$ ab -n 100 -c 10 "http://www.test.com/index.php?aa=1&bb=2"
  2. POST请求:

    #注意:-p是参数文档路径,-T是大写英文字母,
    #post表单格式为:application/x-www-form-urlencoded
    xiaojun1024@test-pc:~$ ab -n 100 -c 10 -p ./query_params.txt -T application/x-www-form-urlencoded "http://www.test.com/index.php"

    ./query_params.txt 文件内容

username=xiao&type=1

结果分析

主要看三个数据

  1. Requests per second:吞吐率
    服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
    计算公式:总请求数 / 处理完成这些请求数所花费的时间,即
    Request per second = Complete requests / Time taken for tests

  2. Time per request:上面的是用户平均请求等待时间

处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level)

  1. Time per request:下面的是服务器平均请求处理时间

处理完成所有请求数所花费的时间 / 总请求数,即
Time taken for / testsComplete requests
可以看到,它是吞吐率的倒数。
同时,它也=用户平均请求等待时间/并发用户数,即
Time per request / Concurrency Level

详细如下:

xiaojun1024@test-pc:~$ ab -n 10 -c 10 "http://www.test.com/index.php?aa=1&bb=2"
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.xiaojun1024.com (be patient).....done

Server Software:        nginx/1.16.1
Server Hostname:        www.test.com.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Server Temp Key:        ECDH P-256 256 bits
TLS Server Name:        www.xiaojun1024.com

Document Path:          /
Document Length:        29542 bytes

Concurrency Level:      5
Time taken for tests:   0.923 seconds
Complete requests:      20
Failed requests:        0
Total transferred:      595540 bytes
HTML transferred:       590840 bytes
Requests per second:    21.66 [#/sec] (mean)
Time per request:       230.791 [ms] (mean)
Time per request:       46.158 [ms] (mean, across all concurrent requests)
Transfer rate:          629.99 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       41   61  22.7     50     113
Processing:    68  125  37.6    123     220
Waiting:       68  118  37.5    114     217
Total:        120  186  33.6    186     274

Percentage of the requests served within a certain time (ms)
  50%    186
  66%    201
  75%    209
  80%    209
  90%    231
  95%    274
  98%    274
  99%    274
 100%    274 (longest request)

附:

ab工具详细介绍:apache官网
其他压力测试工具:http_load

发表回复