-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0dc72f4
commit 5f31768
Showing
5 changed files
with
36 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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.
This file contains 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
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 () |
This file contains 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
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))) |