eprof代码分析工具

清参考http://www.erlang.org/doc/man/eprof.html

 

A Time Profiling Tool for Erlang

主要API  R14B 比R13B提供了更多的API

start/0,

start_profiling/1

stop_profiling/0

profile/2..

analyse/0,

total_analyse/0

log/0

 

 

 23 test(N)->
 24     {ok, Epid}=eprof:start(),  %启动
 25     Pid = spawn(?MODULE, hello, []),
 26     profiling = eprof:start_profiling([Pid]), %启动
 27     test(N,Pid), %执行被分析的程序
 28     eprof:stop_profiling(),
 29 %    eprof:analyse(),
           eprof:log(test), %将分析结果生成文件:
 30     eprof:total_analyse(), %分析结果
 31     eprof:stop(). %停止

 34 test(0,_Pid) ->
 35     ok;
 36 test(N,Pid) ->
 37     send(Pid),
 38     test(N-1,Pid). 
 39 
 40 send(Pid) ->
 41     Pid ! {self(),test},
 42     loop().

51 hello() ->
 52    % Pid1 = spawn(?MODULE, hello1, []),
 53    % loop(),
 54    t(),
 55     %Pid1 ! {self(),test},
 56     receive 
 57         {P,test} ->
 58             P ! returnok,
 59             hello()
 60     end.

61 t() ->
 62     ok.

 66 loop() ->
 67     receive 
 68         T ->
 69             T,                                                                                                                                           
 70             loop()
 71     after 500 ->
 72          timeout
 73     end.
~              

 

 

测试分析结果:

 

7> test_eprof:test(20). 
eprof: Starting profiling ..... 
eprof: Stop profiling
FUNCTION                                       CALLS      TIME 
test_eprof:hello/0                             20         80 % 
test_eprof:t/0                                    20         20 % 

Total time: 0.00
Measurement overhead: 0.00
stopped
8> 

  注意此方法的位置:是在analyse/0和total_analyse前面。否则日志里面没有数据

  log(File) -> ok

Types:

File = atom() | string()

 

This function ensures that the results displayed by analyse/0 and total_analyse/0 are printed both to the file File and the screen

 

 

 

(1)eprof:start_profiling([self()]),分析结果无法搜集到chat:handle_info chat:handle_cast chat:handle_call
(2)eprof:start_profiling([chat]), 分析结果无法搜集到chat:handle_info chat:handle_cast


出现问题的原因:
Sending a message (handled by handle_info) or doing a cast (handled by handle_cast) are both async.
You are stopping the profiler too early.Changing  the order to do the async operations first, then the sync one should  assure that all the requests have been handled before you stop eprof我添加seelp试过,问题搞定!
-module(chat).
-compile(export_all).
%-behaviour(gen_server).
start(N) ->
    start_link(),
    eprof:start(),
    eprof:start_profiling([self()]),
    test(N),
    testcast(),
    test(),
    eprof:stop_profiling(),
    eprof:log(chat),
    eprof:analyse(),
    eprof:total_analyse().

test(N)->
    gen_server:call(?MODULE, {test,N}).
test()->
    chat ! {test,1}.
testcast() ->
    gen_server:cast(?MODULE,castttt).

start_link() ->
    gen_server:start_link({local,?MODULE},?MODULE,[],[]).

init([]) ->
    {ok, {}}.

handle_cast(Msg,State) ->
    tttt(),
    io:format("cast=~p~n",[Msg]),
    {noreply,State}.

handle_call({test,Number},From, State) when is_number(Number) ->
    Reply = Number+1,
    {reply, Reply,State};

handle_call(_,From, State) ->
    Reply = numerror,
    {reply, Reply,State}.
handle_info(Ino,State) ->
    tttt(),
    io:format("info=~p~n",[Ino]),
    {noreply,State}.
tttt() ->
    ok.
 

 

 

你可能感兴趣的:(html,erlang)