syn_benchmark.erl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. %% init
  42. NodeCount = list_to_integer(os:getenv("SYN_BENCH_NODE_COUNT", "2")),
  43. ProcessCount = list_to_integer(os:getenv("SYN_PROCESS_COUNT", "100000")),
  44. %% start nodes
  45. lists:foreach(fun(Count) ->
  46. ShortName = list_to_atom("syn_bench_slave_" ++ integer_to_list(Count)),
  47. {ok, _} = syn_test_suite_helper:start_slave(ShortName)
  48. end, lists:seq(1, NodeCount - 1)),
  49. Nodes = [node() | nodes()],
  50. io:format("-----> Started ~p nodes: ~p~n", [length(Nodes), Nodes]),
  51. %% start syn everywhere
  52. lists:foreach(fun(Node) ->
  53. ok = rpc:call(Node, syn, start, [])
  54. end, Nodes),
  55. timer:sleep(1000),
  56. %% compute names per node
  57. ProcessesPerNode = round(ProcessCount / length(Nodes)),
  58. io:format("-----> ~p processes per node~n", [ProcessesPerNode]),
  59. %% launch test everywhere
  60. CollectingPid = self(),
  61. lists:foldl(fun(Node, Acc) ->
  62. FromName = Acc * ProcessesPerNode + 1,
  63. ToName = FromName + ProcessesPerNode - 1,
  64. rpc:cast(Node, ?MODULE, run_test_on_node, [CollectingPid, FromName, ToName]),
  65. Acc + 1
  66. end, 0, Nodes),
  67. Results = collection_loop(Nodes, []),
  68. io:format("-----> Results: ~p~n", [Results]),
  69. %% stop syn everywhere
  70. lists:foreach(fun(Node) ->
  71. ok = rpc:call(Node, syn, stop, [])
  72. end, [node() | nodes()]),
  73. timer:sleep(1000),
  74. %% stop nodes
  75. lists:foreach(fun(SlaveNode) ->
  76. syn_test_suite_helper:connect_node(SlaveNode),
  77. ShortName = list_to_atom(lists:nth(1, string:split(atom_to_list(SlaveNode), "@"))),
  78. syn_test_suite_helper:stop_slave(ShortName)
  79. end, nodes()),
  80. io:format("-----> Stopped ~p nodes: ~p~n", [length(Nodes), Nodes]),
  81. %% stop node
  82. init:stop().
  83. run_test_on_node(CollectingPid, FromName, ToName) ->
  84. %% launch processes - list is in format {Name, pid()}
  85. PidTuples = [{Name, spawn(?MODULE, process_loop, [])} || Name <- lists:seq(FromName, ToName)],
  86. {ToName, ToPid} = lists:last(PidTuples),
  87. %% register
  88. {TimeReg0, _} = timer:tc(?MODULE, register_pids, [PidTuples]),
  89. RegisteringRate1 = length(PidTuples) / TimeReg0 * 1000000,
  90. %% check
  91. ToPid = syn:whereis(ToName),
  92. %% unregister
  93. {TimeReg1, _} = timer:tc(?MODULE, unregister_pids, [PidTuples]),
  94. UnRegisteringRate1 = length(PidTuples) / TimeReg1 * 1000000,
  95. %% check
  96. undefined = syn:whereis(ToName),
  97. %% re-register
  98. {TimeReg3, _} = timer:tc(?MODULE, register_pids, [PidTuples]),
  99. RegisteringRate2 = length(PidTuples) / TimeReg3 * 1000000,
  100. %% check
  101. ToPid = syn:whereis(ToName),
  102. %% kill all
  103. lists:foreach(fun({_Name, Pid}) ->
  104. exit(Pid, kill)
  105. end, PidTuples),
  106. %% check all unregistered
  107. {TimeReg4, _} = timer:tc(?MODULE, wait_for_unregistered, [ToName]),
  108. CheckAllKilled = TimeReg4 / 1000000,
  109. %% return
  110. CollectingPid ! {node(), [
  111. {registering_rate_1, RegisteringRate1},
  112. {unregistering_rate_1, UnRegisteringRate1},
  113. {registering_rate_2, RegisteringRate2},
  114. {check_all_killed, CheckAllKilled}
  115. ]}.
  116. %% ===================================================================
  117. %% Internal
  118. %% ===================================================================
  119. collection_loop([], Results) -> Results;
  120. collection_loop(Nodes, Results) ->
  121. receive
  122. {Node, Result} ->
  123. collection_loop(lists:delete(Node, Nodes), [{Node, Result} | Results])
  124. after ?MAX_RETRIEVE_WAITING_TIME ->
  125. io:format("COULD NOT COMPLETE TEST IN ~p seconds", [?MAX_RETRIEVE_WAITING_TIME])
  126. end.
  127. process_loop() ->
  128. receive
  129. _ -> ok
  130. end.
  131. register_pids([]) -> ok;
  132. register_pids([{Name, Pid} | TPidTuples]) ->
  133. ok = syn:register(Name, Pid),
  134. register_pids(TPidTuples).
  135. wait_for_unregistered(ToName) ->
  136. case syn:whereis(ToName) of
  137. undefined ->
  138. ok;
  139. _ ->
  140. timer:sleep(50),
  141. wait_for_unregistered(ToName)
  142. end.
  143. unregister_pids([]) -> ok;
  144. unregister_pids([{Name, _Pid} | TPidTuples]) ->
  145. ok = syn:unregister(Name),
  146. unregister_pids(TPidTuples).