This repository has been archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fake_client.erl
135 lines (127 loc) · 5.39 KB
/
fake_client.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
-module(fake_client).
-export([watch/3, watch/5]).
%% @doc Spawn links N watchers
-spec watch(pos_integer(), pos_integer(), inet:ip_address() | inet:hostname(), pos_integer(), match_stream:user_id()) -> {non_neg_integer(), non_neg_integer()}.
watch(N, C, Server, Port, UserIdPrefix) ->
Self = self(),
lists:foreach(
fun(I) ->
io:format("Starting clients ~p to ~p...~n", [I+1, I+C]),
lists:foreach(
fun(IC) ->
UserId = <<UserIdPrefix/binary, $.,
(list_to_binary(integer_to_list(IC)))/binary>>,
proc_lib:spawn_link(
fun() ->
try
{T, {ok, RT}} =
timer:tc(?MODULE, watch, [Server, Port, UserId]),
io:format("Tester ~s done - ~p s~n", [UserId, erlang:round(T/1000000)]),
Self ! {ok, RT}
catch
_:Error ->
io:format("Tester ~s failed: ~p.~n\t~p~n", [UserId, Error, erlang:get_stacktrace()]),
Self ! error
end
end)
end, lists:seq(I+1, I+C)),
io:format("Hold on a second...~n", []),
timer:sleep(250)
end, lists:seq(0, N-1, C)),
{ROk, RSRT, RErr} =
lists:foldl(fun(P, {Ok, SRT, Err}) ->
io:format("Waiting for result #~p~n", [P]),
receive
{ok, RT} -> {Ok+1, SRT + RT, Err};
error -> {Ok, SRT, Err+1};
Other -> io:format("WTF?? ~p~n", [Other]), {Ok, SRT, Err+1}
end
end, {0, 0, 0}, lists:seq(1, N)),
{ROk, erlang:round(RSRT/ROk), RErr}.
%% @doc Connects to the server and watches a game till it ends
-spec watch(inet:ip_address() | inet:hostname(), pos_integer(), match_stream:user_id()) -> ok.
watch(Server, Port, UserId) ->
case gen_tcp:connect(Server, Port, [binary, {packet, line}]) of
{ok, Socket} ->
run(UserId, Socket);
Error ->
throw(Error)
end.
-spec run(binary(), port()) -> {ok, pos_integer()}.
run(UserId, Socket) ->
wait_for_welcome(UserId, Socket, [], timestamp()).
-spec wait_for_welcome(binary(), port(), [binary()], pos_integer()) -> {ok, pos_integer()}.
wait_for_welcome(UserId, Socket, [], Ts) ->
receive
{tcp, Socket, <<"Welcome to Match Stream.", _/binary>>} ->
RT = timestamp() - Ts,
io:format("~p. ~p. Welcome message in ~pms.~n", [self(), Socket, RT]),
wait_for_welcome(UserId, Socket, [], RT);
{tcp, Socket, <<"To watch the current game use: V:2:<your-name>", _/binary>>} ->
gen_tcp:send(Socket, <<"V:2:", UserId/binary, "\r\n">>),
{ok, FRT} = wait_for_first_event(Socket, [{<<"connect">>, complete}], timestamp()),
{ok, erlang:max(Ts, FRT)};
Other -> throw({wtf, Other})
end.
-spec wait_for_first_event(port(), [binary()], pos_integer()) -> {ok, pos_integer()}.
wait_for_first_event(Socket, Events, Ts) ->
receive
{tcp, Socket, <<$\t, Msg/binary>>} -> %% A detail line, error
throw({detail_out_of_event, Msg});
{tcp, Socket, <<"\r\n">>} -> %% A closing line, error
throw(empty_event);
{tcp, Socket, Msg} -> %% A header line, we add the event to the list
RT = timestamp() - Ts,
case binary:split(Msg, [<<": ">>,<<":\n">>], [global, trim]) of
[_Date, Ev] ->
io:format("~p. ~p. First message in ~pms.~n", [self(), Socket, RT]),
{loop(Socket, [Ev|Events]), RT};
_ ->
throw({invalid_header, Msg})
end;
{tcp_closed, Socket} ->
io:format("~p closed~n", [self()]),
throw({incomplete_match, hd(Events)});
Other -> throw({wtf, Other})
end.
-spec loop(port(), [binary()]) -> ok.
loop(Socket, [{Event, complete}|Events]) ->
receive
{tcp, Socket, <<$\t, Msg/binary>>} -> %% A detail line, we ignore it
throw({detail_out_of_event, Msg});
{tcp, Socket, <<"\r\n">>} -> %% A closing line, we close the event
throw(empty_event);
{tcp, Socket, Msg} -> %% A header line, we add the event to the list
case binary:split(Msg, [<<": ">>,<<":\n">>], [global, trim]) of
[_Date, Ev] ->
loop(Socket, [Ev|Events]);
_ ->
throw({invalid_header, Msg})
end;
{tcp_closed, Socket} ->
io:format("~p closed~n", [self()]),
case Event of
<<"stop">> -> ok;
<<"status">> -> ok;
Event -> throw({incomplete_match, Event})
end;
Other -> throw({wtf, Other})
end;
loop(Socket, [Event|Events]) ->
receive
{tcp, Socket, <<$\t, _/binary>>} -> %% A detail line, we ignore it
loop(Socket, [Event|Events]);
{tcp, Socket, <<"\r\n">>} -> %% A closing line, we close the event
%io:format("Event on (~p): ~s~n", [self(), Event]),
loop(Socket, [{Event, complete}|Events]);
{tcp, Socket, Msg} -> %% A header line, we add the event to the list
throw({incomplete_event, Event, Msg});
{tcp_closed, Socket} ->
throw({disconnected_on, Event});
Other -> throw({wtf, Other})
end.
timestamp() ->
{_, _, MicroSecs} = erlang:now(),
Millis = erlang:trunc(MicroSecs/1000),
calendar:datetime_to_gregorian_seconds(
calendar:universal_time()) * 1000 + Millis.