summaryrefslogtreecommitdiff
path: root/bench.erl
blob: bc5102da7ac70768021ef50b875281f7d2f6a93a (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa test/ebin -pa ../lager/ebin -pa ../lager/deps/goldrush/ebin

-mode(compile).

add_entries(Entries) ->
    lists:foreach(fun (Entry) ->
                          ht:add(Entry)
                  end, Entries).

heapsize(Pid) when is_pid(Pid) ->
    {total_heap_size, TotalHeapSize} = erlang:process_info(Pid, total_heap_size),
    io_lib:format("~.2f M", [TotalHeapSize*8/1024/1024]);
heapsize(Name) when is_atom(Name) ->
    heapsize(whereis(Name)).

timeprint(Time) ->
    io_lib:format("~.2fs", [Time/1000000]).

printheapsize() ->
    case true of
        true ->
            Memory = erlang:memory(),
            Total = proplists:get_value(total, Memory),
            io:format("Heap size: local ~s ht ~s total ~.2f M~n", [heapsize(self()), heapsize(ht), Total/1024/1024]);
        false ->
            none
    end.

main(_) ->
    %%eprof:start(),
    lager:start(),
    lager:set_loglevel(lager_console_backend, debug),
    FirstBatch = 5000000,
    SecondBatch = 5000000,
    ThirdBatch = 1000000,

    LagerPath = "ebin",
    case code:add_path(LagerPath) of
        true ->
            ok;
        {error, bad_directory} ->
            io:format("Could not add path ~p~n", [LagerPath]),
            halt(1)
    end,
    ht:start_link(),

    printheapsize(),
    %%eprof:start_profiling([ht]),
    {Time1, _Tree} = timer:tc(fun () -> ht:reset_tree([FirstBatch]) end),
    %%eprof:stop_profiling(),
    io:format("Init with ~p entries: ~s~n", [FirstBatch+1, timeprint(Time1)]),
    printheapsize(),

    {Time2a, _} = timer:tc(fun () -> ht:root(FirstBatch div 2) end),
    io:format("Build half tree: ~s~n", [timeprint(Time2a)]),
    printheapsize(),
    {Time2b, _} = timer:tc(fun () -> ht:root(FirstBatch - 10) end),
    io:format("Build almost whole tree: ~s~n", [timeprint(Time2b)]),
    printheapsize(),
    {Time2c, _} = timer:tc(fun () -> ht:root() end),
    io:format("Build whole tree: ~s~n", [timeprint(Time2c)]),
    printheapsize(),
    {Time2d, _} = timer:tc(fun () -> ht:root() end),
    io:format("Build tree again: ~s~n", [timeprint(Time2d)]),
    printheapsize(),
    io:format("treesize: ~p~n", [ht:size()]),

    {Time3, _} = timer:tc(fun () -> ht:load_tree(FirstBatch + SecondBatch) end),
    io:format("Load up to ~p entries: ~s~n", [FirstBatch + SecondBatch + 1, timeprint(Time3)]),
    printheapsize(),

    {Time4c, _} = timer:tc(fun () -> ht:root() end),
    io:format("Build whole tree: ~s~n", [timeprint(Time4c)]),
    printheapsize(),
    {Time4d, _} = timer:tc(fun () -> ht:root() end),
    io:format("Build tree again: ~s~n", [timeprint(Time4d)]),
    printheapsize(),
    io:format("treesize: ~p~n", [ht:size()]),


    Entries2 = lists:map(fun (_) -> <<"bar">> end, lists:seq(1, ThirdBatch)),
    {Time5, _} = timer:tc(fun () -> add_entries(Entries2) end),
    io:format("Add ~p entries: ~s~n", [length(Entries2), timeprint(Time5)]),
    printheapsize(),
    {Time6, _} = timer:tc(fun () -> ht:root() end),
    io:format("Build tree: ~s~n", [timeprint(Time6)]),
    printheapsize(),
    garbage_collect(self()),
    garbage_collect(whereis(ht)),
    printheapsize(),
    %%eprof:analyze(),
    ok.