-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.es
More file actions
executable file
·99 lines (77 loc) · 2.28 KB
/
benchmark.es
File metadata and controls
executable file
·99 lines (77 loc) · 2.28 KB
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
95
96
97
98
99
#!/usr/bin/env escript
%%! -pa ebin/ -pa deps/khash/ebin
-mode(compile).
-export([
benchmark_insert/3,
benchmark_update/4,
benchmark_drain/3
]).
-define(MODULES, [
couch_lru_dict,
couch_lru_ets,
couch_lru_khash
%couch_lru_list
]).
-define(SIZES, [
1,
10,
100,
1000,
10000,
50000,
100000,
500000
]).
-define(OP_COUNT, 100000).
main(_) ->
seed(),
TestMods = shuffle(?MODULES),
Sizes = shuffle(?SIZES),
lists:foreach(fun(Mod) ->
Results = lists:map(fun(Size) ->
{_, Ref} = spawn_monitor(fun() -> bench(Mod, Size) end),
receive {'DOWN', Ref, _, _, Result} -> Result end
end, Sizes),
display(Mod, Results)
end, TestMods).
bench(Mod, Size) ->
Lru0 = Mod:new(),
{ITime, Lru1} = timer:tc(?MODULE, benchmark_insert, [Mod, Lru0, Size]),
{UTime, Lru2} = timer:tc(?MODULE, benchmark_update, [Mod, Lru1, Size, ?OP_COUNT]),
{DTime, _Lru3} = timer:tc(?MODULE, benchmark_drain, [Mod, Lru2, Size]),
exit({Size, ITime, UTime, DTime}).
benchmark_insert(_Mod, Lru, 0) ->
Lru;
benchmark_insert(Mod, Lru, Size) ->
Name = list_to_binary(integer_to_list(Size)),
NewLru = Mod:push(Name, Lru),
benchmark_insert(Mod, NewLru, Size - 1).
benchmark_update(_Mod, Lru, _Size, 0) ->
Lru;
benchmark_update(Mod, Lru, Size, Count) ->
Name = list_to_binary(integer_to_list(random:uniform(Size))),
NewLru = Mod:update(Name, Lru),
benchmark_update(Mod, NewLru, Size, Count - 1).
benchmark_drain(_Mod, Lru, 0) ->
Lru;
benchmark_drain(Mod, Lru, Size) ->
{_, NewLru} = Mod:pop(Lru),
benchmark_drain(Mod, NewLru, Size - 1).
seed() ->
<<
I1:32/unsigned-integer,
I2:32/unsigned-integer,
I3:32/unsigned-integer
>> = crypto:strong_rand_bytes(12),
random:seed({I1, I2, I3}).
display(Mod, Results0) ->
Results = lists:sort(Results0),
io:format("~s:~n", [Mod]),
io:format(" ~8s ~12s ~12s ~12s~n", [size, insert, update, drain]),
lists:foreach(fun({Size, ITime, UTime, DTime}) ->
io:format(" ~8b ~12b ~12b ~12b~n", [Size, ITime, UTime, DTime])
end, Results),
io:format("~n", []).
shuffle(List) ->
Tagged = [{random:uniform(), Item} || Item <- List],
[Item || {_, Item} <- lists:sort(Tagged)].