Last active
September 7, 2023 15:30
-
-
Save nickva/cbfcf421e18964d6520161ede671563c to your computer and use it in GitHub Desktop.
Ping-pong across dist with N parallel workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% | |
% $ erl -name [email protected] | |
% | |
% $ erl -name [email protected] | |
% ([email protected])2> dist_perf:go('[email protected]'). | |
-module(distbench). | |
-export([go/1, go/2, go/3, stop/0, gc/0, stats/1]). | |
go(RemoteNode) -> | |
go(RemoteNode, 100). | |
go(RemoteNode, N) -> | |
Payload = doc(), %[<<0:100000/unit:8>> || _ <- lists:seq(1, 10)], | |
go(RemoteNode, N, Payload). | |
go(RemoteNode, N, Payload) when is_atom(RemoteNode), is_integer(N) -> | |
stop(), | |
StatsPid = spawn_link(?MODULE, stats, [RemoteNode]), | |
Pids = [spawn_link(RemoteNode, fun | |
F() -> | |
receive {From, Msg} -> | |
From ! term_to_binary(Msg), | |
F() | |
end | |
end | |
) || _<- lists:seq(1, N)], | |
Pinger = fun | |
F(Pid) -> | |
Msg = {self(), Payload}, | |
SendRes = erlang:send(Pid, Msg, [nosuspend]), | |
case SendRes of | |
ok -> | |
receive _Res -> F(Pid) end; | |
nosuspend -> | |
erlang:send(Pid, Msg), | |
receive _Res -> F(Pid) end | |
end | |
end, | |
Ctx = {StatsPid, [{Pid, spawn_link(fun() -> Pinger(Pid) end)} || Pid <- Pids]}, | |
put(dist_perf, Ctx), | |
started. | |
stop() -> | |
case erase(dist_perf) of | |
undefined -> | |
ok; | |
{StatsPid, Pids} -> | |
[begin unlink(Pid1), exit(Pid1, kill), unlink(Pid2), exit(Pid2, kill) end || {Pid1, Pid2} <- Pids], | |
unlink(StatsPid), | |
exit(StatsPid, kill) | |
end. | |
gc() -> | |
[erlang:garbage_collect(Pid) || Pid<-processes()], | |
ok. | |
stats(RemoteNode) -> | |
{_, {output, T0Out}} = erlang:statistics(io), | |
timer:sleep(5000), | |
{_, {output, T1Out}} = erlang:statistics(io), | |
SentMBs = round((T1Out - T0Out) / 1024 / 1024 / 5), | |
{Res, PingDt} = ping(RemoteNode), | |
io:format(" * Sent:~pMB/s, ping:~p ~p (usec)~n", [SentMBs, Res, PingDt]), | |
stats(RemoteNode). | |
ping(RemoteNode) -> | |
T0 = erlang:monotonic_time(), | |
Res = net_adm:ping(RemoteNode), | |
Dt = erlang:monotonic_time() - T0, | |
DtMicro = erlang:convert_time_unit(Dt, native, microsecond), | |
{Res, DtMicro}. | |
doc() -> | |
{ | |
doc, % record name | |
<<"adfasdfasfasdfa213445234523452345234sdfasdfasfasfasfasf">>, % id | |
revs(1000), % revlist | |
doc_body() % doc body | |
}. | |
revs(N) -> | |
{5000, [<<171,144,86,232,21,225,82,74,142,26,167,126,41,128,68,131>> || _ <- lists:seq(1, N)]}. | |
doc_body() -> | |
% from random json generator site | |
{[{<<"index">>,0}, | |
{<<"guid">>,<<"71a3d143-5474-4afb-92ec-17c0adcde768">>}, | |
{<<"isActive">>,false}, | |
{<<"balance">>,<<"$3,762.60">>}, | |
{<<"picture">>,<<"http://placehold.it/32x32">>}, | |
{<<"age">>,45}, | |
{<<"eyeColor">>,<<"green">>}, | |
{<<"name">>,<<"Myrna Houston">>}, | |
{<<"gender">>,<<"female">>}, | |
{<<"company">>,<<"DEMINIMUM">>}, | |
{<<"email">>,<<"[email protected]">>}, | |
{<<"phone">>,<<"+1 (952) 563-3195">>}, | |
{<<"address">>, | |
<<"599 Crystal Street, Grenelefe, Federated States Of Micronesia, 6317">>}, | |
{<<"about">>, | |
<<"Nostrud ea Lorem deserunt ea in enim cupidatat est dolor consectetur aute tempor cupidatat. Ut ut do laborum laborum culpa elit ea anim ea. Ex ea officia minim irure quis laborum eu eu.\r\n">>}, | |
{<<"registered">>,<<"2020-01-11T08:52:49 +05:00">>}, | |
{<<"latitude">>,-11.888546}, | |
{<<"longitude">>,119.340239}, | |
{<<"tags">>, | |
[<<"consectetur eiusmod cupidatat do proident reprehenderit ipsum tempor officia non">>, | |
<<"esse occaecat irure commodo velit cillum ut labore commodo sint">>, | |
<<"officia sunt sit qui tempor non adipisicing officia enim ex">>, | |
<<"sunt qui nulla amet exercitation ad labore cillum in nostrud">>, | |
<<"ullamco deserunt duis occaecat laborum veniam labore ex consectetur esse">>]}, | |
{<<"friends">>, | |
[{[{<<"id">>,0},{<<"name">>,<<"Mcbride Marks">>}]}, | |
{[{<<"id">>,1},{<<"name">>,<<"Anthony Wilkinson">>}]}, | |
{[{<<"id">>,2},{<<"name">>,<<"Amy Whitley">>}]}, | |
{[{<<"id">>,3},{<<"name">>,<<"Alston Thomas">>}]}, | |
{[{<<"id">>,4},{<<"name">>,<<"Lydia Kane">>}]}]}, | |
{<<"greeting">>, | |
<<"Hello, Myrna Houston! You have 4 unread messages.">>}, | |
{<<"favoriteFruit">>,<<"apple">>}]}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment