From b624de3ed08456ed4f71d1d89cb45adec554cd57 Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 6 Apr 2022 16:01:04 +0800 Subject: [PATCH 1/4] feat: add random delay --- rebar.config | 3 ++- src/httpc_bench.erl | 17 ++++++++++------- src/httpc_bench_ehttpc.erl | 2 +- src/httpc_bench_server.erl | 1 + 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/rebar.config b/rebar.config index 7fe1d07..3e12efb 100644 --- a/rebar.config +++ b/rebar.config @@ -7,7 +7,8 @@ {git, "https://github.com/benoitc/hackney.git", {tag, "1.17.4"}}}, % {ibrowse, ".*", % {git, "https://github.com/cmullaparthi/ibrowse.git", {tag, "v4.4.0"}}}, - {ehttpc, ".*", {git, "https://github.com/emqx/ehttpc", {tag, "0.1.11"}}}, + % {ehttpc, ".*", {git, "https://github.com/emqx/ehttpc", {tag, "0.1.15"}}}, + {ehttpc, ".*", {git, "https://github.com/emqx/ehttpc", {branch, "0404-refactor-collect-gen-calls-to-queue"}}}, {timing, ".*", {git, "https://github.com/lpgauth/timing.git", {branch, "master"}}} ]}. diff --git a/src/httpc_bench.erl b/src/httpc_bench.erl index c67547b..3c58e17 100644 --- a/src/httpc_bench.erl +++ b/src/httpc_bench.erl @@ -5,20 +5,23 @@ run/0 ]). --define(N, 2048000). +-define(N, 50000). -define(CLIENTS, [ - httpc_bench_buoy, - httpc_bench_ehttpc, - httpc_bench_hackney, - httpc_bench_httpc + % httpc_bench_buoy, + httpc_bench_ehttpc + % httpc_bench_hackney, + % httpc_bench_httpc ]). % -define(CONCURENCIES, [32, 64, 128, 512, 2048, 4096]). % -define(POOL_SIZES, [8, 16, 32, 64, 128, 256]). --define(CONCURENCIES, [10240]). --define(POOL_SIZES, [1024]). +-define(CONCURENCIES, [20480]). +-define(POOL_SIZES, [16]). + +% -define(CONCURENCIES, [10240]). +% -define(POOL_SIZES, [1024]). %% public run() -> diff --git a/src/httpc_bench_ehttpc.erl b/src/httpc_bench_ehttpc.erl index cd9b6b0..a2f5703 100644 --- a/src/httpc_bench_ehttpc.erl +++ b/src/httpc_bench_ehttpc.erl @@ -22,7 +22,7 @@ start(PoolSize) -> application:ensure_all_started(ehttpc), PoolOpts = [{host, "127.0.0.1"}, {port, 8080}, - {enable_pipelining, true}, + {enable_pipelining, false}, {pool_size, PoolSize}, {pool_type, random}, {connect_timeout, 5000}, diff --git a/src/httpc_bench_server.erl b/src/httpc_bench_server.erl index 8c79fdc..9e071c5 100644 --- a/src/httpc_bench_server.erl +++ b/src/httpc_bench_server.erl @@ -76,6 +76,7 @@ loop(Socket, Buffer) -> "Date: Tue, 07 Mar 2017 01:10:09 GMT\r\n", "Content-Length: 12\r\n\r\n", "httpc_bench!">> || _ <- lists:seq(1, N)], + timer:sleep(rand:uniform(300) + 1), case gen_tcp:send(Socket, Responses) of ok -> loop(Socket, Buffer2); From 5917e43b18ec00a3626cffb24ae2254f1836f201 Mon Sep 17 00:00:00 2001 From: zhouzb Date: Sat, 9 Apr 2022 13:13:08 +0800 Subject: [PATCH 2/4] chore: fix server delay and add more print info --- rebar.lock | 68 -------------------------------------- src/httpc_bench.erl | 16 ++++----- src/httpc_bench_server.erl | 3 +- 3 files changed, 10 insertions(+), 77 deletions(-) delete mode 100644 rebar.lock diff --git a/rebar.lock b/rebar.lock deleted file mode 100644 index 2610f14..0000000 --- a/rebar.lock +++ /dev/null @@ -1,68 +0,0 @@ -{"1.1.0", -[{<<"bear">>, - {git,"https://github.com/boundary/bear.git", - {ref,"119234548783af19b8ec75c879c5062676b92571"}}, - 1}, - {<<"buoy">>, - {git,"https://github.com/lpgauth/buoy.git", - {ref,"7c49e2d5ec6b35625004fb8842342ee28641e864"}}, - 0}, - {<<"certifi">>,{pkg,<<"certifi">>,<<"0.7.0">>},1}, - {<<"cowlib">>,{pkg,<<"cowlib">>,<<"1.0.0">>},1}, - {<<"dispcount">>, - {git,"https://github.com/ferd/dispcount.git", - {ref,"f466aecdb8ea2dda0996af08c3f2683dae612aed"}}, - 1}, - {<<"dlhttpc">>, - {git,"https://github.com/lpgauth/dlhttpc.git", - {ref,"ca9664358f3d7e63b58cda48545a57c44189a16b"}}, - 0}, - {<<"edown">>, - {git,"https://github.com/uwiger/edown.git", - {ref,"b7c8eb0ac1859f8fce11cbfe3526f5ec83194776"}}, - 2}, - {<<"fqc">>, - {git,"https://github.com/project-fifo/fqc.git", - {ref,"ad62f8121414ed026a6a9f564b1856191ab7d1fc"}}, - 2}, - {<<"gproc">>,{pkg,<<"gproc">>,<<"0.5.0">>},1}, - {<<"granderl">>,{pkg,<<"granderl">>,<<"0.1.5">>},2}, - {<<"hackney">>, - {git,"https://github.com/benoitc/hackney.git", - {ref,"b3814c981cb600211f0b1b0e133c4dbffc8299bc"}}, - 0}, - {<<"hdr_histogram">>, - {git,"https://github.com/HdrHistogram/hdr_histogram_erl.git", - {ref,"46a4f86f3a9b72181859c2a30c8039c5d486248b"}}, - 1}, - {<<"ibrowse">>, - {git,"https://github.com/cmullaparthi/ibrowse.git", - {ref,"7529807170cf3c8f6a8c5c95526e670420ff591b"}}, - 0}, - {<<"idna">>,{pkg,<<"idna">>,<<"1.2.0">>},1}, - {<<"katipo">>, - {git,"https://github.com/puzza007/katipo.git", - {ref,"40660e346d29fec133087ff64e87552b0de19da1"}}, - 0}, - {<<"metal">>,{pkg,<<"metal">>,<<"0.1.0">>},2}, - {<<"metrics">>,{pkg,<<"metrics">>,<<"1.1.0">>},1}, - {<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.0.2">>},1}, - {<<"shackle">>,{pkg,<<"shackle">>,<<"0.5.0">>},1}, - {<<"ssl_verify_fun">>,{pkg,<<"ssl_verify_fun">>,<<"1.1.1">>},1}, - {<<"timing">>, - {git,"https://github.com/lpgauth/timing.git", - {ref,"8a3c0320692fe48a266b29854e4e27b99f6a7a95"}}, - 0}]}. -[ -{pkg_hash,[ - {<<"certifi">>, <<"861A57F3808F7EB0C2D1802AFEAAE0FA5DE813B0DF0979153CBAFCD853ABABAF">>}, - {<<"cowlib">>, <<"397D890D669E56D486B0B5329973AD1A07012412BC110D34A737698DD6941741">>}, - {<<"gproc">>, <<"2DF2D886F8F8A7B81A4B04AA17972B5965BBC5BF0100EA6D8E8AC6A0E7389AFE">>}, - {<<"granderl">>, <<"F20077A68BD80B8D8783BD15A052813C6483771DEC1A5B837D307CBE92F14122">>}, - {<<"idna">>, <<"AC62EE99DA068F43C50DC69ACF700E03A62A348360126260E87F2B54ECED86B2">>}, - {<<"metal">>, <<"168E0AB553E3B8144547E353085CEDAA43C8769E0B7E43C0146C99C2C33507AC">>}, - {<<"metrics">>, <<"41450DFFBA18B1206BF44FA1AD4E29324D1E561617C2DAF4EF32E3CE50DF08F3">>}, - {<<"mimerl">>, <<"993F9B0E084083405ED8252B99460C4F0563E41729AB42D9074FD5E52439BE88">>}, - {<<"shackle">>, <<"1C90E21A8181D4750BDEBF203F6A7412659A4C04947591FBE71797B89BEC9600">>}, - {<<"ssl_verify_fun">>, <<"28A4D65B7F59893BC2C7DE786DEC1E1555BD742D336043FE644AE956C3497FBE">>}]} -]. diff --git a/src/httpc_bench.erl b/src/httpc_bench.erl index 3c58e17..746974b 100644 --- a/src/httpc_bench.erl +++ b/src/httpc_bench.erl @@ -14,11 +14,11 @@ % httpc_bench_httpc ]). -% -define(CONCURENCIES, [32, 64, 128, 512, 2048, 4096]). -% -define(POOL_SIZES, [8, 16, 32, 64, 128, 256]). +-define(CONCURENCIES, [10240]). +-define(POOL_SIZES, [1]). --define(CONCURENCIES, [20480]). --define(POOL_SIZES, [16]). +% -define(CONCURENCIES, [20480]). +% -define(POOL_SIZES, [16]). % -define(CONCURENCIES, [10240]). % -define(POOL_SIZES, [1024]). @@ -27,8 +27,8 @@ run() -> error_logger:tty(false), io:format("Running benchmark...~n~n" ++ - "Client PoolSize Concurency Requests/s Error %~n" ++ - [$= || _ <- lists:seq(1, 49)] ++ "~n", []), + "Client PoolSize Concurency Requests/s Error % Success Totol Time(s)~n" ++ + [$= || _ <- lists:seq(1, 71)] ++ "~n", []), run_client(?CLIENTS, ?POOL_SIZES, ?CONCURENCIES, ?N). %% private @@ -69,7 +69,7 @@ run_concurency(Client, PoolSize, [Concurency | T], N) -> ]), Qps = lookup(success, Results) / (lookup(total_time, Results) / 1000000), Errors = lookup(errors, Results) / lookup(iterations, Results) * 100, - io:format("~-8s ~7B ~11B ~11B ~8.1f~n", - [Client2, PoolSize, Concurency, trunc(Qps), Errors]), + io:format("~-8s ~7B ~11B ~11B ~8.1f ~8B ~12.2f~n", + [Client2, PoolSize, Concurency, trunc(Qps), Errors, lookup(success, Results), (lookup(total_time, Results) / 1000000)]), Client:stop(), run_concurency(Client, PoolSize, T, N). diff --git a/src/httpc_bench_server.erl b/src/httpc_bench_server.erl index 9e071c5..f6db367 100644 --- a/src/httpc_bench_server.erl +++ b/src/httpc_bench_server.erl @@ -76,7 +76,8 @@ loop(Socket, Buffer) -> "Date: Tue, 07 Mar 2017 01:10:09 GMT\r\n", "Content-Length: 12\r\n\r\n", "httpc_bench!">> || _ <- lists:seq(1, N)], - timer:sleep(rand:uniform(300) + 1), + timer:sleep(10), + % timer:sleep(rand:uniform(300) + 1), case gen_tcp:send(Socket, Responses) of ok -> loop(Socket, Buffer2); From 73f917fb4d634071e24af1ce580f86d52f94a8a0 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Sat, 9 Apr 2022 10:41:08 +0200 Subject: [PATCH 3/4] Increase timeout to 5 minutes To get 100% success --- include/httpc_bench.hrl | 2 +- src/httpc_bench.erl | 8 ++++++-- src/httpc_bench_ehttpc.erl | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/httpc_bench.hrl b/include/httpc_bench.hrl index 7ce82a1..b8ebac7 100644 --- a/include/httpc_bench.hrl +++ b/include/httpc_bench.hrl @@ -5,5 +5,5 @@ -define(PIPELINING, 1024). -define(PATH, <<"/">>). -define(PORT, 8080). --define(TIMEOUT, 12000). +-define(TIMEOUT, 300_000). -define(URL, <<"http://127.0.0.1:8080/">>). diff --git a/src/httpc_bench.erl b/src/httpc_bench.erl index 746974b..819e530 100644 --- a/src/httpc_bench.erl +++ b/src/httpc_bench.erl @@ -5,7 +5,7 @@ run/0 ]). --define(N, 50000). +-define(N, 10240). -define(CLIENTS, [ % httpc_bench_buoy, @@ -29,12 +29,16 @@ run() -> io:format("Running benchmark...~n~n" ++ "Client PoolSize Concurency Requests/s Error % Success Totol Time(s)~n" ++ [$= || _ <- lists:seq(1, 71)] ++ "~n", []), + case lists:any(fun(C) -> ?N < C end, ?CONCURENCIES) of + true -> error(bad_iteration); + false -> ok + end, run_client(?CLIENTS, ?POOL_SIZES, ?CONCURENCIES, ?N). %% private lookup(Key, List) -> case lists:keyfind(Key, 1, List) of - false -> undefined; + false -> error({bad_lookup_key, Key}); {_, Value} -> Value end. diff --git a/src/httpc_bench_ehttpc.erl b/src/httpc_bench_ehttpc.erl index a2f5703..4a25b53 100644 --- a/src/httpc_bench_ehttpc.erl +++ b/src/httpc_bench_ehttpc.erl @@ -26,7 +26,7 @@ start(PoolSize) -> {pool_size, PoolSize}, {pool_type, random}, {connect_timeout, 5000}, - {retry, 5}, + {retry, 0}, {retry_timeout, 1000} ], {ok, _} = ehttpc_sup:start_pool(httpc_bench, PoolOpts). From e8442b14d444d7e2a684673407bc996b8629e08b Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Sat, 9 Apr 2022 11:15:03 +0200 Subject: [PATCH 4/4] Server sleep per request 10 ms by default --- src/httpc_bench_server.erl | 51 +++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/httpc_bench_server.erl b/src/httpc_bench_server.erl index f6db367..b13311c 100644 --- a/src/httpc_bench_server.erl +++ b/src/httpc_bench_server.erl @@ -13,10 +13,11 @@ start() -> case get(?MODULE) of undefined -> - io:format("Server started...~n", []), + Sleep = sleep(), + io:format("HTTP Server started on port ~p, sleep=~p...~n", [?PORT, Sleep]), {ok, LSocket} = listen(), put(?MODULE, LSocket), - spawn(fun () -> accept(LSocket) end), + spawn(fun () -> accept(LSocket, Sleep) end), ok; _LSocket -> {error, already_started} @@ -33,15 +34,26 @@ stop() -> end. %% private -accept(LSocket) -> +accept(LSocket, Sleep) -> case gen_tcp:accept(LSocket) of {ok, Socket} -> - spawn(fun() -> loop(Socket, <<>>) end), - accept(LSocket); + spawn_link( + fun() -> + io:format(user, "started one server process ~p, sleep=~p\n", [Socket, Sleep]), + loop(Socket, Sleep, <<>>) + end), + accept(LSocket, Sleep); {error, _Reason} -> ok end. +sleep() -> + case os:getenv("SLEEP_PER_REQ") of + false -> 10; + "" -> 10; + N -> list_to_integer(N) + end. + count_requests([<<>>], N) -> {N, <<>>}; count_requests([Buffer], N) -> @@ -61,10 +73,13 @@ listen() -> end), receive {ok, LSocket} -> - {ok, LSocket} + {ok, LSocket}; + Other -> + io:format(user, "unexpected listen result: ~p\n", [Other]), + exit(kill) end. -loop(Socket, Buffer) -> +loop(Socket, Sleep, Buffer) -> case gen_tcp:recv(Socket, 0) of {ok, Data} -> Split = binary:split(<>, @@ -76,14 +91,16 @@ loop(Socket, Buffer) -> "Date: Tue, 07 Mar 2017 01:10:09 GMT\r\n", "Content-Length: 12\r\n\r\n", "httpc_bench!">> || _ <- lists:seq(1, N)], - timer:sleep(10), - % timer:sleep(rand:uniform(300) + 1), - case gen_tcp:send(Socket, Responses) of - ok -> - loop(Socket, Buffer2); - {error, _Reason} -> - ok - end; - {error, _Reason} -> - ok + lists:foreach( + fun(R) -> + case gen_tcp:send(Socket, R) of + ok -> ok; + {error, _} -> exit(normal) + end, + timer:sleep(Sleep) + end, Responses), + loop(Socket, Sleep, Buffer2); + {error, Reason} -> + io:format(user, "stopped one server process ~p: ~0p\n", [Socket, Reason]), + exit(normal) end.