0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00
cowboy/test/handlers
Loïc Hoguin 49be0f57cf
Implement dynamic socket buffer sizes
Cowboy will set the socket's buffer size dynamically to
better fit the current workload. When the incoming data
is small, a low buffer size reduces the memory footprint
and improves responsiveness and therefore performance.
When the incoming data is large, such as large HTTP
request bodies, a larger buffer size helps us avoid
doing too many binary appends and related allocations.

Setting a large buffer size for all use cases is
sub-optimal because allocating more than needed
necessarily results in a performance hit (not just
increased memory usage).

By default Cowboy starts with a buffer size of 8192 bytes.
It then doubles or halves the buffer size depending on
the size of the data it receives from the socket. It
stops decreasing at 8192 and increasing at 131072 by
default.

To keep track of the size of the incoming data Cowboy
maintains a moving average. It allows Cowboy to avoid
changing the buffer too often but still react quickly
when necessary. Cowboy will increase the buffer size
when the moving average is above 90% of the current
buffer size, and decrease when the moving average is
below 40% of the current buffer size.

The current buffer size and moving average are
propagated when switching protocols. The dynamic buffer
is implemented in HTTP/1, HTTP/2 and HTTP/1 Websocket.
HTTP/2 Websocket has it disabled because it doesn't
interact directly with the socket; in that case it
is HTTP/2 that has a dynamic buffer.

The dynamic buffer provides a very large performance improvement
in many scenarios, at minimal cost for others. Because it largely
depend on the underlying protocol the improvements are no all equal.
TLS and compression also impact the results.

The improvement when reading a large request body, with the
requests repeated in a fast loop are:

* HTTP: 6x to 20x faster
* HTTPS: 2x to 6x faster
* H2: 4x to 5x faster
* H2C: 20x to 40x faster

I am not sure why H2C's performance was so bad, especially compared
to H2, when using default buffer sizes. Dynamic buffers make H2C a
lot more viable with default settings.

The performance impact on "hello world" type requests is minimal,
it goes from -5% to +5% roughly.

Websocket improvements vary again depending on the protocol, but
also depending on whether compression is enabled:

* HTTP echo: roughly 2x faster
* HTTP send: roughly 4x faster
* H2C echo: roughly 2x faster
* H2C send: 3x to 4x faster

In the echo test we reply back, and Gun doesn't have the dynamic
buffer optimisation, so that probably explains the x2 difference.

