add a benchmark file for IO

This commit is contained in:
Simon Cruanes 2015-09-09 16:07:34 +02:00
parent c2c344e8fc
commit 1454d82e56
2 changed files with 96 additions and 0 deletions

8
_oasis
View file

@ -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

88
benchs/run_bench_io.ml Normal file
View file

@ -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;
()