CouchDB / MochiWeb:持久连接的负面影响

7
我在我的Mint/Debian机器上安装了CouchDB。我的Java Web应用程序在查询CouchDB时遇到了相当长的延迟,所以我开始寻找原因。 编辑:查询模式是很多小查询和小JSON对象(如300字节/1K字节)。Wireshark转储非常好,显示大多数请求-响应时间为3-5毫秒。JVM帧采样向我展示套接字代码(客户端对Couch的查询)有些繁忙,但没有什么值得注意的地方。然后我尝试使用ApacheBench进行相同的分析,结果出乎意料:我发现保持连接会比非持久设置多稳定的39ms延迟。
有人知道如何解释这个问题吗?也许持久连接会增加TCP层的拥塞窗口,然后由于TCP_WAIT和小的请求/响应大小而处于空闲状态,或者类似的情况?这个选项(TCP_WAIT)是否应该对环回tcp连接启用?
w@mint ~ $ uname -a
Linux mint 2.6.39-2-486 #1 Tue Jul 5 02:52:23 UTC 2011 i686 GNU/Linux
w@mint ~ $ curl http://127.0.0.1:5984/
{"couchdb":"Welcome","version":"1.1.1"}

保持连接运行,平均每个请求40毫秒

w@mint ~ $ ab -n 1024 -c 1 -k http://127.0.0.1:5984/
>>>snip
Server Software:        CouchDB/1.1.1
Server Hostname:        127.0.0.1
Server Port:            5984

Document Path:          /
Document Length:        40 bytes

Concurrency Level:      1
Time taken for tests:   41.001 seconds
Complete requests:      1024
Failed requests:        0
Write errors:           0
Keep-Alive requests:    1024
Total transferred:      261120 bytes
HTML transferred:       40960 bytes
Requests per second:    24.98 [#/sec] (mean)
Time per request:       40.040 [ms] (mean)
Time per request:       40.040 [ms] (mean, across all concurrent requests)
Transfer rate:          6.22 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     1   40   1.4     40      48
Waiting:        0    1   0.7      1       8
Total:          1   40   1.3     40      48

Percentage of the requests served within a certain time (ms)
  50%     40
>>>snip
  95%     40
  98%     41
  99%     44
 100%     48 (longest request)

没有保持连接,然后就出现了 - 每个请求平均1毫秒。
w@mint ~ $ ab -n 1024 -c 1 http://127.0.0.1:5984/
>>>snip
Time taken for tests:   1.080 seconds
Complete requests:      1024
Failed requests:        0
Write errors:           0
Total transferred:      236544 bytes
HTML transferred:       40960 bytes
Requests per second:    948.15 [#/sec] (mean)
Time per request:       1.055 [ms] (mean)
Time per request:       1.055 [ms] (mean, across all concurrent requests)
Transfer rate:          213.89 [Kbytes/sec] received

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

Percentage of the requests served within a certain time (ms)
  50%      1
>>>snip
  80%      1
  90%      2
  95%      3
  98%      5
  99%      6
 100%     11 (longest request)

现在,我们开启了keep-alive并通过http头部请求关闭连接。每个请求大约需要1毫秒的时间。

w@mint ~ $ ab -n 1024 -c 1 -k -H 'Connection: close' http://127.0.0.1:5984/
>>>snip
Time taken for tests:   1.131 seconds
Complete requests:      1024
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      236544 bytes
HTML transferred:       40960 bytes
Requests per second:    905.03 [#/sec] (mean)
Time per request:       1.105 [ms] (mean)
Time per request:       1.105 [ms] (mean, across all concurrent requests)
Transfer rate:          204.16 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     1    1   1.2      1      14
Waiting:        0    1   1.1      1      13
Total:          1    1   1.2      1      14

Percentage of the requests served within a certain time (ms)
  50%      1
>>>snip
  80%      1
  90%      2
  95%      3
  98%      6
  99%      7
 100%     14 (longest request)
1个回答

8

是的,这与tcp套接字设置选项有关。该配置现在将所有三种情况水平化为每个请求1毫秒。

[httpd]
socket_options = [{nodelay, true}]

请参考以下内容了解详情: http://wiki.apache.org/couchdb/Performance#Network

1
谢谢您的跟进。我立刻想到了 nodelay,但我不确定是否正确以及如何确认它。 - JasonSmith

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