wrk icon indicating copy to clipboard operation
wrk copied to clipboard

Add option to control connection persistence (Keep-Alive)

Open amichair opened this issue 9 years ago • 8 comments

Thanks for the great tool!

It would be very useful to add an option (-k?) to control whether Keep-Alive is enabled or not, so that one can test the load in different scenarios of web server and network configurations.

Even better would be to specify then number of Keep-Alive requests per connection. For example, to better mimic the real load on a website, you could set this number to e.g. 10, such that on each client connection 10 requests are made and then the connection is closed, and the next request requires a new connection to be established. In this case a setting of 1 would be equivalent to no persistence (1 request per connection) and a setting of 0 might indicate unlimited persistence (which is the current and default behavior, if I understand the thin documentation correctly).

amichair avatar Mar 09 '16 09:03 amichair

In HTTP/1.1 a connection is always assumed to be keep-alive. To close a connection after every request you can use the --header "Connection: Close" option. For the even better part you could specify a per request header using a lua script.

Snuggert avatar Apr 26 '16 13:04 Snuggert

I realize it can be done with a script... but that is much more work (especially to non-developers or the majority of developers, who are not fluent in Lua). My point is that this seems to me like very common and useful functionality for testing real-life scenarios on any HTTP server the will be accessed by real-world browsers, which is most of them. For something very useful and common, it's worth adding a built-in command line option rather than having many people re-implementing the same functionality themselves in scripts... that's mho anyway :-)

amichair avatar Apr 26 '16 16:04 amichair

You can also use the -H "Connection: Close" on the command line, no need to put it in a lua script.

amsharma avatar Aug 22 '16 05:08 amsharma

Note -H "Connection: Close" make serves close the connection. Which is quite different from clients closing them. Whether client or server closes a connection - it changes quite a lot resource use and connection clean up responsibilities - different aspects of TCP handling. When server is made to close connections the OS keeps connections for minutes in TIME_WAIT state. This is quite heavy burden on the server.

mafjmafj avatar Aug 14 '17 16:08 mafjmafj

Got a few thumb-ups up there in the past couple of years... would you reconsider implementing the -k command line argument suggestion? I think that can solve all the pipeline-related scenarios pretty elegantly.

amichair avatar Apr 03 '19 17:04 amichair

I assume that to do this with a Lua script, you would disconnect the connection (and reopen?) in the "response" global Lua function. However, there doesn't seem to be any kind of disconnect or reconnect in the Lua API. So I don't think this can be done from Lua.

You can specify the header, of course, and then you're asking the server to do it for you. Similar, but not quite the same.

noahgibbs avatar Apr 18 '19 07:04 noahgibbs

Here's a branch to do this -- it's a really simple code change: https://github.com/noahgibbs/wrk/tree/no_keepalive

noahgibbs avatar May 31 '19 15:05 noahgibbs

Note -H "Connection: Close" make serves close the connection. Which is quite different from clients closing them. Whether client or server closes a connection - it changes quite a lot resource use and connection clean up responsibilities - different aspects of TCP handling. When server is made to close connections the OS keeps connections for minutes in TIME_WAIT state. This is quite heavy burden on the server.

A workable solution: Permits sockets in the “time-wait” state to be reused for new connections.

sudo sysctl -w net.ipv4.tcp_tw_reuse=1

In high traffic environments, sockets are created and destroyed at very high rates. This parameter, when set, allows “no longer needed” and “about to be destroyed” sockets to be used for new connections. When enabled, this parameter can bypass the allocation and initialization overhead normally associated with socket creation saving CPU cycles, system load and time.

The default value is 0 (off). The recommended value is 1 (on).

ref: https://www.ibm.com/docs/en/linux-on-systems?topic=tuning-tcpip-ipv4-settings

hxysayhi avatar May 05 '22 09:05 hxysayhi