Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. -module(mnesia_transform_benchmark).
  2. %% API
  3. -export([start/0]).
  4. -export([benchmark/0]).
  5.  
  6. %%HOW TO TEST TRANSFORM
  7. %%
  8. %% 1. Compile
  9. %% erlc mnesia_transform_benchmark.erl
  10. %%
  11. %% 2. Start 3 nodes
  12. %% 2.1. $> erl -name 'alice@127.0.0.1' -setcookie z
  13. %% 2.2. $> erl -name 'bob@127.0.0.1' -setcookie z
  14. %% 2.3. $> erl -name 'wow@127.0.0.1' -setcookie z
  15. %%
  16. %% 3. Start mnesia and import 10000 items on each node.
  17. %% 3.1 (alice@127.0.0.1)1> mnesia_transform_benchmark:start().
  18. %% 3.2 (bob@127.0.0.1)1> mnesia_transform_benchmark:start().
  19. %% 3.3 (wow@127.0.0.1)1> mnesia_transform_benchmark:start().
  20. %%
  21. %% 4. Run transform in one of nodes
  22. %% (any_node@127.0.0.1)2> mnesia_transform_benchmark:benchmark().
  23. %%
  24. %% 5. No step 5
  25.  
  26. -record(mqtt_client, {client_id,
  27. node,
  28. pid,
  29. ip,
  30. user_id,
  31. user_type,
  32. monitor,
  33. online_time,
  34. clean_sess
  35. }).
  36.  
  37. -define(CLIENT, mqtt_client).
  38.  
  39. start() ->
  40. ensure_connect_node(),
  41. ensure_mnesia_tables(),
  42. create_clients(10000).
  43.  
  44. benchmark() ->
  45. print_mnesia_info(before_transform),
  46. {Time, Result} = timer:tc(fun() -> do_transform() end),
  47. io:format("Cost Time:~p ms ~p ~n ", [Time/1000, Result]),
  48. print_mnesia_info(after_transform),
  49. ok.
  50.  
  51. %% local function
  52. do_transform() ->
  53. Fun = fun({mqtt_client, ClientId, Node, Pid, Ip, UserId, UserType, Monitor, OnlineTime, CleanSess}) ->
  54. {mqtt_client, ClientId, Node, Pid, Ip, UserId, UserType, Monitor, OnlineTime, CleanSess, <<>>} end,
  55. AttrList = [client_id, node, pid, ip, user_id, user_type, monitor, online_time, clean_sess, scope_id],
  56. mnesia:transform_table(?CLIENT, Fun, AttrList).
  57.  
  58. print_mnesia_info(Type) ->
  59. io:format("------------------------------------------------------~n~n"),
  60. io:format("~p mnesia:info~n", [Type]),
  61. mnesia:info(),
  62. io:format("~p table: info~n ~p~n", [Type, mnesia:table_info(mqtt_client, all)]),
  63. io:format("------------------------------------------------------~n~n").
  64.  
  65. ensure_connect_node() ->
  66. Nodes = ['alice@127.0.0.1', 'bob@127.0.0.1', 'wow@127.0.0.1'],
  67. [net_kernel:connect_node(Node) || Node <- Nodes],
  68. ok.
  69.  
  70. ensure_mnesia_tables() ->
  71. mnesia:start(),
  72. {ok, _} = mnesia:change_config(extra_db_nodes, nodes()),
  73. Attrs = record_info(fields, ?CLIENT),
  74. init_tables(?CLIENT, [user_id, pid, node], Attrs).
  75.  
  76. init_tables(Table, IndexList, Attrs) ->
  77. ExistingTables = mnesia:system_info(tables),
  78. MyNode = erlang:node(),
  79. CopyType = ram_copies,
  80. case lists:member(Table, ExistingTables) of
  81. false ->
  82. {atomic,ok} = mnesia:create_table(Table, [{attributes, Attrs}, {CopyType, [MyNode]}, {index, IndexList}, {majority, true}]);
  83. true ->
  84. TableNodes = mnesia:table_info(Table, all_nodes),
  85. case lists:member(MyNode, TableNodes) of
  86. false -> mnesia:add_table_copy(Table, MyNode, CopyType);
  87. true -> do_nothing
  88. end
  89. end.
  90.  
  91. create_clients(Num)when is_integer(Num) ->
  92. Fun =
  93. fun() ->
  94. Pid = self(),
  95. [begin
  96. Client =
  97. #mqtt_client{client_id = erlang:make_ref(),
  98. node = node(),
  99. pid = Pid,
  100. user_id = Index,
  101. monitor = Pid,
  102. online_time = erlang:localtime(),
  103. clean_sess = true},
  104. mnesia:write(Client)
  105. end|| Index <-lists:seq(1, Num)],
  106. ok
  107. end,
  108. {atomic, ok} = mnesia:transaction(Fun).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement