-
Notifications
You must be signed in to change notification settings - Fork 0
/
prisma_test_server.erl
242 lines (204 loc) · 10.2 KB
/
prisma_test_server.erl
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% Copyright 2011 Ole Rixmann. %%
%% %%
%% This file is part of PRISMA-Aggregator. %%
%% %%
%% PRISMA-Aggregator is free software: you can redistribute it and/or %%
%% modify it under the terms of the GNU Affero General Public License %%
%% as published by the Free Software Foundation, either version 3 of %%
%% the License, or (at your option) any later version. %%
%% %%
%% PRISMA-Aggregator is distributed in the hope that it will be useful, %%
%% but WITHOUT ANY WARRANTY; without even the implied warranty of %%
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. %%
%% See the GNU Affero General Public License for more details. %%
%% %%
%% You should have received a copy of the GNU Affero General Public License %%
%% along with PRISMA-Aggregator. If not, see <https://www.gnu.org/licenses/>. %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(prisma_test_server).
-behaviour(gen_server).
-include("prisma_aggregator.hrl").
-include("jlib.hrl").
%% API
-export([start_link/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([message_received/1,
error_received/2,
start_test/5,
stop_test/0,
overload_received/0,
start_overload_and_recover/3]).
-record(state, {test,
error_count,
message_count,
device,
walltime_init}).
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
message_received(Message) ->
gen_server:cast(?MODULE, {message_received, Message}).
error_received(Error, From) ->
gen_server:cast(?MODULE, {error_received, {Error, From}}).
overload_received() ->
gen_server:cast(?MODULE, overload).
start_test(Aggregator, Server, Params, Startport, PCount) ->
gen_server:cast(?MODULE, {run_test, {Aggregator, Server, Params, Startport, PCount}, 0}).
start_overload_and_recover(From, To, Rate) ->
{Walltime, _} = statistics(wall_clock),
gen_server:cast(?MODULE, {run_test, {overload, {{From, To}, Walltime, 0, Rate}}}).
stop_test() ->
gen_server:call(?MODULE, stop).
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
ok = file:delete("/var/log/ejabberd/teststats.dat"),
{ok, Device} = file:open("/var/log/ejabberd/teststats.dat", write),
{Walltime1970, _} = statistics(wall_clock),
agr:callbacktimer(10, collect_stats),
{ok, #state{error_count = 0,
message_count = 0,
walltime_init = Walltime1970,
device = Device}}.
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast({run_test, {Aggregator, Server, Params, Startport, PCount}, Count}, State) ->
Subs = [mod_prisma_aggregator_tester:send_subscription(jlib:string_to_jid(Aggregator),
mod_prisma_aggregator_tester:create_json_subscription("https://" ++ Server ++ ":" ++ integer_to_list(Startport + (I rem PCount)) ++ "/" ++ Params,
jlib:jid_to_string(mod_prisma_aggregator_tester:get_sender()),
"ATOM",
"test_hohes_1-" ++ integer_to_list(I)))
|| I <- lists:seq(Count, Count + 1000)],
agr:callbacktimer(120000, {run_test, {Aggregator, Server, Params, Startport, PCount}, Count + 1000}),
% mod_prisma_aggregator_tester:send_bulk_subscriptions(jlib:string_to_jid(Aggregator), Subs),
?INFO_MSG("Test: ~p Neue Nachrichten wurden verschickt ~p, an ~p", [Params, Count + 1000, Aggregator]),
{noreply, State#state{test=continuous}};
handle_cast({message_received, _Message}, State) ->
{noreply, State#state{message_count = State#state.message_count + 1}};
handle_cast({error_received, {Error, From}}, State) ->
{[{<<"class">>,<<"de.prisma.datamodel.message.ErrorMessage">>},
{<<"subscriptionID">>, SubId}, _, _]} = Error,
try
mod_prisma_aggregator_tester:send_iq(mod_prisma_aggregator_tester:get_sender(),
jlib:string_to_jid(From),
"unsubscribe",
json_eep:term_to_json(binary_to_list(SubId)))
catch
_ : _ -> fail %wegen binary to list
end,
{noreply, State#state{error_count = State#state.error_count + 1}};
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(overload, State) ->
?INFO_MSG("In overload", []),
{noreply, State#state{test= recover}};
handle_cast({run_test, {overload, {{From, To}, _StartTime, Count, _Rate, Dev}}}, #state{test=recover} = State) ->
?INFO_MSG("In run recover", []),
file:close(Dev),
lists:map(fun(Num) ->
mod_prisma_aggregator_tester:send_emigrate(From, To, "overload_and_recover-" ++ integer_to_list(Num))
end,
lists:seq(Count div 2, Count, 1)),
{noreply, State};
handle_cast({run_test, {overload, {FromTo, StartTime, Count, Rate}}}, State) ->
{ok, Dev} = file:open("/usr/lib/ejabberd/testfeeds.txt", read),
gen_server:cast(?MODULE, {run_test, {overload, {FromTo, StartTime, Count, Rate, Dev}}}),
{noreply, State};
handle_cast({run_test, {overload, {{From, To}, StartTime, Count, Rate, Dev}}}, State) ->
{Walltime, _} = statistics(wall_clock),
ExpectedMessages = ((Walltime - StartTime) div 1000) * Rate,
MissingMessages = ExpectedMessages - Count,
F = fun(Line, N) ->
mod_prisma_aggregator_tester:subscription_from_line(Line, "aggregatortester." ++ agr:config_read(host), "overload_and_recover-" ++ integer_to_list(N))
end,
Subs = map_to_n_lines(Dev, Count, Count , ExpectedMessages, F, []),
mod_prisma_aggregator_tester:send_bulk_subscriptions(jlib:string_to_jid(From), Subs),
agr:callbacktimer(100, {run_test, {overload, {{From, To}, StartTime, Count + MissingMessages, Rate, Dev}}}),
{noreply, State#state{test=overload}};
handle_cast(collect_stats, State = #state{device = Dev,
walltime_init = Walltime_init,
message_count = MCount,
error_count = ECount}) ->
{Walltime1970, _} = statistics(wall_clock),
io:format(Dev, %add a line to teststats.dat
"~-15w ~-15w ~-15w~n",
[trunc((Walltime1970 - Walltime_init) div 100),
MCount,
ECount]),
agr:callbacktimer(1000, collect_stats),
{noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
map_to_n_lines(_Device, _Start , N, N, _F, Acc) ->
Acc;
map_to_n_lines(Device, Start, Count, N, F, Acc) ->
case io:get_line(Device, "") of
eof -> Acc;
Line ->
if
Count >= Start ->
map_to_n_lines(Device, Start, Count + 1, N, F, [F(Line, Count)| Acc]);
true ->
map_to_n_lines(Device, Start, Count + 1, N, F, Acc)
end
end.