我的ApacheBench负载测试结果中,按长度分类的请求失败。

27
我有一个使用PHP、Lighttpd和MySQL在Centos 5上的网站。我用Apache Bench (ab)测试了下面的代码,结果出现了一些错误(失败的请求),表示长度与正常情况不同。我非常确定我的PHP结果应该始终具有完全相同的长度。我已经查看了我的Lighttpd和MySQL日志以及错误日志,但没有发现任何错误。
有没有办法精确地检查当结果具有其他长度时ab获取的内容,或者有没有其他方法找出引起问题的原因或"坏"结果是什么?
我需要知道这个,因为我需要得到100%良好的结果。
-bash-3.2# ab -n 500 -c 200 http://domain.com/test/index.php
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking domain.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Finished 500 requests


Server Software:        lighttpd/1.4.20
Server Hostname:        domain.com
Server Port:            80

Document Path:          /test/index.php
Document Length:        15673 bytes

Concurrency Level:      200
Time taken for tests:   0.375862 seconds
Complete requests:      500
Failed requests:        499
   (Connect: 0, Length: 499, Exceptions: 0)
Write errors:           0
Total transferred:      7920671 bytes
HTML transferred:       7837000 bytes
Requests per second:    1330.28 [#/sec] (mean)
Time per request:       150.345 [ms] (mean)
Time per request:       0.752 [ms] (mean, across all concurrent requests)
Transfer rate:          20579.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   10   9.4      6      30
Processing:     0  113 133.5     16     342
Waiting:        0  111 134.3     12     341
Total:          0  123 138.9     16     370

Percentage of the requests served within a certain time (ms)
  50%     16
  66%    235
  75%    289
  80%    298
  90%    331
  95%    345
  98%    365
  99%    368
 100%    370 (longest request)
4个回答

21

使用参数-v 2运行ab,意思是详细程度为2。这将转储响应头。如果您的请求未使用分块编码,您将看到一个"Content-Length"头,指示每个响应的大小。

gw:~$ ab -n 1 -v 2 "http://whatever.com/"

...

LOG: header received:
HTTP/1.0 200 OK
...
Content-Length: 1568399
如果你使用分块编码(chunked encoding)回应,那么在传输结束之前长度是未知的。通常情况下,分块编码仅用于已压缩的回应,并且ApacheBench默认不进行压缩。
如果它为某种原因正在压缩回应,那可能就可以解释了;压缩后的长度取决于内容。
你还可以使用`curl -i`和`--compress`选项来查看有和没有压缩的单个请求的响应头。

31
匿名用户的评论(被拒绝的编辑):注意:ab希望所有响应大小相等。如果您的输出有可能大小不同,您应该忽略“失败的请求”,因为ab将认为它们是失败的。 - Anne

4

使用tcpdump

打开两个终端/Shell窗口或直接使用screen。

在第一个窗口中,使用tcpdump将从/到您的NIC(eth0)的传输数据捕获到文件中:

sudo tcpdump -s 9999 -i eth0 -w myfile.txt

在第二个窗口中,执行您的ab命令:
ab -n 500 -c 200 http://domain.com/test/index.php

当这一切都完成后,使用“strings”和“grep”解析文件:

strings myfile2.txt | grep -C 3 "200 OK"

您应该能够通过观察或使用grep命令来监视所有数据段的结果。


尝试使用sudo tcpdump时,我收到了“tcpdump:ioctl:没有这样的设备”消息。 - Tom Smykowski
我按照你写的做了,这是结果:-- </noscript> <!-- End Quantcast tag -->HTTP/1.0 200 OK Connection: close X-Powered-By: PHP/5.2.6 Content-type: text/html我该如何解释这个结果? - Tom Smykowski
Tomaszs:您需要根据您运行的*nix操作系统更改NIC设备标识符。Mac上是en0,Linux上是eth0等。HTTP/1.0 200 OK 表示 Web 服务器找到了所请求的资源并返回内容,页面调用成功!您只需读取myfile2.txt文件的内容,即可查看故障出现的位置。 - randomx
字符串命令具体是做什么的?直接从文件目录中grep有什么区别? - cedricliang

3

ab假设所有响应都是相同的。它查看第一个响应的content-length,然后将其他响应与其进行比较。

来自man页面:

Document Length
  This is the size in bytes of the first successfully returned document. 
  If the document length changes during  testing,  the  response  is 
  considered an error.

如果您的第一个请求包含以下数据:

{"hostname":"nodecellar-1-dwfxd","serverip":"10.1.3.3"}

下一个是:
{"hostname":"nodecellar-1-dwfxd","serverip":"10.1.3.30"}

由于输出的字符数增加了一个字符,因此ab将会因为长度错误而失败。


您可以使用“-l”选项关闭长度检查。 - Gogowitsch

0
这是一个与动态页面有关的问题,它发生在HTTP头部Content-Length在请求之间可能会变化的情况下。当使用ab测试此类页面时,您需要使用-l选项。
-l              Accept variable document length (use this for dynamic pages)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接