写点什么

架构师训练营第七周作业

用户头像
郎哲158
关注
发布于: 2020 年 11 月 08 日

1.性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量是如何变化的,为什么?



乍一看 并发数越高吞吐量会越大,这样说显然不准确。

这里有三个关键变量,请求数, 响应时间,和并发数。单独从公式上看,并发数越高吞吐量越大。实际上,当并发数变大时会影响到影响时间 ,请求数不变,影响时间越大导致除数越小进而导致吞吐量变小,可能会出现并发数越大,导致响应时间巨慢反而吞吐量降低。系统资源固定的情况下,吞吐量理论上是固定的。系统优化的点就是在响应时间上。

2.编程题:

用你熟悉的编程语言写一个Web性能压测工具,输入参数:URL,请求总次数,并发 数。输出参数:平均响应时间,95%响应时间。用这个测试工具以10并发、100次请 求压测www.baidu.com。

-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).




运行结果:

222> work_test:test().

start9 Group,.....120 ms 

2 Group,.....120 ms 

4 Group,.....123 ms 

7 Group,.....125 ms 

6 Group,.....127 ms 

5 Group,.....127 ms 

8 Group,.....128 ms 

3 Group,.....129 ms 

10 Group,.....129 ms 

1 Group,.....130 ms 

end

avaerage_response_time is 12.57

P95 is 19

100



发布于: 2020 年 11 月 08 日阅读数: 29
用户头像

郎哲158

关注

还未添加个人签名 2017.12.20 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第七周作业