forked from ocaml-community/yojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.ml
66 lines (57 loc) · 2.08 KB
/
bench.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
open Core
open Core_bench
let data =
In_channel.read_all "bench.json"
let yojson_data = Yojson.Safe.from_string data
(* chosen by fair dice roll, guaranteed to be large *)
let large = 10_000
let large_int_assoc =
let ints = List.init large ~f:(fun n ->
(string_of_int n, `Int n))
in
`Assoc ints
let large_int_list =
let ints = List.init large ~f:(fun n -> `Int n) in
`List ints
let large_string_list =
let strings = List.init large ~f:(fun n ->
`String (string_of_int n))
in
`List strings
let streamable_string =
let buf = Buffer.create (large * 100) in
for i = 1 to large do
Printf.bprintf buf "%d\n" i
done;
Buffer.contents buf
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));
Bench.Test.create ~name:"JSON writing assoc" (fun () ->
ignore (Yojson.Safe.to_string large_int_assoc));
Bench.Test.create ~name:"JSON writing int list" (fun () ->
ignore (Yojson.Safe.to_string large_int_list));
Bench.Test.create ~name:"JSON writing string list" (fun () ->
ignore (Yojson.Safe.to_string large_string_list));
Bench.Test.create ~name:"JSON writing int list to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_int_list)));
Bench.Test.create ~name:"JSON writing string list to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_string_list)));
Bench.Test.create ~name:"JSON writing assoc to channel" (fun () ->
Out_channel.with_file "/dev/null" ~f:(fun oc ->
ignore (Yojson.Safe.to_channel oc large_int_assoc)));
begin
let buf = Buffer.create 1000 in
Bench.Test.create ~name:"JSON seq roundtrip" (fun () ->
let stream = Yojson.Safe.seq_from_string ~buf streamable_string in
ignore (Yojson.Safe.seq_to_string ~buf stream)
)
end;
])
let () =
main ()