My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
Changes to /trunk/osmos/tests/osmos_benchmark_1.erl
r53 vs. r55 Compare: vs.  Format:
Revision r55
Go to: 
Project members, sign in to write a code review
/trunk/osmos/tests/osmos_benchmark_1.erl   r53 /trunk/osmos/tests/osmos_benchmark_1.erl   r55
1 -module (osmos_benchmark_1). 1 -module (osmos_benchmark_1).
2 2
3 -export ([ run/3 ]). 3 -export ([ run/3 ]).
4 4
5 run (Seconds, WriterSleepMs, ReaderSleepMs) -> 5 run (Seconds, WriterSleepMs, ReaderSleepMs) ->
6 ok = osmos:start (), 6 ok = osmos:start (),
7 Dir = "tmp." ++ os:getpid (), 7 Dir = "tmp." ++ os:getpid (),
8 Fmt = osmos_table_format:new (term, uint64_sum_delete, 1024), 8 Fmt = osmos_table_format:new (term, uint64_sum_delete, 1024),
9 { ok, table } = osmos:open (table, [ { directory, Dir }, { format, Fmt } ]), 9 { ok, table } = osmos:open (table, [ { directory, Dir }, { format, Fmt } ]),
10 Parent = self (), 10 Parent = self (),
11 Start = now (), 11 Start = now (),
12 proc_lib:spawn (fun () -> 12 proc_lib:spawn (fun () ->
13 loop (Parent, 13 loop (Parent,
14 writer, 14 writer,
15 fun (K) -> 15 fun (K) ->
16 ok = osmos:write (table, K, 1) 16 ok = osmos:write (table, K, 1)
17 end, 17 end,
18 WriterSleepMs, 18 WriterSleepMs,
19 Start, 19 Start,
20 1000000 * Seconds, 20 1000000 * Seconds,
21 []) 21 [])
22 end), 22 end),
23 proc_lib:spawn (fun () -> 23 proc_lib:spawn (fun () ->
24 loop (Parent, 24 loop (Parent,
25 reader, 25 reader,
26 fun (K) -> 26 fun (K) ->
27 case osmos:read (table, K) of 27 case osmos:read (table, K) of
28 not_found -> ok; 28 not_found -> ok;
29 { ok, _ } -> ok 29 { ok, _ } -> ok
30 end 30 end
31 end, 31 end,
32 ReaderSleepMs, 32 ReaderSleepMs,
33 Start, 33 Start,
34 1000000 * Seconds, 34 1000000 * Seconds,
35 []) 35 [])
36 end), 36 end),
37 { WM, WS } = receive { writer, W } -> W end, 37 WStats = receive { writer, W } -> W end,
38 { RM, RS } = receive { reader, R } -> R end, 38 RStats = receive { reader, R } -> R end,
39 Info = osmos:info (table), 39 Info = osmos:info (table),
40 io:format ("~p~n" 40 io:format ("table info: ~p~n"
41 "~f +/- ~f microseconds/write~n" 41 "write stats: ~p~n"
42 "~f +/- ~f microseconds/read~n", 42 "read stats: ~p~n",
43 [Info, WM, WS, RM, RS]), 43 [Info, WStats, RStats]),
44 ok = osmos:close (table), 44 ok = osmos:close (table),
45 ok = osmos:stop (), 45 ok = osmos:stop (),
46 os:cmd ("rm -rf " ++ Dir), 46 os:cmd ("rm -rf " ++ Dir),
47 ok. 47 ok.
48 48
49 loop (Parent, What, F, SleepMs, Start, Max, Acc) -> 49 loop (Parent, What, F, SleepMs, Start, Max, Acc) ->
50 case timer:now_diff (now (), Start) > Max of 50 case timer:now_diff (now (), Start) > Max of
51 false -> 51 false ->
52 K = random_key (), 52 K = random_key (),
53 T0 = now (), 53 T0 = now (),
54 F (K), 54 F (K),
55 DT = timer:now_diff (now (), T0), 55 DT = timer:now_diff (now (), T0),
56 timer:sleep (SleepMs), 56 timer:sleep (SleepMs),
57 loop (Parent, What, F, SleepMs, Start, Max, [DT | Acc]); 57 loop (Parent, What, F, SleepMs, Start, Max, [DT | Acc]);
58 true -> 58 true ->
59 Parent ! { What, stats (Acc) } 59 Parent ! { What, stats (Acc) }
60 end. 60 end.
61 61
62 % want a spectrum of keys from frequent to unique 62 % want a spectrum of keys from frequent to unique
63 random_key () -> 63 random_key () ->
64 random_key (1/16, []). 64 random_key (1/16, []).
65 65
66 random_key (P, Acc) -> 66 random_key (P, Acc) ->
67 case random:uniform () < P of 67 case random:uniform () < P of
68 true -> list_to_binary (Acc); 68 true -> list_to_binary (Acc);
69 false -> random_key (P, [random_byte () | Acc]) 69 false -> random_key (P, [random_byte () | Acc])
70 end. 70 end.
71 71
72 random_byte () -> 72 random_byte () ->
73 lists:sum ([ random:uniform (64) - 1 || _ <- lists:seq (1, 4) ]). 73 lists:sum ([ random:uniform (64) - 1 || _ <- lists:seq (1, 4) ]).
74 74
75 stats (Xs) -> 75 stats (Xs) ->
76 N = length (Xs), 76 N = length (Xs),
77 Mu = lists:sum (Xs) / N, 77 Mean = lists:sum (Xs) / N,
78 Sum = lists:sum ([ begin D = X - Mu, D * D end || X <- Xs ]), 78 Sorted = lists:sort (Xs),
79 Sigma = math:sqrt (Sum / N), 79 Deciles = [ { 10 * Q, lists:nth (round (N * Q / 10), Sorted) }
80 { Mu, Sigma }. 80 || Q <- lists:seq (1, 9) ],
81 [ { mean, Mean }, { deciles, Deciles } ].
Powered by Google Project Hosting