summaryrefslogtreecommitdiff
path: root/deps/meck/test/meck_performance_test.erl
blob: 71af107d6e73adc1d8346b9e62cac39022896ba3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
%% @doc
-module(meck_performance_test).

%% Interface exports
-export([run/1]).

%%==============================================================================
%% Interface exports
%%==============================================================================

run(N) ->
    meck:new(test),
    io:format("\t\tMin\tMax\tMed\tAvg~n"),
    io:format("expect/3\t~p\t~p\t~p\t~p~n",
              test_avg(meck, expect, [test, normal, fun() -> ok end], N)),
    io:format("expect/3+args\t~p\t~p\t~p\t~p~n",
              test_avg(meck, expect, [test, normal_args,
                                      fun(_, _) -> ok end], N)),
    io:format("expect/4\t~p\t~p\t~p\t~p~n",
              test_avg(meck, expect, [test, shortcut, 0, ok], N)),
    io:format("expect/4+args\t~p\t~p\t~p\t~p~n",
              test_avg(meck, expect, [test, shortcut_args, 2, ok], N)),

    meck:expect(test, shortcut_opaque, 0, self()),

    io:format("~n\t\tMin\tMax\tMed\tAvg~n"),
    io:format("normal\t\t~p\t~p\t~p\t~p~n",
              test_avg(test, normal, [], N)),
    io:format("normal_args\t~p\t~p\t~p\t~p~n",
              test_avg(test, normal_args, [a, b], N)),
    io:format("shortcut\t~p\t~p\t~p\t~p~n",
              test_avg(test, shortcut, [], N)),
    io:format("shortcut_args\t~p\t~p\t~p\t~p~n",
              test_avg(test, shortcut_args, [a, b], N)),
    io:format("shortcut_opaque\t~p\t~p\t~p\t~p~n",
              test_avg(test, shortcut_opaque, [], N)),
    meck:unload(test),

    meck:new(test),
    meck:expect(test, func, 1, ok),
    [test:func(I) || I <- lists:seq(1, 100)],
    io:format("~n\t\tMin\tMax\tMed\tAvg~n"),
    io:format("called\t\t~p\t~p\t~p\t~p~n",
              test_avg(meck, called, [test, func, 50], N)),
    meck:unload(test),
    ok.

%%==============================================================================
%% Internal functions
%%==============================================================================

test_avg(M, F, A, N) when N > 0 ->
    L = test_loop(M, F, A, N, []),
    Length = length(L),
    Min = lists:min(L),
    Max = lists:max(L),
    Med = lists:nth(round((Length / 2)), lists:sort(L)),
    Avg = round(lists:foldl(fun(X, Sum) -> X + Sum end, 0, L) / Length),
    [Min, Max, Med, Avg].

test_loop(_M, _F, _A, 0, List) ->
    List;
test_loop(M, F, A, N, List) ->
    {T, _Result} = timer:tc(M, F, A),
    test_loop(M, F, A, N - 1, [T|List]).