Source file bench_common.ml
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
open Irmin.Export_for_backends
let c0 = Mtime_clock.counter ()
let now_us () = Mtime_clock.count c0 |> Mtime.Span.to_us
let last = ref (now_us ())
let dt_us () =
let l = now_us () in
let d = l -. !last in
last := l;
d
let reporter ?(prefix = "") () =
let report src level ~over k msgf =
let k _ =
over ();
k ()
in
let ppf = match level with Logs.App -> Fmt.stdout | _ -> Fmt.stderr in
let with_stamp h _tags k fmt =
let dt = dt_us () in
Fmt.kpf k ppf
("%s%+04.0fus %a %a @[" ^^ fmt ^^ "@]@.")
prefix dt Logs_fmt.pp_header (level, h)
Fmt.(styled `Magenta string)
(Logs.Src.name src)
in
msgf @@ fun ? ?tags fmt -> with_stamp header tags k fmt
in
{ Logs.report }
let setup_log style_renderer level =
Fmt_tty.setup_std_outputs ?style_renderer ();
Logs.set_level level;
Logs.set_reporter (reporter ());
()
let reset_stats () =
Index.Stats.reset_stats ();
Irmin_pack.Stats.reset_stats ()
let random_char () = char_of_int (Random.int 256)
let random_string n = String.init n (fun _i -> random_char ())
let random_blob () = random_string 10 |> Bytes.of_string
let random_key () = random_string 5
let default_artefacts_dir =
let ( / ) = Filename.concat in
Unix.getcwd () / "_artefacts" / Uuidm.to_string (Uuidm.v `V4)
let prepare_artefacts_dir path =
let rec mkdir_p path =
if Sys.file_exists path then ()
else
let path' = Filename.dirname path in
if path' = path then failwith "Failed to prepare result dir";
mkdir_p path';
Unix.mkdir path 0o755
in
mkdir_p path
let with_timer f =
let t0 = Sys.time () in
let+ a = f () in
let t1 = Sys.time () -. t0 in
(t1, a)
let with_progress_bar ~message ~n ~unit =
let open Progress in
let config =
Config.v ~max_width:(Some 79) ~min_interval:(Some Duration.(of_sec 0.5)) ()
in
let bar =
Line.(
list
[
const message;
count_to n;
const unit;
elapsed ();
parens (const "ETA: " ++ eta n);
bar n;
percentage_of n;
])
in
with_reporter ~config bar
module Conf = Irmin_tezos.Conf
module Schema = struct
open Irmin
module Metadata = Metadata.None
module Contents = Contents.String
module Path = Path.String_list
module Branch = Branch.String
module Hash = Hash.SHA1
module Node = Node.Make (Hash) (Path) (Metadata)
module Commit = Commit.Make (Hash)
module Info = Info.Default
end
module Info (I : Irmin.Info.S) = struct
let f () = I.v ~author:"tests" ~message:"commit" 0L
end
module FSHelper = struct
let file f =
try (Unix.stat f).st_size with Unix.Unix_error (Unix.ENOENT, _, _) -> 0
let dict root = file (Irmin_pack.Layout.V1_and_v2.dict ~root) / 1024 / 1024
let pack root = file (Irmin_pack.Layout.V1_and_v2.pack ~root) / 1024 / 1024
let index root =
let index_dir = Filename.concat root "index" in
let a = file (Filename.concat index_dir "data") in
let b = file (Filename.concat index_dir "log") in
let c = file (Filename.concat index_dir "log_async") in
(a + b + c) / 1024 / 1024
let size root = dict root + pack root + index root
let get_size root = size root
let rm_dir root =
if Sys.file_exists root then (
let cmd = Printf.sprintf "rm -rf %s" root in
[%logs.info "exec: %s" cmd];
let _ = Sys.command cmd in
())
end
module Generate_trees
(Store : Irmin.Generic_key.KV with type Schema.Contents.t = bytes) =
struct
let key depth =
let rec aux i acc =
if i >= depth then acc
else
let k = random_key () in
aux (i + 1) (k :: acc)
in
aux 0 []
let chain_tree tree depth path =
let k = path @ key depth in
Store.Tree.add tree k (random_blob ())
let add_chain_trees depth nb tree =
let path = key 2 in
let rec aux i tree =
if i >= nb then Lwt.return tree
else
let* tree = chain_tree tree depth path in
aux (i + 1) tree
in
aux 0 tree
let large_tree path tree width =
let rec aux i tree =
if i >= width then Lwt.return tree
else
let k = path @ [ random_key () ] in
let* tree = Store.Tree.add tree k (random_blob ()) in
aux (i + 1) tree
in
aux 0 tree
let add_large_trees width nb tree =
let path = key 1 in
let rec aux i tree =
if i >= nb then Lwt.return tree
else
let path = path @ [ random_key () ] in
let* tree = large_tree path tree width in
aux (i + 1) tree
in
aux 0 tree
end