ab 命令 Failed requests 的 Length 问题

荆轲刺秦王

本地虚拟机 CentOS 7 上是 nginx ,需要先安装一下

centos安装ab
yum install httpd-tools

我直接在我用户目录下安装,使用起来 是可以全局使用的:

[root@localhost ~]# which ab
/usr/bin/ab

查看下是否安装成功,直接查看 version 

[root@localhost ~]# ab -V
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

先简单看一下常用参数:

-n:总请求次数(最小默认为1)

-c:并发次数(最小默认为1且不能大于总请求次数,如:10个请求,10个并发,实际就是1人请求1次)

-p:post参数文档路径(-p和-T参数要配合使用)

-T:header头内容类型(此处切记是大写英文字母T) 后面直接写请求路径即可:http://www.test.com/xxx/xxx.html

 

需要注意的是:ab 命令对于 get post 请求 参数各不相同 :

1  无参数 get 请求:

[root@localhost ~]# ab -c 10 -n 100 http://std_ecshop.com/api/status/check/fun

2  有参数 get 请求 :

[root@localhost ~]# ab -c 10 -n 100 "http://std_ecshop.com/api/status/check/fun?age=24&sex=1&Sign=2d6d480da94ac108862e37c4398ebd3f"

需要注意的是:请求 URL 要使用双引号括起来,否则 ab 无法识别 & 符号

 

3  post 请求就需要配合一个 txt 使用了, 因为文档上说的很清楚: -p 和 -T 参数一定要配合使用 否则报错

我们先在当前用户目录下新建一个 123.txt   我的文档内容实际是空的 因为我的接口不需要任何数据

[root@localhost ~]# touch 123.txt

然后发送请求:

[root@localhost ~]# ab -c 10 -n 100 -p 123.txt -T application/x-www-form-urlencoded http://std_ecshop.com/api/status/check/fun

返回结果:

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

Benchmarking std_ecshop.com (be patient).....done


Server Software:        nginx/1.17.9
Server Hostname:        std_ecshop.com
Server Port:            80

Document Path:          /api/status/check/fun
Document Length:        30707 bytes

Concurrency Level:      10
Time taken for tests:   35.489 seconds
Complete requests:      100
Failed requests:        98
   (Connect: 0, Receive: 0, Length: 98, Exceptions: 0)
Write errors:           0
Total transferred:      3095966 bytes
Total body sent:        17100
HTML transferred:       3070166 bytes
Requests per second:    2.82 [#/sec] (mean)
Time per request:       3548.924 [ms] (mean)
Time per request:       354.892 [ms] (mean, across all concurrent requests)
Transfer rate:          85.19 [Kbytes/sec] received
                        0.47 kb/s sent
                        85.66 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:   726 3440 310.0   3437    3951
Waiting:      726 3440 310.0   3437    3951
Total:        726 3441 310.0   3437    3951

Percentage of the requests served within a certain time (ms)
  50%   3437
  66%   3524
  75%   3598
  80%   3631
  90%   3664
  95%   3679
  98%   3704
  99%   3951
 100%   3951 (longest request)

具体参数我就不讲解了,我只关心  Complete requests 和 Failed requests

我这边有98个错误的 很明显是有问题的,下面说一下解决方案

Failed requests 下面 四个项 是具体的错误类型对应的数量

Failed requests: 98
(Connect: 0, Length: 98, Exceptions: 0)
只要出现Failed requests就会多一行数据来统计失败的原因,分别有Connect、Length、Exceptions。

Connect 无法送出要求、目标主机连接失败、要求的过程中被中断。

Receive:当客户端connect成功后,并且服务端成功accept,并且没有开始recv,然后服务端close掉socket,就产生这个错误(平时多见于服务端主动close掉客户端连接,即客户端表现为Connection reset by peer)

Length 响应的内容长度不一致 ( 以 Content-Length 头值为判断依据 )。

Exception 发生无法预期的错误。

 

错误出现在Length上,只要更改测试的地址回应的内容长度就ok了

最简单的就是 不要用 return 返回,要用 exit(); 例如:

    public function checkStatusFun()
    {
        log_write('1111');
        echo 'success';exit();
    }

注意,如果这里用 return ; 作为函数结束 则使用 ab 命令就会报错 Length 

最后看一下接口返回改为 exit(); 之后 ab 命令正确的返回:

[root@localhost ~]# ab -c 2 -n 2 -p 123.txt -T application/x-www-form-urlencoded http://std_ecshop.com/api/status/check/fun
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking std_ecshop.com (be patient).....done


Server Software:        nginx/1.17.9
Server Hostname:        std_ecshop.com
Server Port:            80

Document Path:          /api/status/check/fun
Document Length:        7 bytes

Concurrency Level:      2
Time taken for tests:   1.111 seconds
Complete requests:      2
Failed requests:        0
Write errors:           0
Total transferred:      386 bytes
Total body sent:        342
HTML transferred:       14 bytes
Requests per second:    1.80 [#/sec] (mean)
Time per request:       1110.856 [ms] (mean)
Time per request:       555.428 [ms] (mean, across all concurrent requests)
Transfer rate:          0.34 [Kbytes/sec] received
                        0.30 kb/s sent
                        0.64 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:   417  555 195.5    694     694
Waiting:      417  555 195.5    694     694
Total:        417  555 195.6    694     694

Percentage of the requests served within a certain time (ms)
  50%    694
  66%    694
  75%    694
  80%    694
  90%    694
  95%    694
  98%    694
  99%    694
 100%    694 (longest request)

我预计 网页应该是同理

再说一下 post 请求:

[root@localhost ~]# ab -c 10 -n 100 -p 123.txt -T application/x-www-form-urlencoded http://std_ecshop.com/api/status/check/fun

因为本文中 我的123.txt 是空白文档 如果有参数需要传输 数据格式应为 :  "key=value&key=value&key=value..."

注意:-p是参数文档路径,-T是大写英文字母,post表单格式为:application/x-www-form-urlencoded

也可以使用 json 格式  需要改的地方有:

[root@localhost ~]# ab -c 10 -n 100 -p 123.json -T application/json http://std_ecshop.com/api/status/check/fun

而 123.json 文件里面的数据格式应为:

{
"name":"wangfugui",
"age":"24",
"sex":1,
"sign":"2d6d480da94ac108862e37c4398ebd3f"
}

先暂时告一段落。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值