Apache Bench压测以及和Jmeter对比

Author: Rethink
Date: 2018/12/07
ApacheBench Version 2.3

Apache Bench(简称为ab)是 Apache 提供用于对Apache http server进行基准测试的工具。但是由于其安装和使用简单,所以也可以用于对HTTP接口的压力测试和性能测试。ab是一个命令行工具,使用ab命令可以模拟多线程并发请求,并且对负载机的要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,其原理类似DDOS/CC攻击。

ab可以提供需要的基本性能指标;但是缺点就是没有图形化结果,不能监控。

Tips:在带宽不足的情况下,最好是本机进行测试,建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)

安装

Apache本身会自带ab,如果没有安装Apache,以下方法可以用来便捷的安装ab工具:

  • Ubuntu

    apt-cache install apache2-util

  • CentOS

    yum install httpd-tools

  • MacOS
    系统自带apache,查看版本信息:
$ apachectl -v
Server version: Apache/2.4.33 (Unix)
Server built:   Apr  3 2018 17:54:07
$ ab -V
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
  • Windows

    Windows下可以在官网下载最新版本 ,然后按照引导安装即可。

image

使用以上方法安装完成后,在已添加环境变量的情况下,可以直接使用ab -V 检查是否安装成功。

>ab -V
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/</pre>

使用

Usage: ab [options] http://hostname[:port]/path

Options

 ab -h
Options are:
 -V              显示版本号并退出
 -n requests     总请求数,默认值为1
 -c concurrency  并发请求数,默认值为1,其值不能超过总请求数。建议是可以被总请求数整除,如 -n 100 –c 10模拟10个并发,每个并发10个请求。
 -t timelimit    测试所用的时间上限(单位为秒),它可以使对服务器的测试限制在一个固定的总时间以内,其内部隐含的默认值等于 -n 50000
 -s timeout      每次响应的最大等待时间(单位为秒),默认值为30s.注意s是小写的
 -p postfile     用于存放发起post请求时的数据的文件地址,不要忘记设置请求头。文件内容的格式要视请求头决定。
 -T content-type POST请求所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” ,默认值为“text/plain”
 -H attribute  添加头部Header,需要注意custome-header的格式为 key:value,eg. User-Agent: ApacheBench/2.3', 这也是ab的默认值User-Agent
 -C attribute  对请求附加Cookie,格式为 key=value ,注意和-H区分!
 -m method    设置请求方法
 -v verbosity  显示调试信息,verbosity为数字,可以看做是输出信息的级别。3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。
 -X proxy:port  设置代理地址以及端口信息
 -w             以HTML表的格式输出结果。
 -r          抛出异常继续执行测试任务 
 -i             使用HEAD请求代替GET

 -b windowsize   Size of TCP send/receive buffer, in bytes
 -B address      Address to bind to when making outgoing connections
 -u putfile      File containing data to PUT. Remember also to set -T
 -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
 -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.
 -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
 -h              Display usage information (this message)</pre>

Usage

> ab -n 10 -c 10 http://cnblogs.com/
​
# Get请求,要注意“&”是ab的保留运算符,使用时要用双引号引起来
> ab -n 10 -c 10 http://httpbin.org/get?name=rethink"&"age=3
​
# Post请求,post_data文件中的内容要和所指定的Content-Type对应(默认text/plain)
> ab -n 10 -c 10 -p E:\post_data1.txt -T application/x-www-form-urlencoded http://httpbin.org/post
> ab -n 10 -c 10 -p E:\post_data2.txt -T application/json http://httpbin.org/post
​
# post_data1中的数据为:name=rethink&age=3&method=post
# post_data1中的数据为:{"name":"Rethink","method":"post","age":3}
​
# 使用`-w`参数将结果以HTML的格式输出,然后将结果写入到本地文件中:
>  ab -n 2 -c 2 -v 2 -w  http://httpbin.org/get?name=rethink"&"age=3 > E:\report.html

注意事项:

  1. result.html中会打印每次请求的请求头信息,请求总数较大时,重定向结果输出时可以不指定-v参数;

  2. 使用-H Content-Type:application/json不能代替-T "application/json", 使用前者服务器可能会返回400 bad requests;

  3. 如果提示ab: invalid URL,可能是URL最右边缺少/,例如 http://www.example.com 需要改为*http://www.example.com/* ;

  4. 不支持发送https请求;

  5. postfile注意使用正确的编码格式,否则请求参数服务器端可能无法识别;

  6. 调试请求时,对接口返回的中文字符的支持不友好,会显示乱码;


结果分析

从上面可以看到ab支持参数很多,但一般来说只有-c-n 参数是必要的,例如:

>PS > ab -n 10 -c 10  http://httpbin.org/get?name=rethink"&"age=3
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 cnblogs.com (be patient).....done
​
Server Software:  gunicorn/19.9.0 服务器类型
Server Hostname:        httpbin.org  域名
Server Port:            80  web端口
​
Document Path:          /get?name=rethink&age=3  测试的页面路径
Document Length:        280 bytes  测试的页面大小
​
Concurrency Level:      10  测试的并发数
Time taken for tests:   2.629 seconds  整个测试活动持续的时间
Complete requests:      10  完成的请求数量(只包含状态码==200的请求)
Failed requests:        0  失败的请求数量(包含状态码!=200的请求和超时的请求)
Non-2xx responses:      10     状态码不等于2xx的响应数
Total transferred:      5210 bytes  整个过程中的网络传输量
HTML transferred:       2800 bytes  整个过程中的HTML内容传输量
Requests per second:    3.80 [/sec] (mean)  每秒的响应请求数(QPS),数值等于 请求总数(-n)/Time taken for tests,后面括号中的mean表示这是一个平均值
Time per request:       2629.486 [ms] (mean)    用户平均请求等待时间=concurrency * timetaken * 1000 / done
Time per request:       12.893 [ms] (mean, across all concurrent requests)  每个连接请求实际运行时间的平均值
Transfer rate:          25.15 [Kbytes/sec] received  传输速率
​
Connection Times (ms)
 min  mean[+/-sd] median   max
