| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(http_perf_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
|
| 23 |
%% ct. |
| 24 |
|
| 25 |
all() -> |
| 26 |
%% @todo Enable HTTP/3 for this test suite. |
| 27 |
cowboy_test:common_all() -- [{group, h3}, {group, h3_compress}]. |
| 28 |
|
| 29 |
groups() -> |
| 30 |
cowboy_test:common_groups(ct_helper:all(?MODULE), no_parallel). |
| 31 |
|
| 32 |
init_per_suite(Config) -> |
| 33 |
do_log("", []), |
| 34 |
%% Optionally enable `perf` for the current node. |
| 35 |
% spawn(fun() -> ct:pal(os:cmd("perf record -g -F 9999 -o /tmp/http_perf.data -p " ++ os:getpid() ++ " -- sleep 60")) end), |
| 36 |
Config. |
| 37 |
|
| 38 |
end_per_suite(_) -> |
| 39 |
ok. |
| 40 |
|
| 41 |
init_per_group(Name, Config) -> |
| 42 |
[{group, Name}|cowboy_test:init_common_groups(Name, Config, ?MODULE, #{ |
| 43 |
%% HTTP/1.1 |
| 44 |
max_keepalive => infinity, |
| 45 |
%% HTTP/2 |
| 46 |
%% @todo Must configure Gun for performance too. |
| 47 |
connection_window_margin_size => 64*1024, |
| 48 |
enable_connect_protocol => true, |
| 49 |
env => #{dispatch => init_dispatch(Config)}, |
| 50 |
max_frame_size_sent => 64*1024, |
| 51 |
max_frame_size_received => 16384 * 1024 - 1, |
| 52 |
max_received_frame_rate => {10_000_000, 1}, |
| 53 |
stream_window_data_threshold => 1024, |
| 54 |
stream_window_margin_size => 64*1024 |
| 55 |
})]. |
| 56 |
|
| 57 |
end_per_group(Name, _) -> |
| 58 |
do_log("", []), |
| 59 |
cowboy_test:stop_group(Name). |
| 60 |
|
| 61 |
%% Routes. |
| 62 |
|
| 63 |
init_dispatch(_) -> |
| 64 |
cowboy_router:compile([{'_', [ |
| 65 |
{"/", hello_h, []}, |
| 66 |
{"/read_body", read_body_h, []} |
| 67 |
]}]). |
| 68 |
|
| 69 |
%% Tests: Hello world. |
| 70 |
|
| 71 |
plain_h_hello_1(Config) -> |
| 72 |
doc("Plain HTTP handler Hello World; 10K requests per 1 client."), |
| 73 |
do_bench_get(?FUNCTION_NAME, "/", #{}, 1, 10000, Config). |
| 74 |
|
| 75 |
plain_h_hello_10(Config) -> |
| 76 |
doc("Plain HTTP handler Hello World; 10K requests per 10 clients."), |
| 77 |
do_bench_get(?FUNCTION_NAME, "/", #{}, 10, 10000, Config). |
| 78 |
|
| 79 |
stream_h_hello_1(Config) -> |
| 80 |
doc("Stream handler Hello World; 10K requests per 1 client."), |
| 81 |
do_stream_h_hello(Config, 1). |
| 82 |
|
| 83 |
stream_h_hello_10(Config) -> |
| 84 |
doc("Stream handler Hello World; 10K requests per 10 clients."), |
| 85 |
do_stream_h_hello(Config, 10). |
| 86 |
|
| 87 |
do_stream_h_hello(Config, NumClients) -> |
| 88 |
Ref = config(ref, Config), |
| 89 |
ProtoOpts = ranch:get_protocol_options(Ref), |
| 90 |
StreamHandlers = case ProtoOpts of |
| 91 |
#{stream_handlers := _} -> [cowboy_compress_h, stream_hello_h]; |
| 92 |
_ -> [stream_hello_h] |
| 93 |
end, |
| 94 |
ranch:set_protocol_options(Ref, ProtoOpts#{ |
| 95 |
env => #{}, |
| 96 |
stream_handlers => StreamHandlers |
| 97 |
}), |
| 98 |
do_bench_get(?FUNCTION_NAME, "/", #{}, NumClients, 10000, Config), |
| 99 |
ranch:set_protocol_options(Ref, ProtoOpts). |
| 100 |
|
| 101 |
%% Tests: Large body upload. |
| 102 |
|
| 103 |
plain_h_1M_post_1(Config) -> |
| 104 |
doc("Plain HTTP handler body reading; 10K requests per 1 client."), |
| 105 |
do_bench_post(?FUNCTION_NAME, "/read_body", #{}, <<0:8_000_000>>, 1, 10000, Config). |
| 106 |
|
| 107 |
plain_h_1M_post_10(Config) -> |
| 108 |
doc("Plain HTTP handler body reading; 10K requests per 10 clients."), |
| 109 |
do_bench_post(?FUNCTION_NAME, "/read_body", #{}, <<0:8_000_000>>, 10, 10000, Config). |
| 110 |
|
| 111 |
plain_h_10G_post(Config) -> |
| 112 |
doc("Plain HTTP handler body reading; 1 request with a 10GB body."), |
| 113 |
do_bench_post_one_large(?FUNCTION_NAME, "/read_body", #{}, 10_000, <<0:8_000_000>>, Config). |
| 114 |
|
| 115 |
%% Internal. |
| 116 |
|
| 117 |
do_bench_get(What, Path, Headers, NumClients, NumRuns, Config) -> |
| 118 |
Clients = [spawn_link(?MODULE, do_bench_get_proc, |
| 119 |
[self(), What, Path, Headers, NumRuns, Config]) |
| 120 |
|| _ <- lists:seq(1, NumClients)], |
| 121 |
{Time, _} = timer:tc(?MODULE, do_bench_wait, [What, Clients]), |
| 122 |
do_log("~32s: ~8bµs ~8.1freqs/s", [ |
| 123 |
[atom_to_list(config(group, Config)), $., atom_to_list(What)], |
| 124 |
Time, |
| 125 |
(NumClients * NumRuns) / Time * 1_000_000]), |
| 126 |
ok. |
| 127 |
|
| 128 |
do_bench_get_proc(Parent, What, Path, Headers0, NumRuns, Config) -> |
| 129 |
ConnPid = gun_open(Config), |
| 130 |
Headers = Headers0#{<<"accept-encoding">> => <<"gzip">>}, |
| 131 |
Parent ! {What, ready}, |
| 132 |
receive {What, go} -> ok end, |
| 133 |
do_bench_get_run(ConnPid, Path, Headers, NumRuns), |
| 134 |
Parent ! {What, done}, |
| 135 |
gun:close(ConnPid). |
| 136 |
|
| 137 |
do_bench_get_run(_, _, _, 0) -> |
| 138 |
ok; |
| 139 |
do_bench_get_run(ConnPid, Path, Headers, Num) -> |
| 140 |
Ref = gun:request(ConnPid, <<"GET">>, Path, Headers, <<>>), |
| 141 |
{response, IsFin, 200, _RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 142 |
{ok, _} = case IsFin of |
| 143 |
nofin -> gun:await_body(ConnPid, Ref, infinity); |
| 144 |
fin -> {ok, <<>>} |
| 145 |
end, |
| 146 |
do_bench_get_run(ConnPid, Path, Headers, Num - 1). |
| 147 |
|
| 148 |
do_bench_post(What, Path, Headers, Body, NumClients, NumRuns, Config) -> |
| 149 |
Clients = [spawn_link(?MODULE, do_bench_post_proc, |
| 150 |
[self(), What, Path, Headers, Body, NumRuns, Config]) |
| 151 |
|| _ <- lists:seq(1, NumClients)], |
| 152 |
{Time, _} = timer:tc(?MODULE, do_bench_wait, [What, Clients]), |
| 153 |
do_log("~32s: ~8bµs ~8.1freqs/s", [ |
| 154 |
[atom_to_list(config(group, Config)), $., atom_to_list(What)], |
| 155 |
Time, |
| 156 |
(NumClients * NumRuns) / Time * 1_000_000]), |
| 157 |
ok. |
| 158 |
|
| 159 |
do_bench_post_proc(Parent, What, Path, Headers0, Body, NumRuns, Config) -> |
| 160 |
ConnPid = gun_open(Config), |
| 161 |
Headers = Headers0#{<<"accept-encoding">> => <<"gzip">>}, |
| 162 |
Parent ! {What, ready}, |
| 163 |
receive {What, go} -> ok end, |
| 164 |
do_bench_post_run(ConnPid, Path, Headers, Body, NumRuns), |
| 165 |
Parent ! {What, done}, |
| 166 |
gun:close(ConnPid). |
| 167 |
|
| 168 |
do_bench_post_run(_, _, _, _, 0) -> |
| 169 |
ok; |
| 170 |
do_bench_post_run(ConnPid, Path, Headers, Body, Num) -> |
| 171 |
Ref = gun:request(ConnPid, <<"POST">>, Path, Headers, Body), |
| 172 |
{response, IsFin, 200, _RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 173 |
{ok, _} = case IsFin of |
| 174 |
nofin -> gun:await_body(ConnPid, Ref, infinity); |
| 175 |
fin -> {ok, <<>>} |
| 176 |
end, |
| 177 |
do_bench_post_run(ConnPid, Path, Headers, Body, Num - 1). |
| 178 |
|
| 179 |
do_bench_post_one_large(What, Path, Headers, NumChunks, BodyChunk, Config) -> |
| 180 |
Client = spawn_link(?MODULE, do_bench_post_one_large_proc, |
| 181 |
[self(), What, Path, Headers, NumChunks, BodyChunk, Config]), |
| 182 |
{Time, _} = timer:tc(?MODULE, do_bench_wait, [What, [Client]]), |
| 183 |
do_log("~32s: ~8bµs ~8.1freqs/s", [ |
| 184 |
[atom_to_list(config(group, Config)), $., atom_to_list(What)], |
| 185 |
Time, |
| 186 |
1 / Time * 1_000_000]), |
| 187 |
ok. |
| 188 |
|
| 189 |
do_bench_post_one_large_proc(Parent, What, Path, Headers0, NumChunks, BodyChunk, Config) -> |
| 190 |
ConnPid = gun_open(Config), |
| 191 |
Headers = Headers0#{<<"accept-encoding">> => <<"gzip">>}, |
| 192 |
Parent ! {What, ready}, |
| 193 |
receive {What, go} -> ok end, |
| 194 |
StreamRef = gun:headers(ConnPid, <<"POST">>, Path, Headers#{ |
| 195 |
<<"content-length">> => integer_to_binary(NumChunks * byte_size(BodyChunk)) |
| 196 |
}), |
| 197 |
do_bench_post_one_large_run(ConnPid, StreamRef, NumChunks - 1, BodyChunk), |
| 198 |
{response, IsFin, 200, _RespHeaders} = gun:await(ConnPid, StreamRef, infinity), |
| 199 |
{ok, _} = case IsFin of |
| 200 |
nofin -> gun:await_body(ConnPid, StreamRef, infinity); |
| 201 |
fin -> {ok, <<>>} |
| 202 |
end, |
| 203 |
Parent ! {What, done}, |
| 204 |
gun:close(ConnPid). |
| 205 |
|
| 206 |
do_bench_post_one_large_run(ConnPid, StreamRef, 0, BodyChunk) -> |
| 207 |
gun:data(ConnPid, StreamRef, fin, BodyChunk); |
| 208 |
do_bench_post_one_large_run(ConnPid, StreamRef, NumChunks, BodyChunk) -> |
| 209 |
gun:data(ConnPid, StreamRef, nofin, BodyChunk), |
| 210 |
do_bench_post_one_large_run(ConnPid, StreamRef, NumChunks - 1, BodyChunk). |
| 211 |
|
| 212 |
do_bench_wait(What, Clients) -> |
| 213 |
_ = [receive {What, ready} -> ok end || _ <- Clients], |
| 214 |
_ = [ClientPid ! {What, go} || ClientPid <- Clients], |
| 215 |
_ = [receive {What, done} -> ok end || _ <- Clients], |
| 216 |
ok. |
| 217 |
|
| 218 |
do_log(Str, Args) -> |
| 219 |
ct:log(Str, Args), |
| 220 |
io:format(ct_default_gl, Str ++ "~n", Args). |