syn_benchmark.erl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_benchmark).
  27. %% API
  28. -export([start/0]).
  29. -export([process_loop/0]).
  30. -export([collection_loop/2]).
  31. -export([run_test_on_node/3]).
  32. -export([register_pids/1]).
  33. -export([unregister_pids/1]).
  34. -export([wait_for_unregistered/1]).
  35. %% macros
  36. -define(MAX_RETRIEVE_WAITING_TIME, 60000).
  37. %% ===================================================================
  38. %% API
  39. %% ===================================================================
  40. start() ->
  41. io:format("-----> Starting benchmark on node ~s~n", [node()]),
  42. %% init
  43. ConfigFilePath = filename:join([filename:dirname(code:which(?MODULE)), "syn_benchmark.config"]),
  44. {ok, BenchConfig} = file:consult(ConfigFilePath),
  45. RemoteHosts = proplists:get_value(remote_nodes, BenchConfig, []),
  46. ProcessCount = list_to_integer(os:getenv("SYN_PROCESS_COUNT", "100000")),
  47. %% start nodes
  48. lists:foreach(fun(RemoteHost) ->
  49. io:format("-----> Starting slave node ~s@~s~n", [maps:get(node, RemoteHost), maps:get(host, RemoteHost)]),
  50. {ok, _} = syn_test_suite_helper:start_slave(
  51. maps:get(node, RemoteHost),
  52. maps:get(host, RemoteHost),
  53. maps:get(user, RemoteHost),
  54. maps:get(pass, RemoteHost)
  55. )
  56. end, RemoteHosts),
  57. Nodes = [node() | nodes()],
  58. io:format("-----> Started ~p nodes: ~p~n", [length(Nodes), Nodes]),
  59. %% start syn everywhere
  60. lists:foreach(fun(Node) ->
  61. ok = rpc:call(Node, syn, start, [])
  62. end, Nodes),
  63. timer:sleep(1000),
  64. %% compute names per node
  65. ProcessesPerNode = round(ProcessCount / length(Nodes)),
  66. io:format("-----> ~p processes per node~n", [ProcessesPerNode]),
  67. %% launch test everywhere
  68. CollectingPid = self(),
  69. lists:foldl(fun(Node, Acc) ->
  70. FromName = Acc * ProcessesPerNode + 1,
  71. ToName = FromName + ProcessesPerNode - 1,
  72. rpc:cast(Node, ?MODULE, run_test_on_node, [CollectingPid, FromName, ToName]),
  73. Acc + 1
  74. end, 0, Nodes),
  75. Results = collection_loop(Nodes, []),
  76. io:format("-----> Results: ~p~n", [Results]),
  77. %% stop syn everywhere
  78. lists:foreach(fun(Node) ->
  79. ok = rpc:call(Node, syn, stop, [])
  80. end, [node() | nodes()]),
  81. timer:sleep(1000),
  82. %% stop nodes
  83. lists:foreach(fun(SlaveNode) ->
  84. syn_test_suite_helper:connect_node(SlaveNode),
  85. ShortName = list_to_atom(lists:nth(1, string:split(atom_to_list(SlaveNode), "@"))),
  86. syn_test_suite_helper:stop_slave(ShortName)
  87. end, nodes()),
  88. io:format("-----> Stopped ~p nodes: ~p~n", [length(Nodes), Nodes]),
  89. %% stop node
  90. init:stop().
  91. run_test_on_node(CollectingPid, FromName, ToName) ->
  92. %% launch processes - list is in format {Name, pid()}
  93. PidTuples = [{Name, spawn(?MODULE, process_loop, [])} || Name <- lists:seq(FromName, ToName)],
  94. {ToName, ToPid} = lists:last(PidTuples),
  95. %% register
  96. {TimeReg0, _} = timer:tc(?MODULE, register_pids, [PidTuples]),
  97. RegisteringRate1 = length(PidTuples) / TimeReg0 * 1000000,
  98. %% check
  99. ToPid = syn:whereis(ToName),
  100. %% unregister
  101. {TimeReg1, _} = timer:tc(?MODULE, unregister_pids, [PidTuples]),
  102. UnRegisteringRate1 = length(PidTuples) / TimeReg1 * 1000000,
  103. %% check
  104. undefined = syn:whereis(ToName),
  105. %% re-register
  106. {TimeReg3, _} = timer:tc(?MODULE, register_pids, [PidTuples]),
  107. RegisteringRate2 = length(PidTuples) / TimeReg3 * 1000000,
  108. %% check
  109. ToPid = syn:whereis(ToName),
  110. %% kill all
  111. lists:foreach(fun({_Name, Pid}) ->
  112. exit(Pid, kill)
  113. end, PidTuples),
  114. %% check all unregistered
  115. {TimeReg4, _} = timer:tc(?MODULE, wait_for_unregistered, [ToName]),
  116. CheckAllKilled = TimeReg4 / 1000000,
  117. %% return
  118. CollectingPid ! {node(), [
  119. {registering_rate_1, RegisteringRate1},
  120. {unregistering_rate_1, UnRegisteringRate1},
  121. {registering_rate_2, RegisteringRate2},
  122. {check_all_killed, CheckAllKilled}
  123. ]}.
  124. %% ===================================================================
  125. %% Internal
  126. %% ===================================================================
  127. collection_loop([], Results) -> Results;
  128. collection_loop(Nodes, Results) ->
  129. receive
  130. {Node, Result} ->
  131. collection_loop(lists:delete(Node, Nodes), [{Node, Result} | Results])
  132. after ?MAX_RETRIEVE_WAITING_TIME ->
  133. io:format("COULD NOT COMPLETE TEST IN ~p seconds", [?MAX_RETRIEVE_WAITING_TIME])
  134. end.
  135. process_loop() ->
  136. receive
  137. _ -> ok
  138. end.
  139. register_pids([]) -> ok;
  140. register_pids([{Name, Pid} | TPidTuples]) ->
  141. ok = syn:register(Name, Pid),
  142. register_pids(TPidTuples).
  143. wait_for_unregistered(ToName) ->
  144. case syn:whereis(ToName) of
  145. undefined ->
  146. ok;
  147. _ ->
  148. timer:sleep(50),
  149. wait_for_unregistered(ToName)
  150. end.
  151. unregister_pids([]) -> ok;
  152. unregister_pids([{Name, _Pid} | TPidTuples]) ->
  153. ok = syn:unregister(Name),
  154. unregister_pids(TPidTuples).