Skip to content

Commit

Permalink
Avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Apr 16, 2024
1 parent c91003f commit b8e6a43
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions bench/bench_hashtbl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ end

module Htbl = Hashtbl.Make (Int)

let run_one_domain ~budgetf ?(n_ops = 400 * Util.iter_factor) ?(n_keys = 1000)
~percent_mem ?(percent_add = (100 - percent_mem + 1) / 2)
?(prepopulate = true) () =
let mutex = Mutex.create ()

let run_one ~budgetf ~n_domains ~use_mutex ?(n_keys = 1000) ~percent_mem
?(percent_add = (100 - percent_mem + 1) / 2) ?(prepopulate = true) () =
let limit_mem = percent_mem in
let limit_add = percent_mem + percent_add in

Expand All @@ -26,6 +27,7 @@ let run_one_domain ~budgetf ?(n_ops = 400 * Util.iter_factor) ?(n_keys = 1000)
Htbl.replace t key value
done;

let n_ops = (if use_mutex then 100 else 400) * Util.iter_factor in
let n_ops = (100 + percent_mem) * n_ops / 100 in

let n_ops_todo = Atomic.make 0 |> Multicore_magic.copy_as_padded in
Expand All @@ -34,7 +36,7 @@ let run_one_domain ~budgetf ?(n_ops = 400 * Util.iter_factor) ?(n_keys = 1000)
Atomic.set n_ops_todo n_ops;
Random.State.make_self_init ()
in
let work _ state =
let work_no_mutex _ state =
let rec work () =
let n = Util.alloc n_ops_todo in
if n <> 0 then
Expand Down Expand Up @@ -63,40 +65,7 @@ let run_one_domain ~budgetf ?(n_ops = 400 * Util.iter_factor) ?(n_keys = 1000)
in
work ()
in

let config = Printf.sprintf "one domain, %d%% reads" percent_mem in
Times.record ~budgetf ~n_domains:1 ~init ~work ()
|> Times.to_thruput_metrics ~n:n_ops ~singular:"operation" ~config

let mutex = Mutex.create ()

let run_one ~budgetf ~n_domains ?(n_ops = 400 * Util.iter_factor)
?(n_keys = 1000) ~percent_mem ?(percent_add = (100 - percent_mem + 1) / 2)
?(prepopulate = true) () =
let limit_mem = percent_mem in
let limit_add = percent_mem + percent_add in

assert (0 <= limit_mem && limit_mem <= 100);
assert (limit_mem <= limit_add && limit_add <= 100);

let t = Htbl.create n_keys in

if prepopulate then
for _ = 1 to n_keys do
let value = Random.bits () in
let key = value mod n_keys in
Htbl.replace t key value
done;

let n_ops = (100 + percent_mem) * n_ops / 100 in

let n_ops_todo = Atomic.make 0 |> Multicore_magic.copy_as_padded in

let init _ =
Atomic.set n_ops_todo n_ops;
Random.State.make_self_init ()
in
let work _ state =
let work_mutex _ state =
let rec work () =
let n = Util.alloc n_ops_todo in
if n <> 0 then
Expand Down Expand Up @@ -133,17 +102,21 @@ let run_one ~budgetf ~n_domains ?(n_ops = 400 * Util.iter_factor)
in

let config =
Printf.sprintf "%d worker%s, %d%% reads" n_domains
(if n_domains = 1 then "" else "s")
percent_mem
let percent_mem = Printf.sprintf "%d%% reads" percent_mem in
if use_mutex then
Printf.sprintf "%d worker%s, %s" n_domains
(if n_domains = 1 then "" else "s")
percent_mem
else Printf.sprintf "one domain, %s" percent_mem
in
let work = if use_mutex then work_mutex else work_no_mutex in
Times.record ~budgetf ~n_domains ~init ~work ()
|> Times.to_thruput_metrics ~n:n_ops ~singular:"operation" ~config

let run_suite ~budgetf =
([ 10; 50; 90 ]
|> List.concat_map @@ fun percent_mem ->
run_one_domain ~budgetf ~percent_mem ())
run_one ~budgetf ~n_domains:1 ~use_mutex:false ~percent_mem ())
@ (Util.cross [ 10; 50; 90 ] [ 1; 2; 4; 8 ]
|> List.concat_map @@ fun (percent_mem, n_domains) ->
run_one ~budgetf ~n_domains ~percent_mem ())
run_one ~budgetf ~n_domains ~use_mutex:true ~percent_mem ())

0 comments on commit b8e6a43

Please sign in to comment.