let benchmark_01 (module Test_db : TEST_DATABASE) uri_string
?(collection = 200) ?(big_string_kb = 10) () =
let open Test_db in
let open Trakeva.Action in
let benches = ref [] in
let add_bench s t = benches := (s, t) :: !benches in
DB.load uri_string
>>= fun db ->
let bench_function s f =
let b = Test.now () in
f ()
>>= fun x ->
let e = Test.now () in
add_bench s (e -. b);
return x
in
let bench_action ~action s =
bench_function s (fun () -> DB.act db ~action:(seq action)) in
let action =
List.init collection (fun i ->
set ~key:(sprintf "k%d" i) ~collection:"c" "small")
in
bench_action ~action (sprintf "%d small strings" collection)
>>= fun _ ->
let bench_in_collection name =
bench_action
(sprintf "Set %d %d KB strings into %s" collection big_string_kb name)
~action:(
List.init collection (fun i ->
let value = String.make (big_string_kb * 1_000) 'B' in
set ~key:(sprintf "k%d" i) ~collection:name value))
>>= fun _ ->
bench_function (sprintf "Get all collection %s" name)
(fun () -> DB.get_all db name)
>>= fun cc ->
let l = List.length cc in
Test.check ["bench01"; "length"; name; Int.to_string l] (l = collection);
bench_function (sprintf "Iterate on all collection %s" name)
(fun () ->
let iter = DB.iterator db ~collection:name in
let rec loop () =
iter ()
>>= function
| Some obj -> loop ()
| None -> return ()
in
loop ())
>>= fun () ->
bench_function (sprintf "Get all %s one by one" name)
(fun () ->
let rec loop n =
if n = 0
then return ()
else (
let key = sprintf "k%d" (n - 1) in
DB.get ~collection:name db ~key
>>= fun v ->
Test.check ["bench01"; key; "not none"; ] (v <> None);
loop (n - 1)
)
in
loop collection)
>>= fun _ ->
return ()
in
bench_in_collection "C1" >>= fun () ->
bench_in_collection "C2" >>= fun () ->
bench_in_collection "C3" >>= fun () ->
bench_in_collection "C3" >>= fun () ->
DB.close db
>>= fun () ->
return (test_name, List.rev !benches)