Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'assertTime' macro to eunit #19

Open
richcarl opened this issue Jul 6, 2012 · 0 comments
Open

add 'assertTime' macro to eunit #19

richcarl opened this issue Jul 6, 2012 · 0 comments

Comments

@richcarl
Copy link
Owner

richcarl commented Jul 6, 2012

Luke Gorrie wrote on the erlang mailing list (06/11/2007 12:31 PM):

Did I ever post my favourite profiling macro? It's pretty simple:

-define(TIME(Tag,Expr),
    (fun() ->
             %% NOTE: timer:tc/4 does an annoying 'catch' so we
             %% need to wrap the result in 'ok' to be able to
             %% detect an unhandled exception.
             {__TIME, __RESULT} = timer:tc(erlang,apply,[fun() -> {ok,Expr} end,[]]),
             io:format("time(~s): ~18.3fms ~999p~n", [?MODULE,__TIME/1000, Tag]),
             case __RESULT of
                 {ok,_} -> element(2, __RESULT);
                 {'EXIT',Error}  -> exit(Error)
             end
     end)()).

if you wrap it around a few strategic expressions in your code you end
up with simple running-time summary printouts like this:
time(backup): 3654.744ms restore_table_defs
time(backup): 182.969ms restore_secondary_indexes
time(backup): 311.973ms restore_records
time(backup): 20.928ms checkpoint
time(backup): 5.095ms remove_logs

Update1: Added a macro ?debugTime(Str,Expr) to eunit.hrl in revision 257 (​https://forge.process-one.net/changelog/P1Contribs/trunk/eunit?cs=257), inspired by the one by Luke.

Update 2:
In what way does this differ from timeouts?
Simply measure wall clock before and after?
Use non-hardware dependent units with automatic calibration á la bogomips? (berps?)
Have different macros for measuring different things?
Macros for checking linear/quadratic/exponential time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant