summaryrefslogtreecommitdiff
path: root/src/gm_speed_test.erl
blob: fa515fa8fe3c7ef7f4e5af33bb640105726d1823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2014 GoPivotal, Inc.  All rights reserved.
%%

-module(gm_speed_test).

-export([test/3]).
-export([joined/2, members_changed/4, handle_msg/3, terminate/2]).
-export([wile_e_coyote/2]).

-behaviour(gm).

-include("gm_specs.hrl").

%% callbacks

joined(Owner, _Members) ->
    Owner ! joined,
    ok.

members_changed(_Owner, _Births, _Deaths, _Live) ->
    ok.

handle_msg(Owner, _From, ping) ->
    Owner ! ping,
    ok.

terminate(Owner, _Reason) ->
    Owner ! terminated,
    ok.

%% other

wile_e_coyote(Time, WriteUnit) ->
    {ok, Pid} = gm:start_link(?MODULE, ?MODULE, self(),
                              fun rabbit_misc:execute_mnesia_transaction/1),
    receive joined -> ok end,
    timer:sleep(1000), %% wait for all to join
    timer:send_after(Time, stop),
    Start = now(),
    {Sent, Received} = loop(Pid, WriteUnit, 0, 0),
    End = now(),
    ok = gm:leave(Pid),
    receive terminated -> ok end,
    Elapsed = timer:now_diff(End, Start) / 1000000,
    io:format("Sending rate:   ~p msgs/sec~nReceiving rate: ~p msgs/sec~n~n",
              [Sent/Elapsed, Received/Elapsed]),
    ok.

loop(Pid, WriteUnit, Sent, Received) ->
    case read(Received) of
        {stop, Received1} -> {Sent, Received1};
        {ok,   Received1} -> ok = write(Pid, WriteUnit),
                             loop(Pid, WriteUnit, Sent + WriteUnit, Received1)
    end.

read(Count) ->
    receive
        ping -> read(Count + 1);
        stop -> {stop, Count}
    after 5 ->
            {ok, Count}
    end.

write(_Pid, 0) -> ok;
write(Pid,  N) -> ok = gm:broadcast(Pid, ping),
                  write(Pid, N - 1).

test(Time, WriteUnit, Nodes) ->
    ok = gm:create_tables(),
    [spawn(Node, ?MODULE, wile_e_coyote, [Time, WriteUnit]) || Node <- Nodes].