Skip to content

Commit c89b675

Browse files
committed
Try running benchmarks on CI
1 parent 5c8c4d5 commit c89b675

3 files changed

Lines changed: 112 additions & 9 deletions

File tree

Makefile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,28 @@ docs-open: ## Open odoc docs with default web browser
128128
docs-serve: docs docs-open ## Open odoc docs with default web browser
129129

130130
.PHONY: build-bench
131-
build-bench: ## Run benchmark
132-
$(DUNE) build --profile=release
131+
build-bench: ## Build benchmark executables
132+
$(DUNE) build --profile=release benchmark/bench.exe
133133

134134
.PHONY: bench
135-
bench: build-bench ## Run benchmark
136-
@$(DUNE) exec benchmark/main.exe --profile=release --display-separate-messages --no-print-directory
135+
bench: build-bench ## Run benchmarks
136+
@$(DUNE) exec benchmark/bench.exe --profile=release --display-separate-messages --no-print-directory
137+
138+
.PHONY: bench-json
139+
bench-json: build-bench ## Run benchmarks with JSON output for CI
140+
@$(DUNE) exec benchmark/bench.exe --profile=release --display-separate-messages --no-print-directory -- --json > bench_results.json
137141

138142
.PHONY: bench-watch
139143
bench-watch: build-bench ## Run benchmark in watch mode
140-
@$(DUNE) exec benchmark/main.exe --profile=release --display-separate-messages --no-print-directory --watch
144+
@$(DUNE) exec benchmark/bench.exe --profile=release --display-separate-messages --no-print-directory --watch
145+
146+
.PHONY: bench-core
147+
bench-core: ## Run Core_bench benchmarks (more detailed)
148+
$(DUNE) build --profile=release benchmark/main.exe
149+
@$(DUNE) exec benchmark/main.exe --profile=release --display-separate-messages --no-print-directory
141150

142-
.PHONY: once
143-
bench-once: ## Run benchmark once
151+
.PHONY: bench-once
152+
bench-once: ## Run benchmark once (allocation analysis)
144153
@$(DUNE) exec _build/default/benchmark/once.exe
145154

146155
.PHONY: bench-once-watch

