Skip to content

Commit

Permalink
Use core_bench to run timings
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonidas-from-XIV committed Jan 28, 2019
1 parent 0dc72f4 commit 5f31768
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ uninstall:

.PHONY: bench
bench:
@dune build bench/bench.exe
@dune build @bench --force

.PHONY: clean
clean:
Expand All @@ -25,3 +25,4 @@ clean:
.PHONY: test
test:
@dune runtest --force

20 changes: 20 additions & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Benchmark for Yojson
====================

These benchmarks require `Core_bench` which is not a dependency of Yojson,
because it is not part of the regular installation/testing flow. This is solely
meant for developers that are worried about performance changes in Yojson.

This is also why it is deemed appropriate to depend on a benchmarking library
with this many dependencies: we rather spend time writing benchmarks than to
build a benchmarking library if a perfectly good one already exists in OCaml.

Install
-------

`opam install core_benchmark`

Running the benchmark
---------------------

`make bench` in the top level directory.
File renamed without changes.
39 changes: 12 additions & 27 deletions bench/bench.ml
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
open Core
open Core_bench

let data =
let l = ref [] in
try
while true do
l := input_line stdin :: !l
done;
assert false
with End_of_file -> String.concat "\n" (List.rev !l)
In_channel.read_all "bench.json"

let yojson_data = Yojson.Safe.from_string data

let n = 10_000

let yojson_rd_loop () =
for i = 1 to n do
ignore (Yojson.Safe.from_string data)
done

let yojson_wr_loop () =
for i = 1 to n do
ignore (Yojson.Safe.to_string yojson_data)
done

let time msg f =
let t1 = Unix.gettimeofday () in
f ();
let t2 = Unix.gettimeofday () in
Printf.printf "%s: %.3f\n%!" msg (t2 -. t1)
let main () =
Command.run (Bench.make_command [
Bench.Test.create ~name:"JSON reading" (fun () ->
ignore (Yojson.Safe.from_string data));
Bench.Test.create ~name:"JSON writing" (fun () ->
ignore (Yojson.Safe.to_string yojson_data));
])

let () =
time "rd yojson" yojson_rd_loop;

time "wr yojson" yojson_wr_loop;
main ()
3 changes: 2 additions & 1 deletion bench/dune
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(executable
(name bench)
(flags (-safe-string))
(libraries yojson unix))
(libraries yojson core_bench core))

(alias
(name bench)
(deps bench.json)
(action (run ./bench.exe)))

0 comments on commit 5f31768

Please sign in to comment.