'ab' 程序在多次请求后冻结,为什么? [英] 'ab' program freezes after lots of requests, why?

查看:18
本文介绍了'ab' 程序在多次请求后冻结,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我使用ab"对 Web 服务器进行基准测试时,它会在发送大量请求后冻结一段时间,然后在 20 秒左右后继续.

考虑以下用 Ruby 编写的 HTTP 服务器模拟器:

需要'socket'响应 = "HTTP/1.1 200 OK
" +"连接:关闭
" +"
" +"
"缓冲区="server = TCPServer.new("127.0.0.1", 3000) # 在 3000 端口创建 TCP 服务器.server.listen(1024) # 设置 backlog 为 1024.虽然是真的client = server.accept # 接受新客户端.client.write(RESPONSE) # 写一个股票HTTP"响应.client.close_write # 关闭套接字的写入部分.client.read(nil, buffer) # 从套接字读取所有数据.client.close # 关闭它.结尾

然后我按如下方式运行 ab:

ab -n 45000 -c 10 http://127.0.0.1:3000/

在最初的几秒钟内,ab 会按预期工作并使用 100% 的 CPU:

基准测试 127.0.0.1(请耐心等待)完成 4500 个请求完成 9000 个请求完成13500个请求

在大约 13500 个请求之后,系统 CPU 使用率下降到 0%.ab似乎被某事冻结了.问题不在服务器上,因为此时服务器正在调用 accept().大约 20 秒后 ab 继续好像什么也没发生一样,将再次使用 100% CPU,但在几秒钟后再次冻结.

我怀疑内核中的某些东西正在限制连接,但是是什么以及为什么?我正在使用 OS X Leopard.我在 Linux 上也看到过类似的行为,尽管冻结发生在大量请求并且不经常发生.

这个问题使我无法运行大型 HTTP 基准测试.

解决方案

听起来您的 临时端口.要检查,请使用 netstat 命令并在 TIME_WAIT 状态.

在 Mac OS X 上,默认的临时端口范围是 49152 到 65535,总共 16384 个端口.您可以使用 sysctl 命令:

<前>$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.lastnet.inet.ip.portrange.first: 49152net.inet.ip.portrange.last:65535

一旦您用完临时端口,您通常需要等到 TIME_WAIT 状态到期(2 * 最大段生存期),直到您可以重用特定端口号.您可以通过将范围更改为从 32768 开始(Linux 和 Solaris 上的默认值)来使端口数增加一倍.(最大端口号是 65535,所以你不能增加高端.)

<前>$ sudo sysctl -w net.inet.ip.portrange.first=32768net.inet.ip.portrange.first: 49152 -> 32768

注意IANA指定的官方范围是49152到65535,部分防火墙可以假设动态分配的端口在该范围内.您可能需要重新配置防火墙,以便使用本地网络之外的更大范围.

还可以减少最大段生命周期(Mac OS X 上的 sysctl net.inet.tcp.msl),它控制 TIME_WAIT 状态的持续时间,但这很危险,因为它可能导致旧连接与使用相同端口号的新连接混淆.还有一些技巧涉及使用 SO_REUSEADDR 选项绑定到特定端口,或使用 SO_LINGER 选项关闭,但这些也可能导致新旧连接混淆,所以通常被认为是坏主意.

Whenever I use 'ab' to benchmark a web server, it will freeze for a while after having sent lots of requests, only to continue after 20 seconds or so.

Consider the following HTTP server simulator, written in Ruby:

require 'socket'

RESPONSE = "HTTP/1.1 200 OK
" +
           "Connection: close
" +
           "
" +
           "
"

buffer = ""
server = TCPServer.new("127.0.0.1", 3000)  # Create TCP server at port 3000.
server.listen(1024)                        # Set backlog to 1024.
while true
    client = server.accept             # Accept new client.
    client.write(RESPONSE)             # Write a stock "HTTP" response.
    client.close_write                 # Shutdown write part of the socket.
    client.read(nil, buffer)           # Read all data from the socket.  
    client.close                       # Close it.
end

I then run ab as follows:

ab -n 45000 -c 10 http://127.0.0.1:3000/

During the first few seconds, ab does its job as it's supposed to and uses 100% CPU:

Benchmarking 127.0.0.1 (be patient)
Completed 4500 requests
Completed 9000 requests
Completed 13500 requests

After about 13500 requests, system CPU usage drops to 0%. ab seems to be frozen on something. The problem is not in the server because at this moment, the server is calling accept(). After about 20 seconds ab continues as if nothing happened, and will use 100% CPU again, only to freeze again after several seconds.

I suspect something in the kernel is throttling connections, but what and why? I'm using OS X Leopard. I've seen similar behavior on Linux as well, though the freeze happens at a much larger number of requests and doesn't happen so often.

This problem prevents me from running large HTTP benchmarks.

解决方案

It sounds like you are running out of ephemeral ports. To check, use the netstat command and look for several thousand ports in the TIME_WAIT state.

On Mac OS X the default ephemeral port range is 49152 to 65535, for a total of 16384 ports. You can check this with the sysctl command:

$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535

Once you run out of ephemeral ports, you will normally need to wait until the TIME_WAIT state expires (2 * maximum segment lifetime) until you can reuse a particular port number. You can double the number of ports by changing the range to start at 32768, which is the default on Linux and Solaris. (The maximum port number is 65535 so you cannot increase the high end.)

$ sudo sysctl -w net.inet.ip.portrange.first=32768
net.inet.ip.portrange.first: 49152 -> 32768

Note that the official range designated by IANA is 49152 to 65535, and some firewalls may assume that dynamically assigned ports fall within that range. You may need to reconfigure your firewall in order to make use of a larger range outside of your local network.

It is also possible to reduce the maximum segment lifetime (sysctl net.inet.tcp.msl on Mac OS X), which controls the duration of the TIME_WAIT state, but this is dangerous as it could cause older connections to get mixed up with newer ones that are using the same port number. There are also some tricks involving binding to specific ports with the SO_REUSEADDR option, or closing with the SO_LINGER option, but those also could cause old and new connections to be mixed up, so are generally considered to be bad ideas.

这篇关于'ab' 程序在多次请求后冻结,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