With compression however there isn't much improvement. The results
are roughly within -10% to +10% of each other. Zlib compression
seems to be a bottleneck, or at least to modify the performance
profile to such an extent that the size of the buffer does not
matter. This happens to randomly generated binary data as well
so it is probably not caused by the test data.
2025-02-05 14:29:58 +01:00
..
accept_callback_h.erl Move the final old HTTP suite tests and remove it 2018-11-22 00:12:18 +01:00
accept_callback_missing_h.erl Move many old HTTP test cases to the rest_handler test suite 2018-11-21 18:39:59 +01:00
asterisk_h.erl Properly handle OPTIONS * requests 2017-12-06 14:05:30 +01:00
charset_in_content_types_provided_h.erl Better handle content negotiation when accept contains charsets 2018-11-02 13:49:54 +01:00
charset_in_content_types_provided_implicit_h.erl Better handle content negotiation when accept contains charsets 2018-11-02 13:49:54 +01:00
charset_in_content_types_provided_implicit_no_callback_h.erl Better handle content negotiation when accept contains charsets 2018-11-02 13:49:54 +01:00
charsets_provided_empty_h.erl Add tests for charsets_provided 2018-11-02 13:54:19 +01:00
charsets_provided_h.erl Add tests for charsets_provided 2018-11-02 13:54:19 +01:00
compress_h.erl Don't automatically compress when response has etag 2024-01-05 15:53:42 +01:00
content_types_accepted_h.erl Document media type wildcard in content_types_accepted 2019-10-03 16:20:29 +02:00
content_types_provided_h.erl Move many old HTTP test cases to the rest_handler test suite 2018-11-21 18:39:59 +01:00
crash_h.erl Handle error_response command in cowboy_metrics_h 2018-10-31 15:13:23 +01:00
create_resource_h.erl AcceptCallback may now return created/see_other tuples for POST 2020-11-27 16:17:43 +01:00
custom_req_fields_h.erl Fix using custom fields in Req 2019-09-06 12:25:02 +02:00
decompress_h.erl Rework and improve the decompress stream handler 2024-01-04 15:50:12 +01:00
default_h.erl Add a metrics test when the handler didn't send anything 2017-10-21 13:12:36 +01:00
delay_hello_h.erl Graceful shutdown 2020-11-27 15:38:21 +01:00
delete_resource_h.erl Move the final old HTTP suite tests and remove it 2018-11-22 00:12:18 +01:00
echo_h.erl Fix match_qs with constraints when key is not present 2024-01-23 14:11:29 +01:00
expires_h.erl Move many old HTTP test cases to the rest_handler test suite 2018-11-21 18:39:59 +01:00
generate_etag_h.erl REST: Allow generate_etag to return undefined 2024-01-16 16:28:52 +01:00
hello_h.erl Initial commit with connection/streams 2016-03-05 20:20:42 +01:00
if_range_h.erl Add support for range requests (RFC7233) in cowboy_rest 2018-11-07 18:55:06 +01:00
last_modified_h.erl Move many old HTTP test cases to the rest_handler test suite 2018-11-21 18:39:59 +01:00
long_polling_h.erl Fix broken tests in loop_handler_SUITE 2017-10-31 16:49:48 +00:00
long_polling_sys_h.erl Add initial support for system messages in cowboy_loop 2018-03-23 18:33:16 +01:00
loop_handler_abort_h.erl Data received after RST_STREAM counts toward window 2019-07-16 15:32:58 +02:00
loop_handler_body_h.erl Allow passing options to sub protocols 2017-02-18 18:26:20 +01:00
loop_handler_endless_h.erl Change send_timeout_close test to accomodate macOS 2023-12-14 15:25:25 +01:00
loop_handler_timeout_h.erl Add the idle_timeout HTTP/1.1 protocol option 2017-05-03 17:44:00 +02:00
loop_handler_timeout_hibernate_h.erl Add timeout to cowboy_loop 2023-12-15 15:37:34 +01:00
loop_handler_timeout_info_h.erl Increase loop_handler_timeout timeouts 2023-12-15 16:22:06 +01:00
loop_handler_timeout_init_h.erl Increase loop_handler_timeout timeouts 2023-12-15 16:22:06 +01:00
multipart_h.erl Update the multipart reading interface 2016-08-10 15:09:04 +02:00
provide_callback_missing_h.erl Improve errors for when the ProvideCallback is missing 2018-08-13 17:23:48 +02:00
provide_range_callback_h.erl Add ProvideRangeCallback tests using sendfile 2018-11-11 16:09:28 +01:00
range_satisfiable_h.erl Add support for range requests (RFC7233) in cowboy_rest 2018-11-07 18:55:06 +01:00
ranges_provided_auto_h.erl Add automatic ranged request handling for bytes units 2018-11-11 13:57:26 +01:00
ranges_provided_h.erl Add support for range requests (RFC7233) in cowboy_rest 2018-11-07 18:55:06 +01:00
rate_limited_h.erl Add the rate_limited/2 REST callback 2018-11-04 11:58:59 +01:00
read_body_h.erl Implement dynamic socket buffer sizes 2025-02-05 14:29:58 +01:00
resp_h.erl Initial HTTP/3 implementation 2024-03-26 15:53:48 +01:00
resp_iolist_body_h.erl Always dialyze tests and fix some cowboy_req specs 2018-10-31 10:50:57 +01:00
rest_hello_h.erl Add a cowboy_rest test for malformed if-*-match headers 2018-10-31 22:41:59 +01:00
send_message_h.erl Add more tests to the rfc7230 suite 2017-11-24 00:16:19 +01:00
set_options_h.erl Add cowboy_req:cast/2 2019-10-07 13:25:49 +02:00
stop_handler_h.erl Add support for range requests (RFC7233) in cowboy_rest 2018-11-07 18:55:06 +01:00
stream_handler_h.erl Handle socket errors in HTTP/1.1 and HTTP/2 2023-12-12 15:05:33 +01:00
stream_hello_h.erl Add initial http_perf_SUITE 2025-01-24 13:05:45 +01:00
streamed_result_h.erl Optionally reset the idle timeout when sending data 2023-12-21 14:03:07 +01:00
switch_handler_h.erl Add support for range requests (RFC7233) in cowboy_rest 2018-11-07 18:55:06 +01:00
switch_protocol_flush_h.erl Always dialyze tests and fix some cowboy_req specs 2018-10-31 10:50:57 +01:00
ws_active_commands_h.erl Add the {deflate, boolean()} Websocket command 2018-11-13 15:55:09 +01:00
ws_deflate_commands_h.erl Add the {deflate, boolean()} Websocket command 2018-11-13 15:55:09 +01:00
ws_deflate_opts_h.erl Document the commands based Websocket interface 2019-10-06 16:51:27 +02:00
ws_dont_validate_utf8_h.erl Document the commands based Websocket interface 2019-10-06 16:51:27 +02:00
ws_handle_commands_h.erl Add a commands-based interface to Websocket handlers 2018-09-11 14:33:58 +02:00
ws_ignore.erl Implement dynamic socket buffer sizes 2025-02-05 14:29:58 +01:00
ws_info_commands_h.erl Add a commands-based interface to Websocket handlers 2018-09-11 14:33:58 +02:00
ws_init_commands_h.erl Add a commands-based interface to Websocket handlers 2018-09-11 14:33:58 +02:00
ws_init_h.erl Ensure HTTP/1.1 Websocket resets the trap_exit flag 2024-01-08 11:47:59 +01:00
ws_ping_h.erl Confirm Websocket pong frames are received by handlers 2024-01-16 12:20:38 +01:00
ws_set_options_commands_h.erl Websocket: Allow setting the max_frame_size option dynamically 2025-01-16 14:47:20 +01:00
ws_shutdown_reason_commands_h.erl Add shutdown_reason Websocket command 2019-10-10 11:33:35 +02:00
ws_terminate_h.erl Always dialyze tests and fix some cowboy_req specs 2018-10-31 10:50:57 +01:00