From 1454d82e5638c38b541c23f6b57c91d980477a09 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 9 Sep 2015 16:07:34 +0200 Subject: [PATCH] add a benchmark file for IO --- _oasis | 8 ++++ benchs/run_bench_io.ml | 88 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 benchs/run_bench_io.ml diff --git a/_oasis b/_oasis index fcd1e8b0..78236eee 100644 --- a/_oasis +++ b/_oasis @@ -191,6 +191,14 @@ Executable run_bench_hash MainIs: run_bench_hash.ml BuildDepends: containers, containers.misc +Executable run_bench_io + Path: benchs/ + Install: false + CompiledObject: best + Build$: flag(bench) && flag(unix) && flag(lwt) + MainIs: run_bench_io.ml + BuildDepends: containers, unix, lwt.unix, benchmark + Executable run_test_future Path: tests/threads/ Install: false diff --git a/benchs/run_bench_io.ml b/benchs/run_bench_io.ml new file mode 100644 index 00000000..a741486c --- /dev/null +++ b/benchs/run_bench_io.ml @@ -0,0 +1,88 @@ + +let read_input_char file = + CCIO.with_in file + (fun ic -> + let count = ref 0 in + try + while true do + let _ = input_char ic in + incr count + done; + assert false + with End_of_file -> !count + ) + +let read_input file = + CCIO.with_in file + (fun ic -> + let count = ref 0 in + let n = 4096 in + let b = Bytes.make n ' ' in + try + while true do + let n' = input ic b 0 n in + if n'=0 then raise Exit; + count := !count + n' + done; + assert false + with Exit -> + !count + ) + +let read_read file = + let fd = Unix.openfile file [Unix.O_RDONLY] 0o644 in + let count = ref 0 in + let n = 4096 in + let b = Bytes.make n ' ' in + try + while true do + let n' = Unix.read fd b 0 n in + if n'=0 then raise Exit; + count := !count + n' + done; + assert false + with Exit -> + Unix.close fd; + !count + +let read_lwt file = + let open Lwt.Infix in + Lwt_io.with_file ~mode:Lwt_io.input file + (fun ic -> + let n = 4096 in + let b = Bytes.make n ' ' in + let rec read_chunk count = + Lwt_io.read_into ic b 0 n >>= fun n' -> + let count = count + n' in + if n'>0 then read_chunk count else Lwt.return count + in + read_chunk 0 + ) + +let read_lwt' file = Lwt_main.run (read_lwt file) + +let profile ~f file () = (f file) + +let bench file = + let n1 = read_input_char file in + let n2 = read_input file in + let n3 = read_read file in + let n4 = read_lwt' file in + Printf.printf "results: %d, %d, %d, %d\n" n1 n2 n3 n4; + assert (n1=n2 && n2 = n3 && n3=n4); + Benchmark.throughputN ~repeat:5 4 + [ "input_char", profile ~f:read_input_char file, () + ; "input", profile ~f:read_input file, () + ; "Unix.read", profile ~f:read_read file, () + ; "Lwt_io.read", profile ~f:read_lwt' file, () + ] + +let () = + if Array.length Sys.argv < 2 then invalid_arg "use: truc file"; + let file = Sys.argv.(1) in + Printf.printf "read file %s\n" file; + let res = bench file in + Benchmark.tabulate res; + () + +