'ab' program freezes after lots of requests, why?
Another option to solve the issue is to enable HTTP KeepAlive by adding the "-k"
option. This will make ab to re-use the TCP connections and as consequence it won't exhaust all ports available. For example:
ab -n 45000 -c 10 -k http://127.0.0.1:3000/
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.
Instead of increasing the number of ports, change the length of TIME_WAIT
on Mac OS X.
This only works in development, but I can now ask ab
for as many requests as I want without timing out.
Set the default timeout to 1000ms like so:
$ sudo sysctl -w net.inet.tcp.msl=1000
net.inet.tcp.msl: 15000 -> 1000
The brianp.net page mentioned in the other answer is no longer available. You can retrieve it from the internet archive.