Skip to content

Instantly share code, notes, and snippets.

@rneswold
Last active November 1, 2016 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rneswold/b47e3ce141ae12e10695972de2fba68f to your computer and use it in GitHub Desktop.
Save rneswold/b47e3ce141ae12e10695972de2fba68f to your computer and use it in GitHub Desktop.
Benchmarking module for LWT streams
module TestStream = Nstream
let n_stream = 1_000 (* # of items to pre-load stream. *)
let n_threads = 1_000 (* # of threads reading the stream. *)
let s =
let s, p = TestStream.create () in
begin
for ii = 1 to n_stream do
p (Some ii)
done;
s
end
(* The 'i' argument is the thread "ID". It's included in case we
need to print thread info. *)
let rec thr i s = function
| 0 -> Lwt.return_unit
| n -> let%lwt _ = TestStream.next s in
thr i s (n - 1)
let rec spawn = function
| 1 -> [thr 1 s n_stream]
| n -> (* This expression is split into two because we need to
guarantee the stream is cloned before we recurse. *)
let t = thr n (TestStream.clone s) n_stream in
t :: spawn (n - 1)
let () =
begin
Lwt_main.run (Lwt.join (spawn n_threads));
Gc.print_stat stdout
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment