megaco_codec_meas.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 652 行 · 第 1/2 页

ERL
652
字号
%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%%%% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%%%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%%%%     $Id$%%%%----------------------------------------------------------------------%% Purpose: Measure megaco codec's encoding & decoding time's%%%% Measurement process consists of:%%   For each message in a directory:%%     Pre:         Read message from the file, close the file%%     Measurement: 1) measure decode%%                  2) measure encode (of the previously decoded message)%%     Post:        Print average%%   For each directory: %%     A summery is written, both to the console and to a file, %%     in an excel compatible format.%%%% megaco_codec_meas:t().%% megaco_codec_meas:t([pretty, compact]).%% megaco_codec_meas:t([per, pretty, compact]).%%%%-----------------------------------------------------------------------module(megaco_codec_meas).%% -compile(export_all).%% API-export([t/0, t1/0, t/1]).-export([te/0,	 tb/0, tbb/0, tbp/0,	 tt/0, ttp/0, ttc/0]).%% Internal exports-export([do_measure_codec/7, do_measure_codec_loop/7]).-export([flex_scanner_handler/1]).-include_lib("kernel/include/file.hrl").-define(V3, prev3c).-define(MEASURE_TIMEOUT, 100000).-ifndef(MEASURE_COUNT_TIME).-define(MEASURE_COUNT_TIME, 2*1000*1000). % 2 seconds-endif.-ifndef(MEASURE_TIME).-define(MEASURE_TIME, 10000).-endif.-ifndef(MEASURE_CODECS).-define(MEASURE_CODECS, [pretty, compact, per, ber, erlang]).-endif.-record(stat, {file, ecount, etime, dcount, dtime, size}).%% Runs the measurement on all "official" codecst1() ->    put(everbose,true),    t().t() ->    t(?MEASURE_CODECS).te() ->    t([erlang]).tb() ->    t([ber, per]).tbb() ->    t([ber]).tbp() ->    t([per]).tt() ->    t([pretty, compact]).ttp() ->    t([pretty]).ttc() ->    t([compact]).%% Dirs is a list of directories containing files,%% each with a single megaco message. %%%% Note that it is a requirement that each dir has%% the name of the codec with which the messages has%% been encoded: %%%%    pretty | compact | ber | per | erlang%%t(Dirs) when list(Dirs) ->    io:format("~n", []),    display_os_info(),    display_system_info(),    display_app_info(),    io:format("~n", []),    Started = now(),    EDirs = expand_dirs(Dirs, []),    Results = t1(EDirs, []),     display_time(Started, now()),    store_results(Results).display_os_info() ->    V = case os:version() of	    {Major, Minor, Release} ->		lists:flatten(		  io_lib:format("~w.~w.~w", [Major, Minor, Release]));	    Str ->		Str	end,    case os:type() of	{OsFam, OsName} ->	    io:format("OS:                  ~p-~p: ~s~n", [OsFam, OsName, V]);	OsFam ->	    io:format("OS:                  ~p: ~s~n", [OsFam, V])    end.	    display_system_info() ->    SysArch = string:strip(erlang:system_info(system_architecture),right,$\n),    SysVer  = string:strip(erlang:system_info(system_version),right,$\n),    io:format("System architecture: ~s~n", [SysArch]),    io:format("System version:      ~s~n", [SysVer]),    ok.        display_app_info() ->    display_megaco_info(),    display_asn1_info().display_megaco_info() ->    MI = megaco:module_info(),    {value, {attributes, Attr}} = lists:keysearch(attributes, 1, MI),    {value, {app_vsn,    Ver}}  = lists:keysearch(app_vsn, 1, Attr),    io:format("Megaco version:      ~s~n", [Ver]).display_asn1_info() ->    AI = megaco_ber_bin_drv_media_gateway_control_v1:info(),    Vsn = 	case lists:keysearch(vsn, 1, AI) of	    {value, {vsn, V}} when atom(V) ->		atom_to_list(V);	    {value, {vsn, V}} when list(V) ->		V;	    _ ->		"unknown"	end,    io:format("ASN.1 version:       ~s~n", [Vsn]).%% {MegaSec, Sec, MicroSec}display_time(Start, Fin) ->    FormatDate1 = format_timestamp(Start),    FormatDate2 = format_timestamp(Fin),    FormatDiff  = format_diff(Start, Fin),    io:format("Started:  ~s~n", [FormatDate1]),    io:format("Finished: ~s~n", [FormatDate2]),    io:format("          ~s~n~n~n", [FormatDiff]),    ok.    format_timestamp({_N1, _N2, N3} = Now) ->    {Date, Time}   = calendar:now_to_datetime(Now),    {YYYY,MM,DD}   = Date,    {Hour,Min,Sec} = Time,    FormatDate =        io_lib:format("~.4w:~.2.0w:~.2.0w ~.2.0w:~.2.0w:~.2.0w 4~w",                      [YYYY,MM,DD,Hour,Min,Sec,round(N3/1000)]),    lists:flatten(FormatDate).  format_diff(Start, Fin) ->    DateTime1 = calendar:now_to_universal_time(Start),    DateTime2 = calendar:now_to_universal_time(Fin),    T1 = calendar:datetime_to_gregorian_seconds(DateTime1),    T2 = calendar:datetime_to_gregorian_seconds(DateTime2),    {_, Diff} = calendar:gregorian_seconds_to_datetime(T2 - T1),    Tmp = 	case Diff of	    {0, 0, S} ->		io_lib:format("~.2.0w sec", [S]);	    {0, M, S} ->		io_lib:format("~w min ~.2.0w sec", [M,S]);	    {H, M, S} ->		io_lib:format("~w hour ~w min ~.2.0w sec", [H,M,S])	end,    lists:flatten(Tmp).        			      t1([], Results) ->    lists:reverse(Results);t1([{Dir, Codec, Conf, _} = EDir|Dirs], Results) ->    case (catch measure(EDir)) of	{'EXIT', Reason} ->	    error("measure of codec ~p exited: ~n~p", [Codec, Reason]),	    t1(Dirs, Results);	{error, Reason} ->	    error("skipping codec ~p: ~n~p", [Codec, Reason]),	    t1(Dirs, Results);	{ok, Res} ->	    t1(Dirs, [{Dir, Conf, Res}| Results])    end.measure({Dir, Codec, Conf, Count}) when list(Dir) ->    io:format("measure using codec ~p ~p~n ", [Codec, Conf]),    {Init, Conf1} = measure_init(Conf),    Conf2 = [{version3,?V3}|Conf1],    Res = measure(Dir, Codec, Conf2, read_files(Dir), [], Count),    measure_fin(Init),    Res.expand_dirs([], EDirs) ->    lists:reverse(lists:flatten(EDirs));expand_dirs([Dir|Dirs], EDirs) when atom(Dir) ->    EDir = expand_dir(atom_to_list(Dir)),    expand_dirs(Dirs, [EDir|EDirs]);expand_dirs([Dir|Dirs], EDirs) when list(Dir) ->    EDir = expand_dir(Dir),    expand_dirs(Dirs, [EDir|EDirs]).expand_dir(Dir) ->    case Dir of	"pretty" ->	    [{Dir, megaco_pretty_text_encoder, [flex_scanner], 3000},	     {Dir, megaco_pretty_text_encoder, [],             1000}];	"compact" ->	    [{Dir, megaco_compact_text_encoder, [flex_scanner], 3000},	     {Dir, megaco_compact_text_encoder, [],             1500}];	"ber" ->	    [{Dir, megaco_ber_bin_encoder, [driver,native], 4000},	     {Dir, megaco_ber_bin_encoder, [native],        3000},	     {Dir, megaco_ber_bin_encoder, [driver],        3000},	     {Dir, megaco_ber_bin_encoder, [],              1000}];	"per" ->	    [{Dir, megaco_per_bin_encoder, [driver,native], 4000},	     {Dir, megaco_per_bin_encoder, [native],        3000},	     {Dir, megaco_per_bin_encoder, [driver],        3000},	     {Dir, megaco_per_bin_encoder, [],              1000}];	"erlang" ->	    [	     {Dir, megaco_erl_dist_encoder, [megaco_compressed,compressed], 500},	     {Dir, megaco_erl_dist_encoder, [compressed], 500},	     {Dir, megaco_erl_dist_encoder, [megaco_compressed], 10000}, 	     {Dir, megaco_erl_dist_encoder, [], 10000}	    ];	Else ->	    exit({error, {invalid_codec, Else}})    end.read_files(Dir) ->    case file:list_dir(Dir) of        {ok, Files} ->            lists:sort(Files);        Error ->            exit(Error)    end.measure_init([flex_scanner]) ->    start_flex_scanner();measure_init(Conf) ->    {undefined, Conf}.measure_fin(Pid) when pid(Pid) ->    stop_flex_scanner(Pid),    ok;measure_fin(_) ->    ok.measure(_Dir, _Codec, _Conf, [], [], _MCount) ->    {error, no_messages};measure(_Dir, _Codec, _Conf, [], Res, _MCount) ->    Eavg = avg([Etime/Ecnt || #stat{ecount = Ecnt, etime = Etime} <- Res]),    Davg = avg([Dtime/Dcnt || #stat{dcount = Dcnt, dtime = Dtime} <- Res]),    Savg = avg([Size       || #stat{size = Size} <- Res]),    io:format("~n  Measurment on ~p messages:"	      "~n  Average size:   ~w bytes, "	      "~n          encode: ~w microsec, "	      "~n          decode: ~w microsec~n~n", 	      [length(Res), Savg, Eavg, Davg]),    {ok, lists:reverse(Res)};measure(Dir, Codec, Conf, [File|Files], Results, MCount) ->    io:format(" ~s", [File]),    case (catch do_measure(Dir, Codec, Conf, File, MCount)) of	{ok, Stat} ->	    measure(Dir, Codec, Conf, Files, [Stat | Results], MCount);	{error, S} ->	    io:format("~n~s failed: ~n", [File]),	    error(S,[]),	    measure(Dir, Codec, Conf, Files, Results, MCount);	{info, S} ->	    case get(verbose) of		true ->

⌨️ 快捷键说明

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