Skip to content

Commit

Permalink
Merge pull request #197 from Cjen1/benchmark
Browse files Browse the repository at this point in the history
Add echo benchmark test
  • Loading branch information
talex5 authored May 12, 2020
2 parents 94db5c1 + 74c7470 commit baf1bdd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test-bin/echo/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(executable
(name echo_bench)
(libraries lwt.unix capnp-rpc capnp-rpc-lwt capnp-rpc-net capnp-rpc-unix logs.fmt)
(flags (:standard -w -53-55)))

(rule
(targets echo_api.ml echo_api.mli)
(deps echo_api.capnp)
(action (run capnpc -o %{bin:capnpc-ocaml} %{deps})))
29 changes: 29 additions & 0 deletions test-bin/echo/echo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Api = Echo_api.MakeRPC(Capnp_rpc_lwt)

open Lwt.Infix
open Capnp_rpc_lwt

(*-- Server ----------------------------------------*)
let local =
let module Echo = Api.Service.Echo in

Echo.local @@ object
inherit Echo.service

method ping_impl params release_param_caps =
let open Echo.Ping in
let msg = Params.msg_get params in
release_param_caps ();
let response, results = Service.Response.create Results.init_pointer in
Results.reply_set results ("echo:" ^ msg);
Service.return response
end

(*-- Client ----------------------------------------*)
module Echo = Api.Client.Echo

let ping t msg =
let open Echo.Ping in
let request, params = Capability.Request.create Params.init_pointer in
Params.msg_set params msg;
Capability.call_for_value_exn t method_id request >|= Results.reply_get
5 changes: 5 additions & 0 deletions test-bin/echo/echo_api.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@0xb13fc2f2a4c1d65b;

interface Echo {
ping @0 (msg :Text) -> (reply :Text);
}
44 changes: 44 additions & 0 deletions test-bin/echo/echo_bench.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

open Lwt.Infix

open Capnp_rpc_lwt

let () =
Logs.set_level (Some Logs.Info);
Logs.set_reporter (Logs_fmt.reporter ())

let run_client service =
let n = 100000 in
let ops = List.init n (fun i ->
let payload = Int.to_string i in
let desired_result = "echo:" ^ payload in
fun () ->
Echo.ping service payload >|= fun res ->
assert (res = desired_result)
) in
let st = Unix.gettimeofday () in
Lwt_list.iter_p (fun v -> v ()) ops >>= fun () ->
let ed = Unix.gettimeofday () in
let rate = (Int.to_float n) /. (ed -. st) in
Logs.info (fun m -> m "rate = %f" rate );
Lwt.return_unit

let secret_key = `Ephemeral
let listen_address = `TCP ("127.0.0.1", 7000)

let start_server () =
let config = Capnp_rpc_unix.Vat_config.create ~secret_key ~serve_tls:false listen_address in
let service_id = Capnp_rpc_unix.Vat_config.derived_id config "main" in
let restore = Capnp_rpc_net.Restorer.single service_id Echo.local in
Capnp_rpc_unix.serve config ~restore >|= fun vat ->
Capnp_rpc_unix.Vat.sturdy_uri vat service_id

let () =
Lwt_main.run begin
start_server () >>= fun uri ->
Fmt.pr "Connecting to echo service at: %a@." Uri.pp_hum uri;
let client_vat = Capnp_rpc_unix.client_only_vat () in
let sr = Capnp_rpc_unix.Vat.import_exn client_vat uri in
Sturdy_ref.connect_exn sr >>= fun proxy ->
run_client proxy
end
Empty file added test-bin/echo/echo_bench.mli
Empty file.

0 comments on commit baf1bdd

Please sign in to comment.