⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 megaco_call_flow_test.erl

📁 OTP是开放电信平台的简称
💻 ERL
📖 第 1 页 / 共 5 页
字号:
encoding_time({{Mod, _Opt, Opt2} = Encoder, DecodedMsg}) ->    meter(fun() -> {ok, _} = Mod:encode_message(Opt2, DecodedMsg) end, Encoder).%%----------------------------------------------------------------------%% Compute time for decoding messages%%----------------------------------------------------------------------decoding_times() ->    Decoders = encoders(),    decoding_times(Decoders).%% Returns a list of {MessageSlogan, DecodingTimes} where%% DecodingTimes is the result from decoding_times/2decoding_times(Decoders) ->    [{Slogan, decoding_times(Msg, Decoders)} ||  {Slogan, Msg} <- messages()].%% Returns a list of {Decoder, Res} tuples%% where Res either is the decoding time (integer)%% or an error atomdecoding_times(DecodedMsg, Encoders) ->    [{E, decoding_time(decoding_msg(DecodedMsg, E))} || E <- Encoders].decoding_msg(DecodedMsg, {Mod, Opt, _Opt2} = Encoder) ->    {ok, EncodedMsg} = Mod:encode_message(Opt, DecodedMsg),    {Encoder, EncodedMsg}.decoding_time({{Mod, _Opt, Opt2} = Encoder, EncodedMsg}) ->    meter(fun() -> {ok, _} = Mod:decode_message(Opt2, EncodedMsg) end, Encoder).%%----------------------------------------------------------------------%%----------------------------------------------------------------------coding_times() ->    Coders = encoders(),    coding_times(Coders).%% Returns a list of {MessageSlogan, DecodingTimes} where%% DecodingTimes is the result from decoding_times/2coding_times(Coders) ->    [{Slogan, coding_times(Msg, Coders)} ||  {Slogan, Msg} <- messages()].%% Returns a list of {Decoder, Res} tuples%% where Res either is the decoding time (integer)%% or an error atomcoding_times(DecodedMsg, Coders) ->    [{E, coding_time(coding_msg(DecodedMsg, E))} || E <- Coders].coding_msg(DecodedMsg, {Mod, Opt, _Opt2} = Encoder) ->    {ok, EncodedMsg} = Mod:encode_message(Opt, DecodedMsg),    {Encoder, EncodedMsg}.coding_time({{Mod, _Opt, Opt2} = Encoder, EncodedMsg}) ->    Fun = fun() ->		  {ok, DecodedMsg} = Mod:decode_message(Opt2, EncodedMsg),		  {ok, _}          = Mod:encode_message(Opt2, DecodedMsg)	  end,    meter(Fun, Encoder).%%----------------------------------------------------------------------%% Return size statistics as term%%----------------------------------------------------------------------size_stat() ->    Encoders = encoders(),    MsgSizes = msg_sizes(Encoders),    stat(Encoders, MsgSizes).%%----------------------------------------------------------------------%%----------------------------------------------------------------------gnuplot_gif() ->    [     {size,     gnuplot_size_gif()},     {encoding, gnuplot_enc_time_gif()},     {decoding, gnuplot_dec_time_gif()},     {coding,   gnuplot_code_time_gif()}    ].%%----------------------------------------------------------------------%% Generate GIF picture from size statistics with gnuplot%%----------------------------------------------------------------------gnuplot_size_gif() ->    {ok, _Cwd} = file:get_cwd(),    TmpDir = "megaco_encoded_size.tmp",    GifFile = "megaco_encoded_size.gif",    Header =        ["set title \"Size comparison of Megaco/H.248 encoding formats\"\n",         "set timestamp top\n",         "set terminal gif\n",         "set xlabel \"Test cases from Appendix A\"\n",         "set ylabel \"Message size in bytes\"\n",         "set rmargin 10\n",         "set key left top Left\n",         "set output \"", GifFile, "\"\n\n"],    Encoders = encoders(),    Stat = msg_sizes(Encoders),    BatchFile = gnuplot_dir(TmpDir, Header, Encoders, Stat),    Cmd = "cd " ++ TmpDir ++ "; gnuplot " ++ BatchFile,    os:cmd(Cmd),    ok = io:format("~n~s~nxv ~s~n", [Cmd, filename:join([TmpDir, GifFile])]),    stat(Encoders, Stat).%%----------------------------------------------------------------------%% Return encoding time statistics as term%%----------------------------------------------------------------------encoding_times_stat() ->    Encoders = encoders(),    EncodingTimes = encoding_times(Encoders),    stat(Encoders, EncodingTimes).%%----------------------------------------------------------------------%% Return encoding time statistics as term%%----------------------------------------------------------------------decoding_times_stat() ->    Encoders = encoders(),    DecodingTimes = decoding_times(Encoders),    stat(Encoders, DecodingTimes).%%----------------------------------------------------------------------%% Return encoding time statistics as term%%----------------------------------------------------------------------coding_times_stat() ->    Encoders = encoders(),    CodingTimes = coding_times(Encoders),    stat(Encoders, CodingTimes).%%----------------------------------------------------------------------%% Generate GIF picture from encoding time statistics with gnuplot%%----------------------------------------------------------------------gnuplot_enc_time_gif() ->    {ok, _Cwd} = file:get_cwd(),    TmpDir = "megaco_encoding_time.tmp",    GifFile = "megaco_encoding_time.gif",    Header =        ["set title \"Encoding time comparison of Megaco/H.248 encoding formats\"\n",         "set timestamp top\n",         "set terminal gif\n",         "set xlabel \"Test cases from Appendix A\"\n",         "set ylabel \"Time for encoding in micro seconds\"\n",         "set rmargin 10\n",         "set key left top Left\n",         "set output \"", GifFile, "\"\n\n"],    Encoders = encoders(),    Stat = encoding_times(Encoders),    BatchFile = gnuplot_dir(TmpDir, Header, Encoders, Stat),    Cmd = "cd " ++ TmpDir ++ "; gnuplot " ++ BatchFile,    os:cmd(Cmd),    ok = io:format("~n~s~nxv ~s~n", [Cmd, filename:join([TmpDir, GifFile])]),    stat(Encoders, Stat).%%----------------------------------------------------------------------%% Generate GIF picture from decoding time statistics with gnuplot%%----------------------------------------------------------------------gnuplot_dec_time_gif() ->    {ok, _Cwd} = file:get_cwd(),    TmpDir = "megaco_decoding_time.tmp",    GifFile = "megaco_decoding_time.gif",    Header =        ["set title \"Decoding time comparison of Megaco/H.248 encoding formats\"\n",         "set timestamp top\n",         "set terminal gif\n",         "set xlabel \"Test cases from Appendix A\"\n",         "set ylabel \"Time for decoding in micro seconds\"\n",         "set rmargin 10\n",         "set key left top Left\n",         "set output \"", GifFile, "\"\n\n"],    Encoders = encoders(),    Stat = decoding_times(Encoders),    BatchFile = gnuplot_dir(TmpDir, Header, Encoders, Stat),    Cmd = "cd " ++ TmpDir ++ "; gnuplot " ++ BatchFile,    os:cmd(Cmd),    ok = io:format("~n~s~nxv ~s~n", [Cmd, filename:join([TmpDir, GifFile])]),    stat(Encoders, Stat).%%----------------------------------------------------------------------%% Generate GIF picture from decoding time statistics with gnuplot%%----------------------------------------------------------------------gnuplot_code_time_gif() ->    {ok, _Cwd} = file:get_cwd(),    TmpDir = "megaco_coding_time.tmp",    GifFile = "megaco_coding_time.gif",    Header =        ["set title \"Encoding + decoding time comparison of Megaco/H.248 encoding formats\"\n",         "set timestamp top\n",         "set terminal gif\n",         "set xlabel \"Test cases from Appendix A\"\n",         "set ylabel \"Time for encoding+decoding in micro seconds\"\n",         "set rmargin 10\n",         "set key left top Left\n",         "set output \"", GifFile, "\"\n\n"],    Encoders = encoders(),    Stat = coding_times(Encoders),    BatchFile = gnuplot_dir(TmpDir, Header, Encoders, Stat),    Cmd = "cd " ++ TmpDir ++ "; gnuplot " ++ BatchFile,    os:cmd(Cmd),    ok = io:format("~n~s~nxv ~s~n", [Cmd, filename:join([TmpDir, GifFile])]),    stat(Encoders, Stat).%%----------------------------------------------------------------------%% Encode asn.1 messages%%----------------------------------------------------------------------gen_byte_msg(Msg, {Mod, Opt, _Opt2} = _Encoder) ->    {ok, EncodedMsg} = Mod:encode_message(Opt, Msg),    EncodedMsg.%%----------------------------------------------------------------------%% Gen the C header file content as a binary%%----------------------------------------------------------------------gen_header_file_binary([]) ->    ok;gen_header_file_binary([{S, B}| Rest]) ->    file:write_file(atom_to_list(S), B),    gen_header_file_binary(Rest).%%----------------------------------------------------------------------%% Generate headerfile for asn.1 BER test in C%%----------------------------------------------------------------------gen_ber_header() ->    Encoder = {megaco_ber_encoder, [], []},    L = [{S, gen_byte_msg(Msg, Encoder)} ||  {S, Msg} <- messages()],    gen_header_file_binary(L).%%----------------------------------------------------------------------%% Generate headerfile for asn.1 BER test in C%%----------------------------------------------------------------------gen_ber_bin_header() ->    Encoder = {megaco_ber_bin_encoder, [], []},    L = [{S, gen_byte_msg(Msg, Encoder)} ||  {S, Msg} <- messages()],    gen_header_file_binary(L).%%----------------------------------------------------------------------%% Generate headerfile for asn.1 PER test in C%%----------------------------------------------------------------------gen_per_header() ->    Encoder = {megaco_per_encoder, [], []},    L = [{S, gen_byte_msg(Msg, Encoder)} ||  {S, Msg} <- messages()],    gen_header_file_binary(L).%%----------------------------------------------------------------------%% Execute a fun a number of times and return avg in millis%%----------------------------------------------------------------------meter(Fun, Encoder) ->    MaxTime = timer:seconds(5),    Rep = 2,    M = pretty_mod(Encoder),    io:format("~p:\t", [M]),    Times  = [single_meter(Fun, MaxTime, {M, N}) || N <- lists:seq(1, Rep)],    Min = lists:min(Times),    Max = lists:max(Times),    Median = lists:nth((Rep + 1) div 2, lists:sort(Times)),    io:format("(median=~p, diff=~p)~n", [Median, Max - Min]),    Median.single_meter(Fun, MaxTime, Tag) ->    Pid = spawn_link(?MODULE, single_meter, [self(), Fun, MaxTime, Tag]),    receive	{meter, Pid, Time} ->	    io:format("~p \t", [Time]),	    Time;	{'EXIT', Pid, Reason} ->	    {bad_result, Reason}    end.single_meter(Parent, Fun, Expected, _Tag) ->    erlang:statistics(runtime),    erlang:send_after(Expected, self(), return_count),    Count = count(Fun, 1),    {_, Actual} = erlang:statistics(runtime),    %% Diff = Actual - Expected,    Micros = trunc((Actual / Count) * 1000),    Parent ! {meter, self(), Micros},    unlink(Parent),    exit(normal).count(Fun, N) ->    Fun(),    receive	return_count ->	    N    after 0 ->	    count(Fun, N + 1)    end.stat(Encoders, Stat) ->    Fun =	fun({_Mod, _Opt, _Opt2} = E) ->		List = lists:flatten([[Val || {E2, Val} <- Info, E2 == E] ||					 {_Slogan, Info} <- Stat]),		Max = lists:max(List),		Min = lists:min(List),		case catch lists:sum(List) of		    {'EXIT', _} ->			{E, bad, List};		    Sum when integer(Sum) ->			N   = length(List),			{E, [{min, Min}, {avg, Sum div N}, {max, Max}]}		end	end,    lists:map(Fun, Encoders).gnuplot_dir(Dir, Header, Encoders, Stat) ->    file:make_dir(Dir),    {Names, Arrows} = gnuplot_data(Encoders, Dir, Stat, 1, [], []),    [H | T] = [lists:concat(["\"", N, "\" with linespoints"]) || N <- Names],    Plots = [[", ", Plot] || Plot <- T],    IoList = [Header, Arrows, "\nplot ", H, Plots, "\n\nshow output\n"],    Bin = list_to_binary(IoList),    BatchFile = "gnuplot.batch",    file:write_file(filename:join(Dir, BatchFile), Bin),    BatchFile.gnuplot_data([], _Dir, _Stat, _Pos, Names, Arrows) ->    {lists:reverse(Names), lists:reverse(Arrows)};gnuplot_data([{Mod, _Opt, _Opt2} = E | Encoders], Dir, Stat, Pos, Names, Arrows) ->    Plot = fun({Msg, List}, {N, AccSz}) ->                   {value, {_, Sz}} = lists:keysearch(E, 1, List),                   ActualSz =                       if                           integer(Sz) ->                               Sz;                           true        ->                               ok = io:format("<ERROR> ~p(~p) -> ~p~n",                                              [Mod, Msg, Sz]),                               0                       end,                   Acc = {N + 1, [ActualSz | AccSz]},                   {lists:concat([N + 1, " ", ActualSz, "\n"]), Acc}           end,    {Data, {N, Sizes}} = lists:mapfoldl(Plot, {0, []}, Stat),    Min = lists:min(Sizes),    Max = lists:max(Sizes),    Sum = lists:sum(Sizes),    Avg = Sum div N,    Len = length(Stat),    Pretty = pretty_mod(E),    Name = lists:concat([Pretty, " (", Min, ",", Avg, ",", Max, ")"]),    file:write_file(filename:join(Dir, Name), list_to_binary(Data)),    %%"plot \"-\" title \"", E, "\" with linespoints\n", Data, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -