Skip to content

Commit f0da85e

Browse files
vouillonhhugo
authored andcommitted
Benchmark: bin_prot
1 parent 25139b9 commit f0da85e

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

bench.Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ RUN opam exec -- dune exec tools/ci_setup.exe ../janestreet . \
4747
&& opam remove js_of_ocaml-compiler ojs \
4848
&& opam clean
4949

50+
# Bin_prot packages
51+
RUN opam pin add -n https://github.com/ocaml-wasm/bin_prot.git#wasm-v0.18 \
52+
&& opam install ppx_bin_prot
53+
5054
# Copy sources
5155
COPY --chown=opam:opam . ./
5256

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.PHONY: bench perform
2+
3+
export NAME=Others
4+
export SUBNAME=bin_prot
5+
6+
SHELL=/bin/bash -o pipefail
7+
8+
bench:
9+
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): starting"
10+
dune build --profile release --root .
11+
node _build/default/bench.bc.js 400000
12+
$(MAKE) perform COMPILER=js_of_ocaml SCRIPT=_build/default/bench.bc.js KIND=js
13+
$(MAKE) perform COMPILER=wasm_of_ocaml SCRIPT=_build/default/bench.bc.wasm.js KIND=wasm
14+
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): done"
15+
16+
perform:
17+
/usr/bin/time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) 2>&1 1> /dev/null | \
18+
tee /dev/stderr | \
19+
sh ../../utils/format_metrics.sh exec | \
20+
sh ../../utils/aggregate.sh $(KIND)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
open Bin_prot.Std
2+
3+
type element =
4+
{ a : string
5+
; b : string
6+
; c : string
7+
; d : bool
8+
; e : bool
9+
; f : bool
10+
; g : string option
11+
; h : bool
12+
; i : bool
13+
; j : bool
14+
; k : bool
15+
; l : bool
16+
; m : bool
17+
; n : bool
18+
; o : string option
19+
; p : bool
20+
; q : bool
21+
; r : int
22+
; s : int
23+
; t : int
24+
; u : int
25+
; v : string list (* these are small - 1-5 *)
26+
}
27+
[@@deriving bin_io]
28+
29+
let s = "abcdefabcdefabcdef"
30+
31+
let v = [ s; s; s; s ]
32+
33+
let x =
34+
{ a = s
35+
; b = s
36+
; c = s
37+
; d = true
38+
; e = true
39+
; f = true
40+
; g = Some s
41+
; h = true
42+
; i = true
43+
; j = true
44+
; k = true
45+
; l = true
46+
; m = true
47+
; n = true
48+
; o = Some s
49+
; p = true
50+
; q = true
51+
; r = 65537
52+
; s = 65537
53+
; t = 65537
54+
; u = 65537
55+
; v
56+
}
57+
58+
type t = element list [@@deriving bin_io]
59+
60+
let rec f acc n = if n = 0 then acc else f (x :: acc) (n - 1)
61+
62+
let () =
63+
if Array.length Sys.argv > 1
64+
then (
65+
let count = int_of_string Sys.argv.(1) in
66+
let l = f [] count in
67+
let len = [%bin_size: t] l in
68+
let b = Bin_prot.Common.create_buf len in
69+
ignore ([%bin_write: t] b ~pos:0 l : int);
70+
let s = Bytes.create len in
71+
Bin_prot.Common.blit_buf_string ~src_pos:0 b ~dst_pos:0 s ~len;
72+
Out_channel.with_open_bin "data" @@ fun ch -> Out_channel.output_bytes ch s)
73+
else
74+
let s = In_channel.with_open_bin "data" @@ In_channel.input_all in
75+
let len = String.length s in
76+
let b = Bin_prot.Common.create_buf len in
77+
Bin_prot.Common.blit_string_buf ~src_pos:0 s ~dst_pos:0 b ~len;
78+
let t = Unix.gettimeofday () in
79+
for _ = 0 to 4 do
80+
ignore ([%bin_read: t] b ~pos_ref:(ref 0) : t)
81+
done;
82+
let t' = Unix.gettimeofday () in
83+
Format.printf "%.2f@." (t' -. t)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(executables
2+
(names bench)
3+
(modes js wasm)
4+
(js_of_ocaml
5+
(flags --opt 2))
6+
(wasm_of_ocaml
7+
(flags --opt 2))
8+
(preprocess
9+
(pps ppx_bin_prot))
10+
(libraries unix))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 3.19)

0 commit comments

Comments
 (0)