Skip to content

Commit

Permalink
Improve promise benchmark
Browse files Browse the repository at this point in the history
Include a test where the domains never sleep, to avoid simply
benchmarking time to wake a domain.
  • Loading branch information
talex5 committed Jan 9, 2023
1 parent e2d014f commit a62f7b8
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions bench/bench_promise.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and response = {
next_request : request Promise.u;
}

(* Simulate other work in the domain, and also prevent it from going to sleep.
Otherwise, we're just measuring how long it takes the OS to wake a sleeping thread. *)
let rec spin () =
Fiber.yield ();
spin ()

(* A client and server exchange these payload values.
Each contains the current message and a resolver which the other party can use to reply. *)

Expand Down Expand Up @@ -49,7 +55,11 @@ let bench_resolved ~clock ~n_iters =
Printf.printf "Reading a resolved promise: %.3f ns\n%!" (1e9 *. (t1 -. t0) /. float n_iters);
assert (!t = n_iters)

let run_bench ~domain_mgr ~clock ~use_domains ~n_iters =
let maybe_spin v fn =
if v then Fiber.first spin fn
else fn ()

let run_bench ~domain_mgr ~spin ~clock ~use_domains ~n_iters =
let init_p, init_r = Promise.create () in
Gc.full_major ();
let _minor0, prom0, _major0 = Gc.counters () in
Expand All @@ -58,28 +68,30 @@ let run_bench ~domain_mgr ~clock ~use_domains ~n_iters =
(fun () ->
if use_domains then (
Eio.Domain_manager.run domain_mgr @@ fun () ->
run_server ~n_iters ~i:0 init_r
maybe_spin spin (fun () -> run_server ~n_iters ~i:0 init_r)
) else (
run_server ~n_iters ~i:0 init_r
maybe_spin spin (fun () -> run_server ~n_iters ~i:0 init_r)
)
)
(fun () ->
run_client ~n_iters ~i:0 init_p
maybe_spin spin (fun () -> run_client ~n_iters ~i:0 init_p)
);
let t1 = Eio.Time.now clock in
let time_total = t1 -. t0 in
let time_per_iter = time_total /. float n_iters in
let _minor1, prom1, _major1 = Gc.counters () in
let prom = prom1 -. prom0 in
Printf.printf "%11b, %8d, %8.2f, %13.4f\n%!" use_domains n_iters (1e9 *. time_per_iter) (prom /. float n_iters)
let domains = Printf.sprintf "%b/%b" use_domains spin in
Printf.printf "%11s, %8d, %8.2f, %13.4f\n%!" domains n_iters (1e9 *. time_per_iter) (prom /. float n_iters)

let main ~domain_mgr ~clock =
bench_resolved ~clock ~n_iters:(10_000_000);
Printf.printf "use_domains, n_iters, ns/iter, promoted/iter\n%!";
[false, 1_000_000;
true, 100_000]
|> List.iter (fun (use_domains, n_iters) ->
run_bench ~domain_mgr ~clock ~use_domains ~n_iters
Printf.printf "domains/spin, n_iters, ns/iter, promoted/iter\n%!";
[false, false, 1_000_000;
true, true, 100_000;
true, false, 100_000]
|> List.iter (fun (use_domains, spin, n_iters) ->
run_bench ~domain_mgr ~spin ~clock ~use_domains ~n_iters
)

let () =
Expand Down

0 comments on commit a62f7b8

Please sign in to comment.