【问题标题】:netty: the http file example can not work with apache bench when keep-alive is enablednetty:启用 keep-alive 时,http 文件示例无法与 apache bench 一起使用
【发布时间】:2016-02-29 02:04:58
【问题描述】:

正在测试的http文件示例: https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/example/http/file

我用 netty-4.1.0.beta8 编译了上面的例子。

My test result:
$ ab -k -n 2 -c 1 -v 6 http://127.0.0.1:8080/test.sh
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 127.0.0.1 (be patient)...INFO: POST header == 
---
GET /test.sh HTTP/1.0
Connection: Keep-Alive
Host: 127.0.0.1:8080
User-Agent: ApacheBench/2.3
Accept: */*


---
LOG: header received:
HTTP/1.1 200 OK
content-length: 462
content-type: application/octet-stream
date: Fri, 26 Feb 2016 06:34:52 GMT
expires: Fri, 26 Feb 2016 06:35:52 GMT
cache-control: private, max-age=60
last-modified: Fri, 19 Feb 2016 02:35:40 GMT
connection: keep-alive


LOG: Response code = 200
LOG: header received:
**MY TEST.SH CONTENT**

WARNING: Response code not 2xx (500)
apr_poll: The timeout specified has expired (70007)
Total of 1 requests completed

似乎我的 test.sh 内容被认为是第二个请求的标头。

我的进一步测试:

$ ab -k -n 1 -c 1 -v 6 http://127.0.0.1:8080/test.sh 
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 127.0.0.1 (be patient)...INFO: POST header == 
---
GET /test.sh HTTP/1.0
Connection: Keep-Alive
Host: 127.0.0.1:8080
User-Agent: ApacheBench/2.3
Accept: */*


---
LOG: header received:
HTTP/1.1 200 OK
content-length: 462
content-type: application/octet-stream
date: Fri, 26 Feb 2016 06:39:02 GMT
expires: Fri, 26 Feb 2016 06:40:02 GMT
cache-control: private, max-age=60
last-modified: Fri, 19 Feb 2016 02:35:40 GMT
connection: keep-alive


LOG: Response code = 200
..done


Server Software:        
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /test.sh
Document Length:        0 bytes

Concurrency Level:      1
Time taken for tests:   0.005 seconds
Complete requests:      1
Failed requests:        0
Write errors:           0
Keep-Alive requests:    1
Total transferred:      263 bytes
HTML transferred:       0 bytes
Requests per second:    209.25 [#/sec] (mean)
Time per request:       4.779 [ms] (mean)
Time per request:       4.779 [ms] (mean, across all concurrent requests)
Transfer rate:          53.74 [Kbytes/sec] received

Connection Times (ms)
          min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     5    5   0.0      5       5
Waiting:        5    5   0.0      5       5
Total:          5    5   0.0      5       5

可以看到文档长度为0字节。

我对 www.google.com 运行了类似的命令,它运行良好。 你能帮忙吗?提前致谢。

【问题讨论】:

  • 如果-k 被删除了还能用吗?
  • 是的。我的服务器在发送每个响应后关闭非保持活动 http 连接。在这种情况下,ApacheBench 知道它是套接字输入流的结尾。

标签: java http netty keep-alive


【解决方案1】:

这是 ApacheBench 中的一个错误。

ApacheBench 不完全遵循 http 规范,并假定标头具有一定的大小写。因为 netty 应用程序返回的标头没有 ApacheBench 期望的字符大小写,所以它假定内容长度默认为 0 字节。

Http specification 4.2:

HTTP 标头字段,其中包括 general-header(第 4.5 节), request-header(第 5.3 节)、response-header(第 6.2 节)和 entity-header(第 7.1 节)字段,遵循相同的通用格式 在 RFC 822 [9] 的第 3.1 节中给出。每个头域包括 名称后跟冒号 (":") 和字段值。 字段名称 不区分大小写。 字段值前面可以有任意数量的 LWS,尽管首选单个 SP。标题字段可以扩展 通过在每个额外的行之前至少有一个 SP 来跨越多行 或 HT。应用程序应遵循已知的“通用形式” 或指示,在生成 HTTP 构造时,因为可能存在 一些实现无法接受超出常见形式的任何内容。

因为 ApacheBench 的 keep-alive 开关强制它假设服务器支持 keep alive,所以它不会检测到 keep alive 数据包的“丢失”标头。

这个问题应该在 ApacheBench 中解决,因为它是该应用程序中的一个错误,导致它错过了正确的内容长度。

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2013-08-26
    • 1970-01-01
    • 2013-01-19
    • 2012-01-04
    相关资源
    最近更新 更多