Apache ab 接口测试

一. Apache ab

1 . 介绍

Apache Benchmark(简称ab) 是Apache安装包中自带的压力测试工具 ,简单易用。 使用起来非常的简单和方便。 不仅仅是可以apache服务器进行网站访问压力测试,还可以对其他类型的服务器进行压力测试。 比如nginx,tomcat,IIS等

2 . 安装

- windows :参考 https://blog.csdn.net/foreverling_ling/article/details/81667857
- ubuntu安装ab :   sudo apt-get install apache2-utils
- centOS7 安装ab  :  yum -y install httpd-tools
- centos6.5 默认已安装了ab

3. 参数

您可以使用 ab -h 来查看基准参数。

  • -n 即requests,用于指定压力测试总共的执行次数。
  • -c 即concurrency,用于指定的并发数。
  • -t 即timelimit,等待响应的最大时间(单位:秒)。
  • -b 即windowsize,TCP发送/接收的缓冲大小(单位:字节)。
  • -p 即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。
  • -u 即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。
  • -T 即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。
  • -v 即verbosity,指定打印帮助信息的冗余级别。
  • -w 以HTML表格形式打印结果。
  • -i 使用HEAD请求代替GET请求。
  • -x 插入字符串作为table标签的属性。
  • -y 插入字符串作为tr标签的属性。
  • -z 插入字符串作为td标签的属性。
  • -C 添加cookie信息,例如:"Apache=1234"(可以重复该参数选项以添加多个)。
  • -H 添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
  • -A 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
  • -P 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。
  • -X 指定使用的代理服务器和端口号,例如:"126.10.10.3:88"。
  • -V 打印版本号并退出。
  • -k 使用HTTP的KeepAlive特性。
  • -d 不显示百分比。
  • -S 不显示预估和警告信息。
  • -g 输出结果信息到gnuplot格式的文件中。
  • -e 输出结果信息到CSV格式的文件中。
  • -r 指定接收到错误信息时不退出程序。
  • -h 显示用法信息,其实就是ab -help。

 

​4. 测试get请求

ab -n 1000 -c 10 

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, Welcome to The Apache Software Foundation!

Benchmarking www.baidu.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:     
##请求的地址/域名   
Server Hostname:        www.baidu.com      
##端口            
Server Port:            80
##请求的路径
Document Path:          /s
##页面数据/返回的数据量
Document Length:        262 bytes

##并发数
Concurrency Level:      10
##共使用了多少时间 
Time taken for tests:   1.059 seconds
##完成请求数
Complete requests:      1000
##失败请求数
Failed requests:        11
   (Connect: 0, Receive: 0, Length: 11, Exceptions: 0)
Write errors:           0
Non-2xx responses:      989
##总共传输字节数,包含http的头信息等 
Total transferred:      4233614 bytes
##html字节数,实际的页面传递字节数 
HTML transferred:       3873262 bytes
##每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量 
Requests per second:    944.02 [#/sec] (mean)
##用户平均请求等待时间 
Time per request:       10.593 [ms] (mean)
##每个连接请求实际运行时间的平均值
Time per request:       1.059 [ms] (mean, across all concurrent requests)
##每秒获取的数据长度
Transfer rate:          3902.94 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
##连接的   最小时间,平均值,中值,最大值
Connect:        3    3   0.3      3       7
##处理时间
Processing:     5    7  10.4      6     155
##等待时间
Waiting:        5    7  10.2      5     155
##合计时间
Total:          7   10  10.4      9     158

Percentage of the requests served within a certain time (ms)
  50%      9   ## 50%的请求在9ms内返回 
  66%      9
  75%      9
  80%     10
  90%     11
  95%     16
  98%     34
  99%     49
 100%    158 (longest request)

5. 测试post请求

ab -n 100 -c 10 -p ./xx.txt -T application/x-www-form-urlencoded "172.22.1.120:8094/mapper/bill/msgid_left"

 

-T 指定请求体:content-type,

-p指定参数文件, post参数写在xx.txt 文件中,如page=1&pagesize=10

注意文件不要用Windows自带的文本编辑器编辑。

[service@sparkclient-01 ~]$ cat xx.txt

appId=101&status=1

如果post json数据, 要改变参数 -T application/json,-p为 json格式参数文件。如appid=101&status=1&ms={“tobank”:“123456”}

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.22.1.120 (be patient).....done


Server Software:        
Server Hostname:        172.22.1.120
Server Port:            8094

Document Path:          /mapper/bill/msgid_left
Document Length:        54 bytes

Concurrency Level:      10
Time taken for tests:   0.084 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      15900 bytes
Total POSTed:           18700
HTML transferred:       5400 bytes
Requests per second:    1193.33 [#/sec] (mean)
Time per request:       8.380 [ms] (mean)
Time per request:       0.838 [ms] (mean, across all concurrent requests)
Transfer rate:          185.29 [Kbytes/sec] received
                        217.92 kb/s sent
                        403.22 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     3    8   6.2      5      28
Waiting:        2    7   5.6      4      25
Total:          3    8   6.3      5      28

Percentage of the requests served within a certain time (ms)
  50%      5
  66%      6
  75%      7
  80%     12
  90%     19
  95%     24
  98%     25
  99%     28
 100%     28 (longest request)


测试结论:(从测试结果中得出)
Concurrency Level: 10                                                                 并发数-c
Complete requests: 1000                                                         请求数-n
Time taken for tests: 1.059 seconds                                          总耗时
Time per request: 1.059 [ms] (mean, across all concurrent requests) 平均每个请求耗时
**Requests per second: ** 944.02 [#/sec] (mean)                                 qtp

1000个请求,10个并发,总耗时1.059秒,平均每个请求耗时1.059ms,qtp 944.02 性能良好 或一般





 

参考地址

https://blog.csdn.net/foreverling_ling/article/details/81667857

https://blog.csdn.net/bob_baobao/article/details/83544275

 

 

三. 对比:

jmeter是一次完整的请求和返回;AB只是发出去请求,并不对返回做处理

jmeter可以提供更加详细的统计结果数据;ab的结果为用数学方式统计平均值

jmeter支持可变参数和CSV数据集的输入,能设定更加负责的测试样例;AB不需要写配置文件

jmeter不支持精确时间的压测;AB支持

jmeter为GUI的较重,并且统计了很多结果数据,比AB耗时耗费资源多;AB属于轻量级

posted on   FanF  阅读(205)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示