-
-
Save c-cube/d89debca006b26972b7eb190eb5da869 to your computer and use it in GitHub Desktop.
small IO benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(executable | |
(name main) | |
(libraries unix)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let n = try int_of_string (Sys.getenv "N") with _ -> 500_000_000 | |
let (let@) f x = f x | |
let main file = | |
let ic = open_in_bin file in | |
let@() = Fun.protect ~finally:(fun () -> close_in_noerr ic) in | |
let count = ref 0 in | |
let buf = Bytes.create (64 * 1024) in | |
let n_zeroes = ref 0 in | |
while !count < n do | |
let to_read = min (Bytes.length buf) (n - !count) in | |
let read = input ic buf 0 to_read in | |
if read=0 then failwith "unexpected EOF"; | |
count := !count + read; | |
(* do some processing *) | |
for i=0 to read-1 do | |
if Bytes.get buf i = '\x00' then incr n_zeroes; | |
done; | |
done; | |
Printf.printf "input contained %d zeroes\n" !n_zeroes | |
let () = | |
let file = Sys.argv.(1) in | |
Printf.printf "read %d bytes from %S\n%!" n file; | |
let time0 = Unix.gettimeofday() in | |
main file; | |
let elapsed = Unix.gettimeofday() -. time0 in | |
Printf.printf "took %.4fs (%.4f MiB/s)\n%!" elapsed (float n /. 1024. /. 1024. /. elapsed); | |
() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
exec dune exec --profile=release ./main.exe -- /dev/zero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment