Skip to content

Instantly share code, notes, and snippets.

@talex5
Created June 1, 2013 09:29
Show Gist options
  • Save talex5/5689795 to your computer and use it in GitHub Desktop.
Save talex5/5689795 to your computer and use it in GitHub Desktop.
Test driver for benchmarks
(* ocamlfind ocamlc -o quick -package unix -linkpkg quick.ml *)
(* a tiny C binary that prints argv[0], argv[1] and argv[2] *)
let test_binary = "/home/tal/tmp/run-bench/fast";;
let expected_output = test_binary ^ "\none\ntwo\n";;
let subjects = [
("ATS", "/home/tal/Projects/ats/runenv");
("OCaml (native)", "/home/tal/Projects/ocaml/runenv-opt");
("Haskell", "/home/tal/Projects/Haskell/runenv/Runenv");
("OCaml (bytecode)", "/home/tal/Projects/ocaml/runenv-c");
("Rust", "/home/tal/Projects/rust/runenv");
("C#/Mono (cheating)", "/home/tal/Projects/mono/hello.exe");
(* ("C#/Mono (cheating, bundle)", "/home/tal/Projects/mono/hello"); *)
("Python2", "/home/tal/tmp/run-bench/runenv2.py");
("Python3", "/home/tal/tmp/run-bench/runenv3.py")
];;
open Unix;;
let () = putenv "0install-runenv-TEST" ("[\"" ^ test_binary ^ "\", \"one\"]");;
let n_base = 10;;
let time_of_each_run = 0.1;;
let invoke_child prog argv =
let readme, writeme = Unix.pipe () in
let child_pid: int = create_process prog argv stdin writeme stderr in
close writeme;
let in_channel = Unix.in_channel_of_descr readme in
let data = ref "" in
begin
try
while true do
data := (!data) ^ (input_line in_channel) ^ "\n"
done
with End_of_file -> ()
end;
Unix.close readme;
let (pid, status) = waitpid [] child_pid in
let () = match status with
| WEXITED 0 -> ()
| _ -> failwith("Exit status") in
if (!data) <> expected_output then
failwith("Got:\n" ^ !data ^ "Not:\n" ^ expected_output);
()
;;
let base_time =
let start_time = gettimeofday() in
for x = 1 to n_base do
invoke_child test_binary
(Array.of_list [test_binary; "one"; "two"])
done;
let end_time = gettimeofday() in
1000. *. (end_time -. start_time) /. (float_of_int n_base);;
Printf.printf "% 6.2f ms per call : Baseline\n" base_time;;
let test name subject n =
if Sys.file_exists "TEST" then
unlink "TEST";
symlink subject "TEST";
let size = (stat subject).st_size in
let start_time = gettimeofday() in
for x = 1 to n do invoke_child "./TEST" (Array.of_list ["./TEST"; "two"]) done;
let end_time = gettimeofday() in
let time_ms = 1000. *. (end_time -. start_time) /. (float_of_int n) in
(time_ms, (Printf.sprintf "% 6.2f (+% 6.2f) [%3.0f per second] [%7.2f KB] %s"
time_ms
(time_ms -. base_time)
((float_of_int n) /. (end_time -. start_time))
((float_of_int size) /. 1024.)
name))
;;
let results = List.map
(fun (name, s) -> (test name s 10))
subjects;;
List.map
(fun (time, s) -> print_endline(s))
(List.sort (fun (a, _) (b, _) -> (compare a b)) results)
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment