0x1 ab

  • ab是一个对Apache超文本传输协议(HTTP)服务器进行基准测试的工具
  • 它旨在让您了解当前Apache安装的执行情况
  • 主要向您展示了Apache安装能够处理的每秒请求数
  • ab官方文档
  • 安装指令如下

    apt install -y apache2-utils

0x2 核心参数

  • 这是从ab用户手册中整理出来的命令参数

    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

    核心参数如下:

    1. -n:一共进行多少次请求测试
    2. -c:并发数,每次并发多少个请求
    3. -t:整个测试持续的最大时间,单位是秒,默认50000s
    4. -s:发出请求后等待回应的时间,单位是秒,默认30s
    5. -k:请求并且使用KeepAlive
    6. -C:指定携带的cookie,例如-C "Apache=1234"
    7. -H:自定义header,例如-H "Accept-Encoding: gzip"

0x3 测试压力

  1. 测试10000次请求,并发100

    ab -n 10000 -c 100  http://10.25.153.231:80/index.html
  2. ab执行后会得到如下的输出数据

    root@docker:~# ab -n 10000 -c 100  http://10.25.153.231:80/index.html
    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 10.25.153.231 (be patient)
    Completed 1000 requests
    Completed 2000 requests
    Completed 3000 requests
    Completed 4000 requests
    Completed 5000 requests
    Completed 6000 requests
    Completed 7000 requests
    Completed 8000 requests
    Completed 9000 requests
    Completed 10000 requests
    Finished 10000 requests
    
    
    Server Software:        Apache/2.2.14
    Server Hostname:        10.25.153.231
    Server Port:            80
    
    Document Path:          /index.html
    Document Length:        207 bytes
    
    Concurrency Level:      100
    Time taken for tests:   4.078 seconds
    Complete requests:      10000
    Failed requests:        0
    Non-2xx responses:      10000
    Total transferred:      5960000 bytes
    HTML transferred:       2070000 bytes
    Requests per second:    2451.90 [#/sec] (mean)
    Time per request:       40.785 [ms] (mean)
    Time per request:       0.408 [ms] (mean, across all concurrent requests)
    Transfer rate:          1427.08 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        1    5   1.7      6      15
    Processing:     4   35   8.6     36      64
    Waiting:        2   35   8.5     36      64
    Total:          9   41   8.3     42      70
    
    Percentage of the requests served within a certain time (ms)
      50%     42
      66%     44
      75%     46
      80%     47
      90%     51
      95%     54
      98%     59
      99%     63
     100%     70 (longest request)
    root@docker:~#

0x4 结果解析

  1. ab的报告中一开始先输出目标服务器的相关信息,以及此次的数据量统计

    Server Software:        Apache/2.2.14
    Server Hostname:        10.25.153.231
    Server Port:            80
    
    Document Path:          /index.html
    Document Length:        207 bytes
    
    Concurrency Level:      100
    Time taken for tests:   4.078 seconds
    Complete requests:      10000
    Failed requests:        0
    Non-2xx responses:      10000
    Total transferred:      5960000 bytes
    HTML transferred:       2070000 bytes
    Requests per second:    2451.90 [#/sec] (mean)
    Time per request:       40.785 [ms] (mean)
    Time per request:       0.408 [ms] (mean, across all concurrent requests)
    Transfer rate:          1427.08 [Kbytes/sec] received
  2. 接着罗列了测试中的时间分布

    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        1    5   1.7      6      15
    Processing:     4   35   8.6     36      64
    Waiting:        2   35   8.5     36      64
    Total:          9   41   8.3     42      70
    1. Connect:建立连接的时间分布
    2. Processing:处理数据的时间分布
    3. Waiting:等待响应的时间分布
    4. Total:总的时间
    5. mean:平均数
    6. median:中位数
    7. min/max:最小值,最大值
    8. Total解析:最短的请求只花了9ms,平均耗时41ms,中位数耗时42ms,最长使用70ms
  3. 最后罗列了完整请求的时间占比

    Percentage of the requests served within a certain time (ms)
      50%     42
      66%     44
      75%     46
      80%     47
      90%     51
      95%     54
      98%     59
      99%     63
     100%     70 (longest request)
    1. 50%的请求在42ms在完成
    2. 80%的请求在47ms内完成
    3. 100%的请求在70ms内完成

0x5 自定义测试

  • 之前的测试仅仅是简单的测试的服务器的index性能
  • 接下来使用自定义header以及cookie等功能来测试
  • 这里使用-H以及-C分别指定header以及cookie,同时打开keeplive

    ab -n 10000 \
    -c 100 -k \
    -H "Accept-Encoding: gzip, deflate" \
    http://10.25.153.231:80/index.html
  • 测试报告如下

    root@docker:~# ab -n 10000 -c 100 -k -H "Accept-Encoding: gzip, deflate" http://10.25.153.231:80/index.html
    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 10.25.153.231 (be patient)
    Completed 1000 requests
    Completed 2000 requests
    Completed 3000 requests
    Completed 4000 requests
    Completed 5000 requests
    Completed 6000 requests
    Completed 7000 requests
    Completed 8000 requests
    Completed 9000 requests
    Completed 10000 requests
    Finished 10000 requests
    
    
    Server Software:        Apache/2.2.14
    Server Hostname:        10.25.153.231
    Server Port:            80
    
    Document Path:          /index.html
    Document Length:        4785 bytes
    
    Concurrency Level:      100
    Time taken for tests:   6.745 seconds
    Complete requests:      10000
    Failed requests:        0
    Keep-Alive requests:    9910
    Total transferred:      53106040 bytes
    HTML transferred:       47850000 bytes
    Requests per second:    1482.53 [#/sec] (mean)
    Time per request:       67.452 [ms] (mean)
    Time per request:       0.675 [ms] (mean, across all concurrent requests)
    Transfer rate:          7688.60 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.7      0       8
    Processing:     1   53 384.2      5    4255
    Waiting:        1   53 384.1      5    4254
    Total:          1   53 384.5      5    4255
    
    Percentage of the requests served within a certain time (ms)
      50%      5
      66%      5
      75%      6
      80%      6
      90%      7
      95%      8
      98%     13
      99%   2999
     100%   4255 (longest request)
  • 解读报告可以发现:请求时间有略微的增加,稳定性也差了很多
Last modification:April 22, 2022
如果觉得我的文章对你有用,请随意赞赏