benchmark Conv.from (from json)

This commit is contained in:
Simon Cruanes 2014-03-02 16:06:33 +01:00
parent 3c4270b3a1
commit a2e0d85dca

View file

@ -40,6 +40,39 @@ let bench_point_list x =
] in
Benchmark.tabulate res
(* conversion back from json *)
let rec point_of_json_manual (j:Conv.Json.t) =
let module P = Point in
match j with
| `Assoc l ->
let x = List.assoc "x" l in
let y = List.assoc "y" l in
let color = List.assoc "color" l in
let prev = List.assoc "prev" l in
let prev = match prev with
| `String "none" -> None
| `List [`String "some"; p'] -> Some (point_of_json_manual p')
| _ -> failwith "expected point"
in
begin match x, y, color with
| `Int x, `Int y, `String color -> P.({x;y;color;prev;})
| _ -> failwith "expected point"
end
| _ -> failwith "expected point"
let points_of_json_manual = function
| `List l -> List.map point_of_json_manual l
| _ -> failwith "expected list of points"
let points_of_json_conv =
Conv.from Conv.Json.source (Conv.Sink.list_ Point.sink)
let bench_point_list_back l =
let res = Benchmark.throughputN 5
[ "conv", points_of_json_conv, l
; "manual", points_of_json_manual, l
] in
Benchmark.tabulate res
let () =
Printf.printf "list of 5 elements...\n";
@ -52,4 +85,9 @@ let () =
let l = Gen.(repeat Point.p |> take 10 |> to_rev_list) in
Printf.printf "list of %d points...\n" (List.length l);
bench_point_list l;
(* convert back from json *)
let l' = conv_list_point_to_json l in
Printf.printf "from JSON...\n";
bench_point_list_back l';
()