Skip to content

Commit

Permalink
add an example extracted from a bench
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Nov 29, 2023
1 parent fa9ac42 commit 6e968ff
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@
(modules file_server) ; just check that it compiles
(package pbrt_services)
(libraries pbrt pbrt_yojson pbrt_services))

(rule
(targets orgchart.ml orgchart.mli)
(deps orgchart.proto)
(action
(run ocaml-protoc --pp --binary --ml_out ./ %{deps})))

(test
(name orgchart_ml)
(modules orgchart orgchart_ml)
(package pbrt)
(libraries pbrt))
19 changes: 19 additions & 0 deletions src/examples/orgchart.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

syntax = "proto3";

message Person {
string name = 1;
sint64 age = 2;
}

message Store {
string address = 1;
repeated Person employees = 2;
repeated Person clients = 3;
}

message Company {
string name = 1;
repeated Store stores = 2;
repeated Company subsidiaries = 3;
}
81 changes: 81 additions & 0 deletions src/examples/orgchart_ml.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
open Orgchart

let spf = Printf.sprintf

(* company, with [n] stores and [2^depth] subsidiaries *)
let rec mk_company ~n ~depth : company =
{
name = "bigcorp";
subsidiaries =
(if depth = 0 then
[]
else (
let c = mk_company ~n ~depth:(depth - 1) in
[ c; c ]
));
stores =
List.init n (fun i ->
{
address = spf "%d foobar street" i;
clients =
List.init 2 (fun j ->
{
name = spf "client_%d_%d" i j;
age = Int64.of_int ((j mod 30) + 15);
});
employees =
List.init 2 (fun j ->
{
name = spf "employee_%d_%d" i j;
age = Int64.of_int ((j mod 30) + 18);
});
});
}

let test ~n ~depth () : unit =
let c = mk_company ~n ~depth in
let enc = Pbrt.Encoder.create () in
encode_pb_company c enc;
let str = Pbrt.Encoder.to_string enc in
let c2 =
let dec = Pbrt.Decoder.of_string str in
decode_pb_company dec
in

if c <> c2 then (
Format.eprintf "c=%a@." pp_company c;
Format.eprintf "dec(enc(c))=%a@." pp_company c2;
failwith @@ spf "failed for n=%d, depth=%d" n depth
)
(* else Printf.eprintf "ok for n=%d, depth=%d\n%!" n depth *)

let () =
List.iter
(fun (n, depth) -> test ~n ~depth ())
[
1, 1;
1, 1;
1, 4;
1, 6;
1, 10;
2, 1;
2, 4;
2, 6;
2, 10;
5, 1;
5, 4;
5, 6;
10, 1;
10, 2;
10, 3;
10, 4;
20, 1;
50, 1;
20, 3;
20, 4;
50, 1;
50, 3;
50, 4;
100, 1;
100, 3;
]

0 comments on commit 6e968ff

Please sign in to comment.