-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechoStressTest.erl
More file actions
executable file
·90 lines (70 loc) · 2.47 KB
/
echoStressTest.erl
File metadata and controls
executable file
·90 lines (70 loc) · 2.47 KB
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
#!/usr/bin/env escript
getCSOCK_OPTS() -> [binary, {packet, 0}, {nodelay, true}].
getTestData() ->
Data = <<"abcdefghijk">>,
{byte_size(Data), Data}.
main([SIP,SPort,CNum,CPingNum]) ->
{ok, IP} = inet_parse:ipv4_address(SIP),
Port = list_to_integer(SPort),
Num = list_to_integer(CNum),
PingNum = list_to_integer(CPingNum),
FPid = self(),
ChildArray = [],
forkAllClients(FPid, IP,Port,Num,Num,PingNum,ChildArray),
waitChild(Num,0,0);
main(_) -> usage().
usage()->
io:format("~nUsage: ./echoStressTest.erl <dest addr> <dest port> <client num> <ping round num>~n"),
io:format("~nExample:~n"),
io:format(" ./echoStressTest.erl 127.0.0.1 6678 223 1900~n~n").
waitChild(0,DoneNum,ErrorNum)->
io:format("*** waitChild DoneNum: ~p~n", [DoneNum]),
io:format("*** waitChild ErrorNum: ~p~n", [ErrorNum]);
waitChild(Num,DoneNum,ErrorNum)->
io:format("*** Work process Done number: ~p~n", [DoneNum]),
receive
{done}->
waitChild(Num-1,DoneNum+1,ErrorNum);
{error}->
waitChild(Num-1,DoneNum,ErrorNum+1);
Other->
io:format("*** waitChild Invalid message: ~p~n", [Other]),
waitChild(Num,DoneNum,ErrorNum)
end.
forkAllClients(_,_,_,0,Num,_,ChildArray)->
io:format("The ~p clients forked!~n", [Num]),
lists:foreach( fun(Pid)-> Pid ! {go} end, ChildArray),
io:format("Begin to wait for all the process work is done !~n");
forkAllClients(FPid,IP,Port,Remain,Total,PingNum,ChildArray)->
Pid = spawn(
fun () ->
receive
{go}->
{ok, US} = gen_tcp:connect(IP, Port, [{active, true} | getCSOCK_OPTS()]),
client_loop(FPid,US,PingNum)
end
end
),
forkAllClients(FPid,IP,Port,Remain-1,Total,PingNum,[Pid|ChildArray]).
client_loop(FPid,US,0)->
gen_tcp:close(US),
FPid ! {done};
client_loop(FPid,US,PingNum)->
{DataSize,BinData} = getTestData(),
ok = gen_tcp:send(US, BinData),
client_loop(FPid,US,DataSize,DataSize),
client_loop(FPid,US,PingNum-1).
client_loop(_FPid,_US,0,_DataSize)->
ok;
client_loop(FPid,US,_UnReceived,DataSize)->
receive
{tcp, US, Data} ->
%%io:format("received ~p~n",[Data]),
%%io:format("The unreceived:~p~n",[DataSize-byte_size(Data)]),
client_loop(FPid,US,DataSize-byte_size(Data),DataSize);
Other ->
io:format("*** Invalid message: ~p~n", [Other]),
FPid ! {error}
after 1000 *10 ->
FPid ! {error}
end.