Skip to content

Commit 0d752a2

Browse files
committed
db_benchmark: properly clean up the generated file after each benchmark run
1 parent 41c07dc commit 0d752a2

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/test/db_benchmark/common.ml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,9 @@ module Ops = struct
101101
read_key (module Db) db key
102102
end
103103

104-
(* Temporary directory management *)
105-
let make_temp_dir prefix =
106-
let pid = Unix.getpid () |> Pid.to_int in
107-
let dir_name = Printf.sprintf "%s_%d" prefix pid in
108-
Unix.mkdir_p dir_name ; dir_name
109-
110-
let cleanup_temp_dir dir =
111-
match Sys.file_exists dir with
112-
| `Yes ->
113-
ignore
114-
( Core_unix.system (Printf.sprintf "rm -rf %s" (Filename.quote dir))
115-
: Core_unix.Exit_or_signal.t )
116-
| _ ->
117-
()
104+
let with_temp_dir prefix ~f =
105+
let dir_name = Unix.mkdtemp prefix in
106+
try f dir_name
107+
with e ->
108+
Mina_stdlib_unix.File_system.rmrf dir_name ;
109+
raise e

src/test/db_benchmark/db_benchmark.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ module Single_file_db = Single_file_impl.Make ()
1010

1111
module Multi_file_db = Multi_file_impl.Make ()
1212

13-
let init_db (type db) (module Db : Common.Database with type t = db) name =
13+
let init_db (type db) ~dir (module Db : Common.Database with type t = db) name =
1414
(* Initialization: create DB and warmup *)
15-
let dir = Common.make_temp_dir (Printf.sprintf "db_bench_%s" name) in
1615
let db = Db.create dir in
1716
Common.Ops.warmup (module Db) db ;
1817
eprintf "Warmup complete for %s\n" name ;
@@ -37,8 +36,9 @@ let make_read_bench (type db) (module Db : Common.Database with type t = db)
3736

3837
let test ~name (type db) (module Db : Common.Database with type t = db)
3938
(f : (module Common.Database with type t = db) -> db -> unit -> unit) =
40-
Bench.Test.create_with_initialization ~name (fun `init ->
41-
init_db (module Db) name |> f (module Db) )
39+
Common.with_temp_dir name ~f:(fun dir ->
40+
Bench.Test.create_with_initialization ~name (fun `init () ->
41+
init_db ~dir (module Db) name |> f (module Db) ) )
4242

4343
(* Create all benchmarks *)
4444
let all_benchmarks () =

0 commit comments

Comments
 (0)