前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >性能测试工具 - ab

性能测试工具 - ab

作者头像
程序猿石头
发布2020-07-14 16:02:36
2.2K0
发布2020-07-14 16:02:36
举报
文章被收录于专栏:程序猿石头程序猿石头

之前知道一般网站性能可以通过 LoadRunner, JMeter, QTP 等相应的软件进行测试, 印象中本科学习 “软件测试” 这门课程时安装并使用过, LoadRunner等不是一个小软件, 安装不是那么的容易.

最近发现Apache还有一款小巧玲珑的工具可以直接用来做压力测试, 相关文档可以参见 Apache ab 官网.

Mac 下自带(具体记不清是因为我安装了Apache还是系统自带的了)了这个 ab 工具(Apache HTTP server benchmarking tool), ab 我猜应该就是 Apache Benchmarking 的缩写吧?

ab 用法

ab 命令参数如下,

代码语言:javascript
复制
ab  [ -A auth-username:password ] [ -b windowsize ] [ -B local-address ] [ -c concurrency ] [ -C cookie-name=value
] [ -d ] [ -e csv-file ] [ -f protocol ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ -l ] [ -m
HTTP-method  ] [ -n requests ] [ -p POST-file ] [ -P proxy-auth-username:password ] [ -q ] [ -r ] [ -s timeout ] [
-S ] [ -t timelimit ] [ -T content-type ] [ -u PUT-file ] [ -v verbosity] [ -V ] [ -w ] [ -x <table>-attributes  ]
[  -X  proxy[:port]  ]  [  -y  <tr>-attributes  ]  [  -z  <td>-attributes  ]  [ -Z ciphersuite ] [http[s]://]host-
name[:port]/path

不过常用的, 也就是, -n 跟请求数, -c 跟并发数.

代码语言:javascript
复制
-n requests     Number of requests to perform
-c concurrency  Number of multiple requests to make at a time

在对网站进行测试的时候, 可能需要登录态进行测试, 可以通过 -C 加 Cookie的方式进行测试, 测试之前, 最好确认这个命令用法是否正确, 只用1个请求看看响应的长度是否一致(可以通过 与 curl 命令的结果进行对比).

例如, 通过 ab -c 1 -n 1 -C 'cookiedata=xxx' "http://shangtongdai.yxapp.xyz/loans" 得到的 Document Length: 53218 bytes 和用 curl -b 'cookiedata=xxx' "http://shangtongdai.yxapp.xyz/loans" 得到的Content-Length: 53218 一致.

然后进行完整的测试, 可以得到详细的结果报告.

代码语言:javascript
复制
# 200并发,一共10000请求ab -c 200 -n 10000 -C 'cookiedata=xxx' "http://shangtongdai.yxapp.xyz/loans"# 结果
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking shangtongdai.yxapp.xyz (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 requestsServer Software:        Tengine/2.1.1
Server Hostname:        shangtongdai.yxapp.xyz
Server Port:            80Document Path:          /loans
Document Length:        53218 bytesConcurrency Level:      200
Time taken for tests:   53.098 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      534570000 bytes
HTML transferred:       532180000 bytes
Requests per second:    188.33 [#/sec] (mean)
Time per request:       1061.967 [ms] (mean)
Time per request:       5.310 [ms] (mean, across all concurrent requests)
Transfer rate:          9831.59 [Kbytes/sec] receivedConnection Times (ms)
             min  mean[+/-sd] median   max
Connect:        0    1   1.8      1      39
Processing:    38 1055 344.1   1046    2964
Waiting:       37 1051 345.5   1043    2959
Total:         39 1056 344.4   1047    2969Percentage of the requests served within a certain time (ms)
 50%   1047
 66%   1170
 75%   1252
 80%   1311
 90%   1477
 95%   1657
 98%   1860
 99%   1986
100%   2969 (longest request)

ab post “bug”

在某个场景下, 我需要对其中一个post的接口进行测试. 根据 ab 的 mannual 看到 post 时候, 需要将post的data用文件保存, 然后通过参数 -p postdata.file 传输.

但在实际ab进行测试时, 发现返回的结果异常, 正常情况下 response 的size比通过ab返回的response size大得多, 说明通过ab发送的http请求失败了.

经过tcpdump抓包最后发现 ab 请求无效的原因是: postdata 文件会多一个字符(文件末尾的换行符), 导致server端的 form 解析失败, 因而返回异常的response.

这个坑是vim的默认配置导致的, vim默认会在文件末尾添加一个文件结束符, vim 默认配置 'endofline' 'eol' boolean (default on) , 可以通过 set noendofline 解决.

实际过程中,(去掉文件末尾的换行符可以解决), 或者将postdata多添加一个参数可以解决(这个参数server端没有用到时多余的, form可以正常解析, 因此 response 正常了).

下面来重现一下这个过程, 拿百度为例吧.

代码语言:javascript
复制
➜  com~apple~CloudDocs$ cat postdata.txt
a=65&b=66

用 curl 执行, curl -i -H "Content-Type: application/x-www-form-urlencoded" -d "a=65&b=66" "http://www.baidu.com".

用 ab 执行, ab -c 1 -n 1 -T 'application/x-www-form-urlencoded;' -p postdata.txt 'http://www.baidu.com/'

抓包得到以下结果

用 curl 执行并抓包的结果是:

发现HTTP协议版本号不同, UA不同, Content-Length不同. 刚开始还以为是ab的bug, 最后发现确实是 Content-Length 相差1, 而多的这个字符换行符导致了 server 段的 form 填充失败(上例中体现不了, 反正post百度无效的请求).

代码语言:javascript
复制
➜  com~apple~CloudDocs$ wc -c postdata.txt
     10 postdata.txt
➜  com~apple~CloudDocs$ ll postdata.txt
-rw-r--r--@ 1 tanglei  staff    10B  4 11 21:26 postdata.txt
➜  com~apple~CloudDocs$ cat postdata1.txt
a=65&b=66%                                                                                                                             ➜  com~apple~CloudDocs$ wc -c postdata1.txt
      9 postdata1.txt
➜  com~apple~CloudDocs$ ll postdata1.txt
-rw-r--r--@ 1 tanglei  staff     9B  4 11 21:26 postdata1.txt

最后去掉postdata文件末尾的结束符后, 得以成功.

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿石头 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ab 用法
  • ab post “bug”
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档