Connect:      210  216   6.1    216     229
Processing:   229 1022 658.7   1081    2181
Waiting:      229  827 558.2    866    1731
Total:        445 1238 659.4   1291    2399
​
Percentage of the requests served within a certain time (ms)
 50%   1291  整体响应时间的分布比 
 66%   1507
 75%   1731
 80%   1949
 90%   2399  表示90%的请求在1291ms内得到响应
 95%   2399
 98%   2399
 99%   2399
 100%   2399 (longest request)

AB 和 JMETER 比较

下面分别用ab和jmeter压测一个Get请求,然后通过对比效果,来简单分析二者的异同。

  • ab

在ab命令行中设置10个并发,持续60s,结果如下:

PS> ab -c 10 -t 60  http://httpbin.org/get?name=Rethink"&"date=20181207"&"method=get
​
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 httpbin.org (be patient)
Finished 1386 requests
​
​
Server Software:        gunicorn/19.9.0
Server Hostname:        httpbin.org
Server Port:            80
​
Document Path:          /get?name=Rethink&date=20181207&method=get
Document Length:        330 bytes
​
Concurrency Level:      10
Time taken for tests:   60.050 seconds
Complete requests:      1332
Failed requests:        0
Total transferred:      760572 bytes
HTML transferred:       439560 bytes
Requests per second:    22.18 [#/sec] (mean)
Time per request:       450.828 [ms] (mean)
Time per request:       45.083 [ms] (mean, across all concurrent requests)
Transfer rate:          12.37 [Kbytes/sec] received
​
Connection Times (ms)
 min  mean[+/-sd] median   max
Connect:      189  219 139.5    200    2245
Processing:   196  227  89.5    212    1573
Waiting:      196  226  89.1    212    1573
Total:        387  446 174.6    414    2551
​
Percentage of the requests served within a certain time (ms)
 50%    414
 66%    422
 75%    427
 80%    431
 90%    458
 95%    553
 98%   1045
 99%   1476
 100%   2551 (longest request)</pre>
  • Jmeter

在JMETER GUI界面中编辑脚本信息,线程数设置10,R-U时间设置1s,循环次数设置Forever。如下图:image

然后使用 JMETER 命令行模式运行脚本,这是因为使用非GUI模式可以节省负载机资源,测试结果更加准切。

$ jmeter -n -t ~/JmeterWar.jmx -l JmeterWar.csv
Creating summariser <summary>
Created the tree successfully using /Users/rethink/JmeterWar.jmx
Starting the test @ Sun Dec 09 16:56:02 CST 2018 (1544345762429)
Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4448
summary +      1 in 00:00:01 =    1.6/s Avg:   504 Min:   504 Max:   504 Err:     0 (0.00%) Active: 7 Started: 7 Finished: 0
summary +    621 in 00:00:27 =   23.2/s Avg:   427 Min:   389 Max:  1465 Err:     0 (0.00%) Active: 10 Started: 10 Finished: 0
summary =    622 in 00:00:27 =   22.7/s Avg:   427 Min:   389 Max:  1465 Err:     0 (0.00%)
summary +    711 in 00:00:30 =   23.7/s Avg:   421 Min:   389 Max:  1470 Err:     0 (0.00%) Active: 10 Started: 10 Finished: 0
summary =   1333 in 00:00:57 =   23.2/s Avg:   424 Min:   389 Max:  1470 Err:     0 (0.00%)
^Z
[4]+  Stopped                 jmeter -n -t ~/JmeterWar.jmx -l JmeterWar.csv

在GUI界面中打开JmeterWar.csv文件,如下:

image

  • 二者对比
ab Jmeter
发送的请求总数 1386 1350
Average 414ms 424ms
Min 387ms 389ms
Median 414ms 413ms
Max 2551ms 1470ms
90% Line 458ms 434ms
95% Line 553ms 452ms
99% Line 1476ms 859ms
Error % 0.00 0.00
QPS 22.18/sec 23.4/sec

通过以上表格数据进行分析,可以发现二者的测试效果竟然是如此的接近,但实际上述数据并不是太准确,因为Jmeter实际不支持精确的测试时间的限制,所以我是到达60s后手动强制杀死进程,这就导致有些请求可能被漏掉,但通过比较聚合数据,基本上误差是在可以接受的范围内。


个人总结

1、jmeter是一次完整的请求和返回, 而AB只是发出去请求,并不对返回做处理,只是请求发送成功或者失败。 所以从准确性来说,Jmeter更准确,而AB速度更快,可以用最少的机器资源产生更多的访问请求;

2、Jmeter本身支持断言、可变参数和CSV数据集的输入,能设定更加灵活多变的的测试场景,而AB则不支持(暂时没想到);

3、Jmeter可以提供更加详细的统计结果数据,比如接口错误信息、单线程的请求时间等,而AB则不支持;

4、Jmeter不支持精确时间的压测,比如压测10分钟,但是AB支持;

5、Jmeter支持分布式的压测集群,且支持函数,AB不支持;

6、软件自身耗费资源:Jmeter由于比较重,且统计了很多结果数据,比AB耗时耗费资源多,B属于超轻量级,在开发测试过程中十分适合做单接口压测。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章