-
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.
Merge pull request #76 from Leonidas-from-XIV/revive-benchmark
Revive benchmark
- Loading branch information
Showing
5 changed files
with
39 additions
and
49 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,53 +1,18 @@ | ||
open Printf | ||
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 jsonwheel_data = Json_io.json_of_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 jsonwheel_rd_loop () = | ||
for i = 1 to n do | ||
ignore (Json_io.json_of_string data) | ||
done | ||
|
||
let jsonwheel_wr_loop () = | ||
for i = 1 to n do | ||
ignore (Json_io.string_of_json ~compact:true jsonwheel_data) | ||
done | ||
|
||
let time msg f = | ||
let t1 = Unix.gettimeofday () in | ||
f (); | ||
let t2 = Unix.gettimeofday () in | ||
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 "rd json-wheel" jsonwheel_rd_loop; | ||
time "rd yojson" yojson_rd_loop; | ||
time "rd json-wheel" jsonwheel_rd_loop; | ||
|
||
time "wr yojson" yojson_wr_loop; | ||
time "wr json-wheel" jsonwheel_wr_loop; | ||
time "wr yojson" yojson_wr_loop; | ||
time "wr json-wheel" jsonwheel_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,5 +1,9 @@ | ||
;; doesn't actually build b/c json_io doesn't exist | ||
(executable | ||
(name bench) | ||
(flags (-safe-string)) | ||
(libraries yojson)) | ||
(libraries yojson core_bench core)) | ||
|
||
(alias | ||
(name bench) | ||
(deps bench.json) | ||
(action (run ./bench.exe))) |