diff options
author | Loïc Hoguin <[email protected]> | 2025-01-24 13:05:45 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2025-01-24 13:05:45 +0100 |
commit | 2531b26acf804892b27a9171afa566ac007616ef (patch) | |
tree | a110035bfd8186a23dadd8a7949c1e893d67bdda | |
parent | 05d77153a02d90b97a075ed059f878b5ab9ab615 (diff) | |
download | cowboy-2531b26acf804892b27a9171afa566ac007616ef.tar.gz cowboy-2531b26acf804892b27a9171afa566ac007616ef.tar.bz2 cowboy-2531b26acf804892b27a9171afa566ac007616ef.zip |
Add initial http_perf_SUITE
-rw-r--r-- | src/cowboy_http.erl | 1 | ||||
-rw-r--r-- | test/cowboy_test.erl | 43 | ||||
-rw-r--r-- | test/handlers/stream_hello_h.erl | 15 | ||||
-rw-r--r-- | test/http_perf_SUITE.erl | 132 |
4 files changed, 171 insertions, 20 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 9c92ec5..78d65d2 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -295,6 +295,7 @@ set_timeout(State=#state{streams=[], in_state=InState}, idle_timeout) when element(1, InState) =/= ps_body -> State; %% Otherwise we can set the timeout. +%% @todo Don't do this so often, use a strategy similar to Websocket/H2 if possible. set_timeout(State0=#state{opts=Opts, overriden_opts=Override}, Name) -> State = cancel_timeout(State0), Default = case Name of diff --git a/test/cowboy_test.erl b/test/cowboy_test.erl index 670da18..e547b90 100644 --- a/test/cowboy_test.erl +++ b/test/cowboy_test.erl @@ -120,50 +120,53 @@ common_groups(Tests, Parallel) -> Groups end. -init_common_groups(Name = http, Config, Mod) -> - init_http(Name, #{ +init_common_groups(Name, Config, Mod) -> + init_common_groups(Name, Config, Mod, #{}). + +init_common_groups(Name = http, Config, Mod, ProtoOpts) -> + init_http(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)} }, [{flavor, vanilla}|Config]); -init_common_groups(Name = https, Config, Mod) -> - init_https(Name, #{ +init_common_groups(Name = https, Config, Mod, ProtoOpts) -> + init_https(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)} }, [{flavor, vanilla}|Config]); -init_common_groups(Name = h2, Config, Mod) -> - init_http2(Name, #{ +init_common_groups(Name = h2, Config, Mod, ProtoOpts) -> + init_http2(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)} }, [{flavor, vanilla}|Config]); -init_common_groups(Name = h2c, Config, Mod) -> - Config1 = init_http(Name, #{ +init_common_groups(Name = h2c, Config, Mod, ProtoOpts) -> + Config1 = init_http(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)} }, [{flavor, vanilla}|Config]), lists:keyreplace(protocol, 1, Config1, {protocol, http2}); -init_common_groups(Name = h3, Config, Mod) -> - init_http3(Name, #{ +init_common_groups(Name = h3, Config, Mod, ProtoOpts) -> + init_http3(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)} }, [{flavor, vanilla}|Config]); -init_common_groups(Name = http_compress, Config, Mod) -> - init_http(Name, #{ +init_common_groups(Name = http_compress, Config, Mod, ProtoOpts) -> + init_http(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)}, stream_handlers => [cowboy_compress_h, cowboy_stream_h] }, [{flavor, compress}|Config]); -init_common_groups(Name = https_compress, Config, Mod) -> - init_https(Name, #{ +init_common_groups(Name = https_compress, Config, Mod, ProtoOpts) -> + init_https(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)}, stream_handlers => [cowboy_compress_h, cowboy_stream_h] }, [{flavor, compress}|Config]); -init_common_groups(Name = h2_compress, Config, Mod) -> - init_http2(Name, #{ +init_common_groups(Name = h2_compress, Config, Mod, ProtoOpts) -> + init_http2(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)}, stream_handlers => [cowboy_compress_h, cowboy_stream_h] }, [{flavor, compress}|Config]); -init_common_groups(Name = h2c_compress, Config, Mod) -> - Config1 = init_http(Name, #{ +init_common_groups(Name = h2c_compress, Config, Mod, ProtoOpts) -> + Config1 = init_http(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)}, stream_handlers => [cowboy_compress_h, cowboy_stream_h] }, [{flavor, compress}|Config]), lists:keyreplace(protocol, 1, Config1, {protocol, http2}); -init_common_groups(Name = h3_compress, Config, Mod) -> - init_http3(Name, #{ +init_common_groups(Name = h3_compress, Config, Mod, ProtoOpts) -> + init_http3(Name, ProtoOpts#{ env => #{dispatch => Mod:init_dispatch(Config)}, stream_handlers => [cowboy_compress_h, cowboy_stream_h] }, [{flavor, compress}|Config]). diff --git a/test/handlers/stream_hello_h.erl b/test/handlers/stream_hello_h.erl new file mode 100644 index 0000000..e67e220 --- /dev/null +++ b/test/handlers/stream_hello_h.erl @@ -0,0 +1,15 @@ +%% This module is the fastest way of producing a Hello world! + +-module(stream_hello_h). + +-export([init/3]). +-export([terminate/3]). + +init(_, _, State) -> + {[ + {response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>}, + stop + ], State}. + +terminate(_, _, _) -> + ok. diff --git a/test/http_perf_SUITE.erl b/test/http_perf_SUITE.erl new file mode 100644 index 0000000..8702b9d --- /dev/null +++ b/test/http_perf_SUITE.erl @@ -0,0 +1,132 @@ +%% Copyright (c) 2025, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(http_perf_SUITE). +-compile(export_all). +-compile(nowarn_export_all). + +-import(ct_helper, [config/2]). +-import(ct_helper, [doc/1]). +-import(cowboy_test, [gun_open/1]). + +%% ct. + +all() -> + %% @todo Enable HTTP/3 for this test suite. + cowboy_test:common_all() -- [{group, h3}, {group, h3_compress}]. + +groups() -> + cowboy_test:common_groups(ct_helper:all(?MODULE), no_parallel). + +init_per_suite(Config) -> + do_log("", []), + %% Optionally enable `perf` for the current node. +% spawn(fun() -> ct:pal(os:cmd("perf record -g -F 9999 -o /tmp/ws_perf.data -p " ++ os:getpid() ++ " -- sleep 60")) end), + Config. + +end_per_suite(_) -> + ok. + +init_per_group(Name, Config) -> + [{group, Name}|cowboy_test:init_common_groups(Name, Config, ?MODULE, #{ + %% HTTP/1.1 + max_keepalive => infinity, + %% HTTP/2 + max_received_frame_rate => {10_000_000, 1} + })]. + +end_per_group(Name, _) -> + do_log("", []), + cowboy_test:stop_group(Name). + +%% Routes. + +init_dispatch(_) -> + cowboy_router:compile([{'_', [ + {"/", hello_h, []} + ]}]). + +%% Tests. + +stream_h_hello_1(Config) -> + doc("Stream handler Hello World; 10K requests per 1 client."), + do_stream_h_hello(Config, 1). + +stream_h_hello_10(Config) -> + doc("Stream handler Hello World; 10K requests per 10 clients."), + do_stream_h_hello(Config, 10). + +do_stream_h_hello(Config, NumClients) -> + Ref = config(ref, Config), + ProtoOpts = ranch:get_protocol_options(Ref), + StreamHandlers = case ProtoOpts of + #{stream_handlers := _} -> [cowboy_compress_h, stream_hello_h]; + _ -> [stream_hello_h] + end, + ranch:set_protocol_options(Ref, ProtoOpts#{ + env => #{}, + stream_handlers => StreamHandlers + }), + do_bench_get(?FUNCTION_NAME, "/", #{}, NumClients, 10000, Config), + ranch:set_protocol_options(Ref, ProtoOpts). + +plain_h_hello_1(Config) -> + doc("Plain HTTP handler Hello World; 10K requests per 1 client."), + do_bench_get(?FUNCTION_NAME, "/", #{}, 1, 10000, Config). + +plain_h_hello_10(Config) -> + doc("Plain HTTP handler Hello World; 10K requests per 10 clients."), + do_bench_get(?FUNCTION_NAME, "/", #{}, 10, 10000, Config). + +%% Internal. + +do_bench_get(What, Path, Headers, NumClients, NumRuns, Config) -> + Clients = [spawn_link(?MODULE, do_bench_proc, [self(), What, Path, Headers, NumRuns, Config]) + || _ <- lists:seq(1, NumClients)], + _ = [receive {What, ready} -> ok end || _ <- Clients], + {Time, _} = timer:tc(?MODULE, do_bench_get1, [What, Clients]), + do_log("~32s: ~8bµs ~8.1freqs/s", [ + [atom_to_list(config(group, Config)), $., atom_to_list(What)], + Time, + (NumClients * NumRuns) / Time * 1_000_000]), + ok. + +do_bench_get1(What, Clients) -> + _ = [ClientPid ! {What, go} || ClientPid <- Clients], + _ = [receive {What, done} -> ok end || _ <- Clients], + ok. + +do_bench_proc(Parent, What, Path, Headers0, NumRuns, Config) -> + ConnPid = gun_open(Config), + Headers = Headers0#{<<"accept-encoding">> => <<"gzip">>}, + Parent ! {What, ready}, + receive {What, go} -> ok end, + do_bench_run(ConnPid, Path, Headers, NumRuns), + Parent ! {What, done}, + gun:close(ConnPid). + +do_bench_run(_, _, _, 0) -> + ok; +do_bench_run(ConnPid, Path, Headers, Num) -> + Ref = gun:request(ConnPid, <<"GET">>, Path, Headers, <<>>), + {response, IsFin, 200, _RespHeaders} = gun:await(ConnPid, Ref, infinity), + {ok, _} = case IsFin of + nofin -> gun:await_body(ConnPid, Ref, infinity); + fin -> {ok, <<>>} + end, + do_bench_run(ConnPid, Path, Headers, Num - 1). + +do_log(Str, Args) -> + ct:log(Str, Args), + io:format(ct_default_gl, Str ++ "~n", Args). |