Skip to content

Commit

Permalink
Add initial http_perf_SUITE
Browse files Browse the repository at this point in the history
  • Loading branch information
essen committed Jan 24, 2025
1 parent 05d7715 commit 2531b26
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/cowboy_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 23 additions & 20 deletions test/cowboy_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand Down
15 changes: 15 additions & 0 deletions test/handlers/stream_hello_h.erl
Original file line number Diff line number Diff line change
@@ -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.
132 changes: 132 additions & 0 deletions test/http_perf_SUITE.erl
Original file line number Diff line number Diff line change
@@ -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).

0 comments on commit 2531b26

Please sign in to comment.