benchmark/bench.ml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
(* Benchmark runner with JSON output for github-action-benchmark (customBiggerIsBetter) *)
2+
3+
let json_mode = ref false
4+
let iterations = 10000
5+
6+
type benchmark_result = { name : string; ops_per_sec : float }
7+
8+
let measure_benchmark ~name render_fn =
9+
for _ = 1 to 100 do
10+
let _ = render_fn () in
11+
()
12+
done;
13+
Gc.full_major ();
14+
Gc.compact ();
15+
let start = Unix.gettimeofday () in
16+
for _ = 1 to iterations do
17+
let _ = render_fn () in
18+
()
19+
done;
20+
let elapsed = Unix.gettimeofday () -. start in
21+
let ops_per_sec = float_of_int iterations /. elapsed in
22+
{ name; ops_per_sec }
23+
24+
let print_result r = Printf.printf "%-35s %12.0f ops/sec\n" r.name r.ops_per_sec
25+
26+
let print_results_table results =
27+
Printf.printf "\n%s\n" (String.make 50 '=');
28+
Printf.printf "server-reason-react Benchmarks\n";
29+
Printf.printf "%s\n" (String.make 50 '=');
30+
Printf.printf "%-35s %12s\n" "Benchmark" "Throughput";
31+
Printf.printf "%s\n" (String.make 50 '-');
32+
List.iter print_result results;
33+
Printf.printf "%s\n" (String.make 50 '=')
34+
35+
let print_results_json results =
36+
let json_entries =
37+
List.map
38+
(fun r -> Printf.sprintf {| {"name": "%s", "unit": "ops/sec", "value": %.2f}|} r.name r.ops_per_sec)
39+
results
40+
in
41+
print_endline "[";
42+
print_endline (String.concat ",\n" json_entries);
43+
print_endline "]"
44+
45+
let () =
46+
let args = Array.to_list Sys.argv in
47+
json_mode := List.mem "--json" args;
48+
49+
let open Benchmark_scenarios in
50+
let results =
51+
[
52+
measure_benchmark ~name:"trivial/renderToStaticMarkup" (fun () -> ReactDOM.renderToStaticMarkup (Trivial.make ()));
53+
measure_benchmark ~name:"trivial/renderToString" (fun () -> ReactDOM.renderToString (Trivial.make ()));
54+
measure_benchmark ~name:"depth/10" (fun () -> ReactDOM.renderToStaticMarkup (DeepTree.Depth10.make ()));
55+
measure_benchmark ~name:"depth/25" (fun () -> ReactDOM.renderToStaticMarkup (DeepTree.Depth25.make ()));
56+
measure_benchmark ~name:"depth/50" (fun () -> ReactDOM.renderToStaticMarkup (DeepTree.Depth50.make ()));
57+
measure_benchmark ~name:"depth/100" (fun () -> ReactDOM.renderToStaticMarkup (DeepTree.Depth100.make ()));
58+
measure_benchmark ~name:"width/10" (fun () -> ReactDOM.renderToStaticMarkup (WideTree.Wide10.make ()));
59+
measure_benchmark ~name:"width/100" (fun () -> ReactDOM.renderToStaticMarkup (WideTree.Wide100.make ()));
60+
measure_benchmark ~name:"width/500" (fun () -> ReactDOM.renderToStaticMarkup (WideTree.Wide500.make ()));
61+
measure_benchmark ~name:"table/10" (fun () -> ReactDOM.renderToStaticMarkup (Table.Table10.make ()));
62+
measure_benchmark ~name:"table/100" (fun () -> ReactDOM.renderToStaticMarkup (Table.Table100.make ()));
63+
measure_benchmark ~name:"table/500" (fun () -> ReactDOM.renderToStaticMarkup (Table.Table500.make ()));
64+
measure_benchmark ~name:"props/small" (fun () -> ReactDOM.renderToStaticMarkup (PropsHeavy.Small.make ()));
65+
measure_benchmark ~name:"props/medium" (fun () -> ReactDOM.renderToStaticMarkup (PropsHeavy.Medium.make ()));
66+
measure_benchmark ~name:"props/large" (fun () -> ReactDOM.renderToStaticMarkup (PropsHeavy.Large.make ()));
67+
measure_benchmark ~name:"realworld/ecommerce24" (fun () ->
68+
ReactDOM.renderToStaticMarkup (Ecommerce.Products24.make ()));
69+
measure_benchmark ~name:"realworld/dashboard" (fun () -> ReactDOM.renderToStaticMarkup (Dashboard.make ()));
70+
measure_benchmark ~name:"realworld/blog50" (fun () -> ReactDOM.renderToStaticMarkup (Blog.Blog50.make ()));
71+
measure_benchmark ~name:"realworld/form" (fun () -> ReactDOM.renderToStaticMarkup (Form.make ()));
72+
]
73+
in
74+
75+
if !json_mode then print_results_json results else print_results_table results

benchmark/dune

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
; Main benchmark dune file
2+
3+
; CI benchmark runner - simple, JSON-compatible output for github-action-benchmark
4+
5+
(executable
6+
(name bench)
7+
(modules bench)
8+
(libraries
9+
unix
10+
benchmark_scenarios
11+
server-reason-react.react
12+
server-reason-react.reactDom)
13+
(preprocess
14+
(pps server-reason-react.ppx)))
15+
216
; Legacy executables (keeping for compatibility)
317

418
(executable
@@ -32,10 +46,15 @@
3246

3347
(rule
3448
(alias bench)
49+
(action
50+
(run ./bench.exe)))
51+
52+
(rule
53+
(alias bench-json)
3554
(action
3655
(with-stdout-to
37-
run.csv
38-
(run ./main.exe))))
56+
bench_results.json
57+
(run ./bench.exe --json))))
3958

4059
(rule
4160
(alias bench-once)

0 commit comments

Comments
 (0)