-module(work_test).
-export([test/4,
process/4,
loop/1,
test/0]).
test() ->
io:format("start"),
ets:new(result, [named_table,public]),
test("https://www.baidu.com", 10, 10, self()),
%ets:insert(result, {1,1}),
loop(1),
Result = ets:tab2list(result),
ets:delete(result),
List = print(Result, []),
%io:format("~p~n", [List]),
print_average(List),
print_95(List),
length(Result).
print_95(List) ->
NewList = lists:sort(List),
print_95(NewList, 0).
print_95([], _N) ->
nofound;
print_95([H|_T], N) when N==95 ->
io:format("P95 is ~p~n", [H]);
print_95([_H|T], N) ->
print_95(T, N+1).
print_average(List) ->
print_average(List, 0, length(List)).
print_average([], Acc, Len) ->
Result = Acc/Len,
io:format("avaerage_response_time is ~p~n", [Result]);
print_average([H|T], Acc, Len) ->
print_average(T, H+Acc, Len).
print([], Acc) ->
Acc;
print([{{_Process, _Num}, {S, E}}|T], Acc) ->
Ns = get_diff_ms(S, E),
%K = integer_to_list(Process)++integer_to_list(Num),
%io:format("~p~n", [K,Ns}]),
print(T, [Ns|Acc]).
loop(11) ->
io:format("end~n"),
ok;
loop(N) ->
receive {_ProcessNum, List} ->
%io:format("~p~n", [List]),
[ets:insert(result, V)|| V <- List],
%io:format("~p", [ets:tab2list(result)]),
loop(N+1)
after 10000 ->
ok
end.
test(Url,Process, Num, Pid) ->
ssl:start(),
inets:start(),
%process(Num, self(), Url).
[spawn(?MODULE, process, [Num, ProcessNum, Url, Pid]) || ProcessNum <- lists:seq(1, Process)].
process(Num, ProcessNum, Url, Pid) ->
Ps = now(),
Fun = fun(N, Acc) ->
S = now(),
case httpc:request('get',{Url,[]},[{timeout, 15000}],[]) of
{ok, {{_HttpVersion, 200, _RtDesc}, _Header, _}} ->
"ok";
_Ohter ->
"error"
end,
E = now(),
%io:format("~p,~p",[{s,e, ProcessNum}, self()]),
%dict:store({ProcessNum, Num}, {S, E}, Dict),
%io:format("~p...............~n",[{ProcessNum, Num}]),
[{{ProcessNum, N},{S,E}} | Acc]
end,
%[Fun(N)||N<- lists:seq(1,Num)],
Result = lists:foldl(Fun, [], lists:seq(1, Num)),
Pend = now(),
Ns = get_diff_ms(Ps, Pend),
io:format("~p Group,.....~p ms ~n", [ProcessNum, Ns]),
Pid ! {ProcessNum, Result},
ok.
get_diff_ms({MS, SS, MiS}, {ME, SE, MiE}) ->
(ME*1000000000+SE*1000+ MiE div 1000) -
(MS*1000000000+SS*1000+ MiS div 1000).
评论