标签:href alt percent cross ges min 印象 ons too
(题图: 某偏远山沟小河 effected by google photos)ab [ -A auth-use![](https://s4.51cto.com/images/blog/202008/03/9d463589bf180e24fbfe2fc0cfe3e58d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)rname: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 跟并发数.
-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 一致.
然后进行完整的测试, 可以得到详细的结果报告.
# 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 requests
Server Software: Tengine/2.1.1
Server Hostname: shangtongdai.yxapp.xyz
Server Port: 80
Document Path: /loans
Document Length: 53218 bytes
Concurrency 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] received
Connection 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 2969
Percentage 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)
在某个场景下, 我需要对其中一个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 正常了).
下面来重现一下这个过程, 拿百度为例吧.
? 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百度无效的请求).
? 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文件末尾的结束符后, 得以成功.
标签:href alt percent cross ges min 印象 ons too
原文地址:https://blog.51cto.com/14890678/2516469