博主博客

Apache Bench

ApacheBench 是 Apache 服务器自带的一个web压力测试工具,简称ab。ab又是一个命令行工具,对发起负载的本机要求很低,根据ab命令可以创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访问,因此可以用来测试目标服务器的负载压力。总的来说ab工具小巧简单,上手学习较快,可以提供需要的基本性能指标,但是没有图形化结果,不能监控。

点击这里下载

解压后在httpd-2.4.39-win64-VC15\Apache24\bin文件夹下可以看到ab.exe
即可以通过设置环境变量,也可以通过命令行直接进入该文件夹,调用ab命令。

Usage: ab [options] [http://]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)

例如,我们测试 http://192.168.1.106:8080/ 可以用以下命令。

C:\Users\nukix\Desktop\httpd-2.4.39-win64-VC15\Apache24\bin>ab -c 5000 -n 5000 http://192.168.1.106:8080/
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 192.168.1.106 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests


Server Software:
Server Hostname:        192.168.1.106
Server Port:            8080

# 测试界面
Document Path:          /
# 界面大小
Document Length:        11184 bytes

# 测试的并发数
Concurrency Level:      5000
# 整个测试持续的时间
Time taken for tests:   45.928 seconds
# 完成的请求数量
Complete requests:      5000
# 失败的请求数量
Failed requests:        0
# 整个过程中的网络船数量
Total transferred:      56480000 bytes
# 整个过程中的HTML内容传输量
HTML transferred:       55920000 bytes
# 最重要的指标之一,相当于LR中的每秒事务数,后面括号中的mean表示这是一个平均值
Requests per second:    108.87 [#/sec] (mean)
# 最重要的指标之二,相当于LR中的平均事务响应时间,后面括号中的mean表示这是一个平均值
Time per request:       45927.886 [ms] (mean)
# 每个连接请求实际运行时间的平均值
Time per request:       9.186 [ms] (mean, across all concurrent requests)
# 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
Transfer rate:          1200.93 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    9 158.6      1    3002
Processing:   807 26109 12324.1  23463   45655
Waiting:       13 25646 12302.9  23003   45156
Total:        809 26118 12322.0  23464   45656

Percentage of the requests served within a certain time (ms)
  50%  23464
  66%  35899
  75%  39120
  80%  39244
  90%  39444
  95%  42546
  98%  45604
  99%  45626
 100%  45656 (longest request)

参考资料:https://www.cnblogs.com/Ryana/p/6279232.html