Debian Codegraph @

test/ws_perf_SUITE.erl

raw
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(ws_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/2]).
22 -import(cowboy_test, [gun_down/1]).
23
24 %% ct.
25
26 all() ->
27 [{group, binary}, {group, ascii}, {group, mixed}, {group, japanese}].
28
29 groups() ->
30 CommonGroups = cowboy_test:common_groups(ct_helper:all(?MODULE), no_parallel),
31 SubGroups = [G || G = {GN, _, _} <- CommonGroups,
32 GN =:= http orelse GN =:= h2c orelse GN =:= http_compress orelse GN =:= h2c_compress],
33 [
34 {binary, [], SubGroups},
35 {ascii, [], SubGroups},
36 {mixed, [], SubGroups},
37 {japanese, [], SubGroups}
38 ].
39
40 init_per_suite(Config) ->
41 %% Optionally enable `perf` for the current node.
42 % spawn(fun() -> ct:pal(os:cmd("perf record -g -F 9999 -o /tmp/ws_perf.data -p " ++ os:getpid() ++ " -- sleep 60")) end),
43 Config.
44
45 end_per_suite(_Config) ->
46 ok.
47
48 init_per_group(Name, Config) when Name =:= http; Name =:= http_compress ->
49 init_info(Name, Config),
50 cowboy_test:init_common_groups(Name, Config, ?MODULE);
51 init_per_group(Name, Config) when Name =:= h2c; Name =:= h2c_compress ->
52 init_info(Name, Config),
53 {Flavor, Opts} = case Name of
54 h2c -> {vanilla, #{}};
55 h2c_compress -> {compress, #{stream_handlers => [cowboy_compress_h, cowboy_stream_h]}}
56 end,
57 Config1 = cowboy_test:init_http(Name, Opts#{
58 %% The margin sizes must be larger than the larger test message for plain sockets.
59 connection_window_margin_size => 128*1024,
60 enable_connect_protocol => true,
61 env => #{dispatch => init_dispatch(Config)},
62 max_frame_size_sent => 64*1024,
63 max_frame_size_received => 16384 * 1024 - 1,
64 max_received_frame_rate => {10_000_000, 1},
65 stream_window_data_threshold => 1024,
66 stream_window_margin_size => 128*1024
67 }, [{flavor, Flavor}|Config]),
68 lists:keyreplace(protocol, 1, Config1, {protocol, http2});
69 init_per_group(ascii, Config) ->
70 init_text_data("ascii.txt", Config);
71 init_per_group(mixed, Config) ->
72 init_text_data("grok_segond.txt", Config);
73 init_per_group(japanese, Config) ->
74 init_text_data("japanese.txt", Config);
75 init_per_group(binary, Config) ->
76 [{frame_type, binary}|Config].
77
78 init_info(Name, Config) ->
79 DataInfo = case config(frame_type, Config) of
80 text -> config(text_data_filename, Config);
81 binary -> binary
82 end,
83 ConnInfo = case Name of
84 http -> "cleartext HTTP/1.1";
85 http_compress -> "cleartext HTTP/1.1 with compression";
86 h2c -> "cleartext HTTP/2";
87 h2c_compress -> "cleartext HTTP/2 with compression"
88 end,
89 ct:pal("Websocket over ~s (~s)", [ConnInfo, DataInfo]).
90
91 init_text_data(Filename, Config) ->
92 {ok, Text} = file:read_file(filename:join(config(data_dir, Config), Filename)),
93 [
94 {frame_type, text},
95 {text_data, Text},
96 {text_data_filename, Filename}
97 |Config].
98
99 end_per_group(Name, _Config) ->
100 cowboy_test:stop_group(Name).
101
102 %% Dispatch configuration.
103
104 init_dispatch(_Config) ->
105 cowboy_router:compile([
106 {"localhost", [
107 {"/ws_echo", ws_echo, []},
108 {"/ws_ignore", ws_ignore, []}
109 ]}
110 ]).
111
112 %% Support functions for testing using Gun.
113
114 do_gun_open_ws(Path, Config) ->
115 ConnPid = gun_open(Config, #{
116 http2_opts => #{
117 connection_window_margin_size => 64*1024,
118 max_frame_size_sent => 64*1024,
119 max_frame_size_received => 16384 * 1024 - 1,
120 notify_settings_changed => true,
121 stream_window_data_threshold => 1024,
122 stream_window_margin_size => 64*1024
123 },
124 tcp_opts => [{nodelay, true}],
125 ws_opts => #{compress => config(flavor, Config) =:= compress}
126 }),
127 case config(protocol, Config) of
128 http -> ok;
129 http2 ->
130 {notify, settings_changed, #{enable_connect_protocol := true}}
131 = gun:await(ConnPid, undefined) %% @todo Maybe have a gun:await/1?
132 end,
133 StreamRef = gun:ws_upgrade(ConnPid, Path),
134 receive
135 {gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} ->
136 {ok, ConnPid, StreamRef};
137 {gun_response, ConnPid, _, _, Status, Headers} ->
138 exit({ws_upgrade_failed, Status, Headers});
139 {gun_error, ConnPid, StreamRef, Reason} ->
140 exit({ws_upgrade_failed, Reason})
141 after 1000 ->
142 error(timeout)
143 end.
144
145 receive_ws(ConnPid, StreamRef) ->
146 receive
147 {gun_ws, ConnPid, StreamRef, Frame} ->
148 {ok, Frame}
149 after 30000 ->
150 {error, timeout}
151 end.
152
153 %% Tests.
154
155 echo_1_00064KiB(Config) ->
156 doc("Send and receive a 64KiB frame."),
157 do_echo(Config, echo_1, 1, 64 * 1024).
158
159 echo_1_00256KiB(Config) ->
160 doc("Send and receive a 256KiB frame."),
161 do_echo(Config, echo_1, 1, 256 * 1024).
162
163 echo_1_01024KiB(Config) ->
164 doc("Send and receive a 1024KiB frame."),
165 do_echo(Config, echo_1, 1, 1024 * 1024).
166
167 echo_1_04096KiB(Config) ->
168 doc("Send and receive a 4096KiB frame."),
169 do_echo(Config, echo_1, 1, 4096 * 1024).
170
171 %% Minus one because frames can only get so big.
172 echo_1_16384KiB(Config) ->
173 doc("Send and receive a 16384KiB - 1 frame."),
174 do_echo(Config, echo_1, 1, 16384 * 1024 - 1).
175
176 echo_N_00000B(Config) ->
177 doc("Send and receive a 0B frame 1000 times."),
178 do_echo(Config, echo_N, 1000, 0).
179
180 echo_N_00256B(Config) ->
181 doc("Send and receive a 256B frame 1000 times."),
182 do_echo(Config, echo_N, 1000, 256).
183
184 echo_N_01024B(Config) ->
185 doc("Send and receive a 1024B frame 1000 times."),
186 do_echo(Config, echo_N, 1000, 1024).
187
188 echo_N_04096B(Config) ->
189 doc("Send and receive a 4096B frame 1000 times."),
190 do_echo(Config, echo_N, 1000, 4096).
191
192 echo_N_16384B(Config) ->
193 doc("Send and receive a 16384B frame 1000 times."),
194 do_echo(Config, echo_N, 1000, 16384).
195
196 %echo_N_16384B_10K(Config) ->
197 % doc("Send and receive a 16384B frame 10000 times."),
198 % do_echo(Config, echo_N, 10000, 16384).
199
200 do_echo(Config, What, Num, FrameSize) ->
201 {ok, ConnPid, StreamRef} = do_gun_open_ws("/ws_echo", Config),
202 FrameType = config(frame_type, Config),
203 FrameData = case FrameType of
204 text -> do_text_data(Config, FrameSize);
205 binary -> rand:bytes(FrameSize)
206 end,
207 %% Heat up the processes before doing the real run.
208 % do_echo_loop(ConnPid, StreamRef, Num, FrameType, FrameData),
209 {Time, _} = timer:tc(?MODULE, do_echo_loop, [ConnPid, StreamRef, Num, FrameType, FrameData]),
210 do_log("~-6s ~-6s ~6s: ~8bµs", [What, FrameType, do_format_size(FrameSize), Time]),
211 gun:ws_send(ConnPid, StreamRef, close),
212 {ok, close} = receive_ws(ConnPid, StreamRef),
213 gun_down(ConnPid).
214
215 do_echo_loop(_, _, 0, _, _) ->
216 ok;
217 do_echo_loop(ConnPid, StreamRef, Num, FrameType, FrameData) ->
218 gun:ws_send(ConnPid, StreamRef, {FrameType, FrameData}),
219 {ok, {FrameType, FrameData}} = receive_ws(ConnPid, StreamRef),
220 do_echo_loop(ConnPid, StreamRef, Num - 1, FrameType, FrameData).
221
222 send_1_00064KiB(Config) ->
223 doc("Send a 64KiB frame."),
224 do_send(Config, send_1, 1, 64 * 1024).
225
226 send_1_00256KiB(Config) ->
227 doc("Send a 256KiB frame."),
228 do_send(Config, send_1, 1, 256 * 1024).
229
230 send_1_01024KiB(Config) ->
231 doc("Send a 1024KiB frame."),
232 do_send(Config, send_1, 1, 1024 * 1024).
233
234 send_1_04096KiB(Config) ->
235 doc("Send a 4096KiB frame."),
236 do_send(Config, send_1, 1, 4096 * 1024).
237
238 %% Minus one because frames can only get so big.
239 send_1_16384KiB(Config) ->
240 doc("Send a 16384KiB - 1 frame."),
241 do_send(Config, send_1, 1, 16384 * 1024 - 1).
242
243 send_N_00000B(Config) ->
244 doc("Send a 0B frame 10000 times."),
245 do_send(Config, send_N, 10000, 0).
246
247 send_N_00256B(Config) ->
248 doc("Send a 256B frame 10000 times."),
249 do_send(Config, send_N, 10000, 256).
250
251 send_N_01024B(Config) ->
252 doc("Send a 1024B frame 10000 times."),
253 do_send(Config, send_N, 10000, 1024).
254
255 send_N_04096B(Config) ->
256 doc("Send a 4096B frame 10000 times."),
257 do_send(Config, send_N, 10000, 4096).
258
259 send_N_16384B(Config) ->
260 doc("Send a 16384B frame 10000 times."),
261 do_send(Config, send_N, 10000, 16384).
262
263 %send_N_16384B_10K(Config) ->
264 % doc("Send and receive a 16384B frame 10000 times."),
265 % do_send(Config, send_N, 10000, 16384).
266
267 do_send(Config, What, Num, FrameSize) ->
268 {ok, ConnPid, StreamRef} = do_gun_open_ws("/ws_ignore", Config),
269 %% Prepare the frame data.
270 FrameType = config(frame_type, Config),
271 FrameData = case FrameType of
272 text -> do_text_data(Config, FrameSize);
273 binary -> rand:bytes(FrameSize)
274 end,
275 %% Heat up the processes before doing the real run.
276 % do_send_loop(Socket, Num, FrameType, Mask, MaskedFrameData),
277 {Time, _} = timer:tc(?MODULE, do_send_loop, [ConnPid, StreamRef, Num, FrameType, FrameData]),
278 do_log("~-6s ~-6s ~6s: ~8bµs", [What, FrameType, do_format_size(FrameSize), Time]),
279 gun:ws_send(ConnPid, StreamRef, close),
280 {ok, close} = receive_ws(ConnPid, StreamRef),
281 gun_down(ConnPid).
282
283 do_send_loop(ConnPid, StreamRef, 0, _, _) ->
284 gun:ws_send(ConnPid, StreamRef, {text, <<"CHECK">>}),
285 {ok, {text, <<"CHECK">>}} = receive_ws(ConnPid, StreamRef),
286 ok;
287 do_send_loop(ConnPid, StreamRef, Num, FrameType, FrameData) ->
288 gun:ws_send(ConnPid, StreamRef, {FrameType, FrameData}),
289 do_send_loop(ConnPid, StreamRef, Num - 1, FrameType, FrameData).
290
291 tcp_send_N_00000B(Config) ->
292 doc("Send a 0B frame 10000 times."),
293 do_tcp_send(Config, tcps_N, 10000, 0).
294
295 tcp_send_N_00256B(Config) ->
296 doc("Send a 256B frame 10000 times."),
297 do_tcp_send(Config, tcps_N, 10000, 256).
298
299 tcp_send_N_01024B(Config) ->
300 doc("Send a 1024B frame 10000 times."),
301 do_tcp_send(Config, tcps_N, 10000, 1024).
302
303 tcp_send_N_04096B(Config) ->
304 doc("Send a 4096B frame 10000 times."),
305 do_tcp_send(Config, tcps_N, 10000, 4096).
306
307 tcp_send_N_16384B(Config) ->
308 doc("Send a 16384B frame 10000 times."),
309 do_tcp_send(Config, tcps_N, 10000, 16384).
310
311 do_tcp_send(Config, What, Num, FrameSize) ->
312 {ok, Socket} = do_tcp_handshake(Config, #{}),
313 %% Prepare the frame data.
314 FrameType = config(frame_type, Config),
315 FrameData = case FrameType of
316 text -> do_text_data(Config, FrameSize);
317 binary -> rand:bytes(FrameSize)
318 end,
319 %% Mask the data outside the benchmark to avoid influencing the results.
320 Mask = 16#37fa213d,
321 MaskedFrameData = ws_SUITE:do_mask(FrameData, Mask, <<>>),
322 FrameSizeWithHeader = FrameSize + case FrameSize of
323 N when N =< 125 -> 6;
324 N when N =< 16#ffff -> 8;
325 N when N =< 16#7fffffffffffffff -> 14
326 end,
327 %% Run the benchmark; different function for h1 and h2.
328 {Time, _} = case config(protocol, Config) of
329 http -> timer:tc(?MODULE, do_tcp_send_loop_h1,
330 [Socket, Num, FrameType, Mask, MaskedFrameData]);
331 http2 -> timer:tc(?MODULE, do_tcp_send_loop_h2,
332 [Socket, 65535, Num, FrameType, Mask, MaskedFrameData, FrameSizeWithHeader])
333 end,
334 do_log("~-6s ~-6s ~6s: ~8bµs", [What, FrameType, do_format_size(FrameSize), Time]),
335 gen_tcp:close(Socket).
336
337 %% Do a prior knowledge handshake.
338 do_tcp_handshake(Config, LocalSettings) ->
339 Protocol = config(protocol, Config),
340 Socket1 = case Protocol of
341 http ->
342 {ok, Socket, _} = ws_SUITE:do_handshake(<<"/ws_ignore">>, Config),
343 Socket;
344 http2 ->
345 {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
346 %% Send a valid preface.
347 ok = gen_tcp:send(Socket, [
348 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
349 cow_http2:settings(LocalSettings)
350 ]),
351 %% Receive the server preface.
352 {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
353 {ok, << 4:8, 0:40, SettingsPayload:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
354 RemoteSettings = cow_http2:parse_settings_payload(SettingsPayload),
355 #{enable_connect_protocol := true} = RemoteSettings,
356 %% Send the SETTINGS ack.
357 ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
358 %% Receive the SETTINGS ack.
359 {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
360 %% Send a CONNECT :protocol request to upgrade the stream to Websocket.
361 {ReqHeadersBlock, _} = cow_hpack:encode([
362 {<<":method">>, <<"CONNECT">>},
363 {<<":protocol">>, <<"websocket">>},
364 {<<":scheme">>, <<"http">>},
365 {<<":path">>, <<"/ws_ignore">>},
366 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
367 {<<"sec-websocket-version">>, <<"13">>},
368 {<<"origin">>, <<"http://localhost">>}
369 ]),
370 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, ReqHeadersBlock)),
371 %% Receive a 200 response.
372 {ok, << Len1:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
373 {ok, RespHeadersBlock} = gen_tcp:recv(Socket, Len1, 1000),
374 {RespHeaders, _} = cow_hpack:decode(RespHeadersBlock),
375 {_, <<"200">>} = lists:keyfind(<<":status">>, 1, RespHeaders),
376 Socket
377 end,
378 %% Enable active mode to avoid delays in receiving data at the end of benchmark.
379 ok = inet:setopts(Socket1, [{active, true}]),
380 %% Stream number 1 is ready.
381 {ok, Socket1}.
382
383 do_tcp_send_loop_h1(Socket, 0, _, Mask, _) ->
384 MaskedFrameData = ws_SUITE:do_mask(<<"CHECK">>, Mask, <<>>),
385 ok = gen_tcp:send(Socket,
386 <<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedFrameData/binary>>),
387 do_tcp_wait_for_check(Socket);
388 do_tcp_send_loop_h1(Socket, Num, FrameType, Mask, MaskedFrameData) ->
389 Len = byte_size(MaskedFrameData),
390 LenBits = case Len of
391 N when N =< 125 -> << N:7 >>;
392 N when N =< 16#ffff -> << 126:7, N:16 >>;
393 N when N =< 16#7fffffffffffffff -> << 127:7, N:64 >>
394 end,
395 FrameHeader = <<1:1, 0:3, 2:4, 1:1, LenBits/bits, Mask:32>>,
396 ok = gen_tcp:send(Socket, [
397 FrameHeader,
398 MaskedFrameData
399 ]),
400 do_tcp_send_loop_h1(Socket, Num - 1, FrameType, Mask, MaskedFrameData).
401
402 do_tcp_send_loop_h2(Socket, _, 0, _, Mask, _, _) ->
403 MaskedFrameData = ws_SUITE:do_mask(<<"CHECK">>, Mask, <<>>),
404 ok = gen_tcp:send(Socket, cow_http2:data(1, nofin,
405 <<1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedFrameData/binary>>)),
406 do_tcp_wait_for_check(Socket);
407 do_tcp_send_loop_h2(Socket, Window0, Num, FrameType, Mask, MaskedFrameData, FrameSizeWithHeader)
408 when Window0 < FrameSizeWithHeader ->
409 %% The remaining window isn't large enough so
410 %% we wait for WINDOW_UPDATE frames.
411 Window = do_tcp_wait_window_updates(Socket, Window0),
412 do_tcp_send_loop_h2(Socket, Window, Num, FrameType, Mask, MaskedFrameData, FrameSizeWithHeader);
413 do_tcp_send_loop_h2(Socket, Window, Num, FrameType, Mask, MaskedFrameData, FrameSizeWithHeader) ->
414 Len = byte_size(MaskedFrameData),
415 LenBits = case Len of
416 N when N =< 125 -> << N:7 >>;
417 N when N =< 16#ffff -> << 126:7, N:16 >>;
418 N when N =< 16#7fffffffffffffff -> << 127:7, N:64 >>
419 end,
420 FrameHeader = <<1:1, 0:3, 2:4, 1:1, LenBits/bits, Mask:32>>,
421 ok = gen_tcp:send(Socket, cow_http2:data(1, nofin, [
422 FrameHeader,
423 MaskedFrameData
424 ])),
425 do_tcp_send_loop_h2(Socket, Window - FrameSizeWithHeader,
426 Num - 1, FrameType, Mask, MaskedFrameData, FrameSizeWithHeader).
427
428 do_tcp_wait_window_updates(Socket, Window) ->
429 receive
430 {tcp, Socket, Data} ->
431 do_tcp_wait_window_updates_parse(Socket, Window, Data)
432 after 0 ->
433 Window
434 end.
435
436 do_tcp_wait_window_updates_parse(Socket, Window, Data) ->
437 case Data of
438 %% Ignore the connection-wide WINDOW_UPDATE.
439 <<4:24, 8:8, 0:8, 0:32, 0:1, _:31, Rest/bits>> ->
440 do_tcp_wait_window_updates_parse(Socket, Window, Rest);
441 %% Use the stream-specific WINDOW_UPDATE to increment our window.
442 <<4:24, 8:8, 0:8, 1:32, 0:1, Inc:31, Rest/bits>> ->
443 do_tcp_wait_window_updates_parse(Socket, Window + Inc, Rest);
444 %% Other frames are not expected.
445 <<>> ->
446 do_tcp_wait_window_updates(Socket, Window)
447 end.
448
449 do_tcp_wait_for_check(Socket) ->
450 receive
451 {tcp, Socket, Data} ->
452 case binary:match(Data, <<"CHECK">>) of
453 nomatch -> do_tcp_wait_for_check(Socket);
454 _ -> ok
455 end
456 after 5000 ->
457 error(timeout)
458 end.
459
460 %% Internal.
461
462 do_text_data(Config, FrameSize) ->
463 do_text_data1(config(text_data, Config), FrameSize).
464
465 do_text_data1(LargeText, FrameSize) when byte_size(LargeText) >= FrameSize ->
466 binary:part(LargeText, 0, FrameSize);
467 do_text_data1(LargeText, FrameSize) ->
468 do_text_data1(<<LargeText/binary, LargeText/binary>>, FrameSize).
469
470 do_format_size(Size) when Size < 1024 ->
471 integer_to_list(Size) ++ "B";
472 do_format_size(Size) when Size < (1024*1024) ->
473 integer_to_list(Size div 1024) ++ "KiB";
474 do_format_size(Size) ->
475 integer_to_list(Size div (1024*1024)) ++ "MiB".
476
477 do_log(Str, Args) ->
478 ct:log(Str, Args),
479 io:format(ct_default_gl, Str ++ "~n", Args).