|
| 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 |
0 commit comments