diff --git a/src/measure/utTc.erl b/src/measure/utTc.erl index b288d44..db30c8b 100644 --- a/src/measure/utTc.erl +++ b/src/measure/utTc.erl @@ -1,19 +1,25 @@ -module(utTc). --compile([export_all, nowarn_unused_function, nowarn_unused_vars, nowarn_export_all]). +-compile(inline). +-compile({inline_size, 128}). + +-export([ + ts/4 + , tm/5 +]). %% 单进程循环测试:LoopTimes是循环次数 -%% utTc:ts(Module, Function, ArgsList, LoopTimes). -%% 多进程并发测试:SpawnProcessesCount是并发的进程数 -%% utTc:tm(Module, Function, ArgsList, SpawnProcessesCount). +%% utTc:ts(LoopTimes, Module, Function, ArgsList). +%% 多进程并发测试:SpawnProcessesCount是并发的进程数 LoopTimes是循环次数 +%% utTc:tm(ProcessesCount, LoopTimes, Module, Function, ArgsList). -tc(M, F, A) -> - {Microsecond, _} = timer:tc (M, F, A), +doTc(M, F, A) -> + {Microsecond, _} = timer:tc(M, F, A), Microsecond. distribution(List, Aver) -> distribution(List, Aver, 0, 0). -distribution([H|T], Aver, Greater, Less) -> +distribution([H | T], Aver, Greater, Less) -> case H > Aver of true -> distribution(T, Aver, Greater + 1, Less); @@ -26,25 +32,25 @@ distribution([], _Aver, Greater, Less) -> %% =================================================================== %% test: one process test N times %% =================================================================== - -ts(M, F, A, N) -> - {Max, Min, Sum, Aver, Greater, Less} = loop ({M, F, A}, N), - io:format ("=====================~n"), - io:format ("execute [~p] times of {~p, ~p, ~p}:~n", [N, M, F, A]), - io:format ("Maximum: ~p(us)\t~p(s)~n", [Max, Max / 1000000]), - io:format ("Minimum: ~p(us)\t~p(s)~n", [Min, Min / 1000000]), - io:format ("Sum: ~p(us)\t~p(s)~n", [Sum, Sum / 1000000]), - io:format ("Average: ~p(us)\t~p(s)~n", [Aver, Aver / 1000000]), - io:format ("Greater: ~p ~n", [Greater]), - io:format ("Less: ~p ~n", [Less]), - io:format ("=====================~n"). - - -loop({M, F, A}, N) -> - loop ({M, F, A}, N, 1, 0, 0, 0, []). - -loop({M, F, A}, N, I, Max, Min, Sum, List) when N >= I -> - Microsecond = tc (M, F, A), +ts(LoopTime, M, F, A) -> + {Max, Min, Sum, Aver, Greater, Less} = loopTs(LoopTime, M, F, A, LoopTime, 0, 0, 0, []), + io:format("=====================~n"), + io:format("execute [~p] times of {~p, ~p, ~p}:~n", [LoopTime, M, F, A]), + io:format("MaxTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Max), float_to_binary(Max / 1000000, [{decimals, 6}, compact])]), + io:format("MinTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Min), float_to_binary(Min / 1000000, [{decimals, 6}, compact])]), + io:format("SumTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Sum), float_to_binary(Sum / 1000000, [{decimals, 6}, compact])]), + io:format("AvgTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Aver), float_to_binary(Aver / 1000000, [{decimals, 6}, compact])]), + io:format("Gra: ~10s ~10s(s)~n", [integer_to_binary(Greater), float_to_binary(Greater / LoopTime, [{decimals, 2}]) ++ "%"]), + io:format("Less: ~10s ~10s(s)~n", [integer_to_binary(Less), float_to_binary(Less / LoopTime, [{decimals, 2}]) ++ "%"]), + io:format("=====================~n"). + + +loopTs(0, _M, _F, _A, LoopTime, Max, Min, Sum, List) -> + Aver = Sum / LoopTime, + {Greater, Less} = distribution(List, Aver), + {Max, Min, Sum, Aver, Greater, Less}; +loopTs(Index, M, F, A, LoopTime, Max, Min, Sum, List) -> + Microsecond = doTc(M, F, A), NewSum = Sum + Microsecond, if Max == 0 -> @@ -59,42 +65,38 @@ loop({M, F, A}, N, I, Max, Min, Sum, List) when N >= I -> NewMax = Max, NewMin = Min end, - loop ({M, F, A}, N, I + 1, NewMax, NewMin, NewSum, [Microsecond|List]); -loop({_M, _F, _A}, N, _I, Max, Min, Sum, List) -> - Aver = Sum / N, - {Greater, Less} = distribution(List, Aver), - {Max, Min, Sum, Aver, Greater, Less}. + loopTs(Index - 1, M, F, A, LoopTime, NewMax, NewMin, NewSum, [Microsecond | List]). + %% =================================================================== %% Concurrency test: N processes each test one time %% =================================================================== -tm(M, F, A, N) -> - {Max, Min, Sum, Aver, Greater, Less} = cloop ({M, F, A}, N), - io:format ("=====================~n"), - io:format ("execute [~p] times of {~p, ~p, ~p}:~n", [N, M, F, A]), - io:format ("Maximum: ~p(us)\t~p(s)~n", [Max, Max / 1000000]), - io:format ("Minimum: ~p(us)\t~p(s)~n", [Min, Min / 1000000]), - io:format ("Sum: ~p(us)\t~p(s)~n", [Sum, Sum / 1000000]), - io:format ("Average: ~p(us)\t~p(s)~n", [Aver, Aver / 1000000]), - io:format ("Greater: ~p ~n", [Greater]), - io:format ("Less: ~p ~n", [Less]), - io:format ("=====================~n"). - - -cloop({M, F, A}, N) -> - CollectorPid = self(), - ok = loopSpawn({M, F, A}, CollectorPid, N), - collector(0, 0, 0, N, 1, []). - - -loopSpawn({M, F, A}, CollectorPid, N) when N > 0 -> - spawn_link(fun() -> worker({M, F, A}, CollectorPid) end), - loopSpawn({M, F, A}, CollectorPid, N - 1); -loopSpawn(_, _, 0) -> - ok. - -collector(Max, Min, Sum, N, I, List) when N >= I -> +tm(ProcCnt, LoopTime, M, F, A) -> + loopSpawn(M, F, A, self(), ProcCnt, LoopTime), + {Max, Min, Sum, Aver, Greater, Less} = collector(ProcCnt, 0, 0, 0, ProcCnt, []), + io:format("=====================~n"), + io:format("execute process Cnt:[~p] LoopTime:[~p] times of {~p, ~p, ~p}:~n", [ProcCnt, LoopTime, M, F, A]), + io:format("MaxTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Max), float_to_binary(Max / 1000000, [{decimals, 6}, compact])]), + io:format("MinTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Min), float_to_binary(Min / 1000000, [{decimals, 6}, compact])]), + io:format("SumTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Sum), float_to_binary(Sum / 1000000, [{decimals, 6}, compact])]), + io:format("AvgTime: ~10s(us) ~10s(s)~n", [integer_to_binary(Aver), float_to_binary(Aver / 1000000, [{decimals, 6}, compact])]), + io:format("Grar: ~10s ~10s(s)~n", [integer_to_binary(Greater), float_to_binary(Greater / ProcCnt, [{decimals, 2}]) ++ "%"]), + io:format("Less: ~10s ~10s(s)~n", [integer_to_binary(Less), float_to_binary(Less / ProcCnt, [{decimals, 2}]) ++ "%"]), + io:format("=====================~n"). + + +loopSpawn(0, _, _, _, _, _) -> + ok; +loopSpawn(ProcCnt, M, F, A, CollectorPid, LoopTime) -> + spawn_link(fun() -> worker(LoopTime, M, F, A, CollectorPid) end), + loopSpawn(ProcCnt - 1, M, F, A, CollectorPid, LoopTime). + +collector(0, Max, Min, Sum, ProcCnt, List) -> + Aver = Sum / ProcCnt, + {Greater, Less} = distribution(List, Aver), + {Max, Min, Sum, Aver, Greater, Less}; +collector(Index, Max, Min, Sum, ProcCnt, List) -> receive {result, Microsecond} -> NewSum = Sum + Microsecond, @@ -111,18 +113,20 @@ collector(Max, Min, Sum, N, I, List) when N >= I -> NewMax = Max, NewMin = Min end, - collector(NewMax, NewMin, NewSum, N, I + 1, [Microsecond|List]) - after - 10000 -> - ok - end; -collector(Max, Min, Sum, N, _, List) -> - Aver = Sum / N, - {Greater, Less} = distribution(List, Aver), - {Max, Min, Sum, Aver, Greater, Less}. - + collector(Index - 1, NewMax, NewMin, NewSum, ProcCnt, [Microsecond | List]) + after 1800000 -> + io:format("execute time out~n"), + ok + end. + +worker(LoopTime, M, F, A, CollectorPid) -> + SumTime = loopTm(LoopTime, M, F, A, 0), + CollectorPid ! {result, SumTime}. + +loopTm(0, _, _, _, SumTime) -> + SumTime; +loopTm(LoopTime, M, F, A, SumTime) -> + Microsecond = doTc(M, F, A), + loopTm(LoopTime - 1, M, F, A, SumTime + Microsecond). -worker({M, F, A}, CollectorPid) -> - Microsecond = tc(M, F, A), - CollectorPid ! {result, Microsecond}.