Debian Codegraph @

src/horse.erl

raw
1 %% Copyright (c) 2013, Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(horse).
16
17 -export([app_perf/1]).
18 -export([mod_perf/1]).
19
20 %% These might be interesting later on.
21 %% @todo horse_init, horse_end
22 %% @todo horse_init_per_test, horse_end_per_test
23
24 app_perf(App) when is_atom(App) ->
25 io:format("Running horse on application ~s~n", [App]),
26 ok = application:load(App),
27 {ok, Modules} = application:get_key(App, modules),
28 _ = [mod_perf(M) || M <- lists:sort(Modules)],
29 ok.
30
31 mod_perf(Mod) when is_atom(Mod) ->
32 Perfs = [F || {F, 0} <- Mod:module_info(exports),
33 "horse_" =:= string:substr(atom_to_list(F), 1, 6)],
34 _ = [fun_perf(Mod, Fun) || Fun <- Perfs],
35 ok.
36
37 fun_perf(Mod, Fun) when is_atom(Mod), is_atom(Fun) ->
38 %% Dry run.
39 _ = Mod:Fun(),
40 %% Proper run.
41 Before = os:timestamp(),
42 _Val = Mod:Fun(),
43 After = os:timestamp(),
44 %% Results.
45 Time = timer:now_diff(After, Before),
46 "horse_" ++ Name = atom_to_list(Fun),
47 io:format("~s:~s in ~b.~6.10.0bs~n",
48 [Mod, Name, Time div 1000000, Time rem 1000000]),
49 ok.