Created
June 5, 2025 09:39
-
-
Save davesnx/425c7817c4da725356c0bada7ca1b34c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[@@@warning "-27"] | |
let get_memory_info () = | |
let ic = open_in "/proc/self/status" in | |
let rec read_lines rss vmsize = | |
try | |
let line = input_line ic in | |
if String.starts_with ~prefix:"VmRSS:" line then | |
let rss_kb = String.sub line 6 (String.length line - 9) |> String.trim |> int_of_string in | |
read_lines (Some rss_kb) vmsize | |
else if String.starts_with ~prefix:"VmSize:" line then | |
let vm_kb = String.sub line 7 (String.length line - 10) |> String.trim |> int_of_string in | |
read_lines rss (Some vm_kb) | |
else | |
read_lines rss vmsize | |
with End_of_file -> | |
close_in ic; | |
(rss, vmsize) | |
in | |
read_lines None None | |
let measure_extreme title f = | |
let (rss_before, vm_before) = get_memory_info () in | |
let gc_before = Gc.stat () in | |
let start_time = Unix.gettimeofday () in | |
Printf.printf "\n=== %s ===\n" title; | |
Printf.printf "π START: RSS=%d KB, VM=%d KB\n" | |
(Option.value rss_before ~default:0) | |
(Option.value vm_before ~default:0); | |
let result = | |
try | |
let res = f () in | |
Printf.printf "β SUCCESS\n"; | |
Some res | |
with | |
| Stack_overflow -> | |
Printf.printf "β STACK OVERFLOW\n"; | |
Printf.printf "π§ Try: ulimit -s 32768\n"; | |
None | |
| Out_of_memory -> | |
Printf.printf "β OUT OF MEMORY\n"; | |
Printf.printf "π§ System hit memory limit\n"; | |
None | |
| exn -> | |
Printf.printf "β EXCEPTION: %s\n" (Printexc.to_string exn); | |
Printf.printf "π§ Backtrace: %s\n" (Printexc.get_backtrace ()); | |
None | |
in | |
let end_time = Unix.gettimeofday () in | |
let (rss_after, vm_after) = get_memory_info () in | |
let gc_after = Gc.stat () in | |
Printf.printf "β±οΈ Time: %.3fs\n" (end_time -. start_time); | |
Printf.printf "π§ END: RSS=%d KB, VM=%d KB\n" | |
(Option.value rss_after ~default:0) | |
(Option.value vm_after ~default:0); | |
Printf.printf "π DELTA: RSS=%+d KB, VM=%+d KB\n" | |
((Option.value rss_after ~default:0) - (Option.value rss_before ~default:0)) | |
((Option.value vm_after ~default:0) - (Option.value vm_before ~default:0)); | |
Printf.printf "ποΈ GC: Minor=%.0f, Major=%.0f, Heap=%d words\n" | |
(gc_after.minor_words -. gc_before.minor_words) | |
(gc_after.major_words -. gc_before.major_words) | |
gc_after.heap_words; | |
result | |
(* 1. MASSIVE LISTS *) | |
let test_massive_lists () = | |
Printf.printf "π TESTING MASSIVE LISTS\n"; | |
Printf.printf "========================\n"; | |
let massive_sizes = [50000; 100000; 200000; 500000; 1000000] in | |
List.iter (fun size -> | |
let title = Printf.sprintf "MASSIVE React.list size %d" size in | |
measure_extreme title (fun () -> | |
let list = React.list (List.init size (fun i -> | |
React.string (Printf.sprintf "massive_item_%d" i) | |
)) in | |
let element = React.createElement "div" [] [list] in | |
let result = ReactDOM.renderToStaticMarkup element in | |
String.length result | |
) |> ignore; | |
(* Force cleanup between massive tests *) | |
Gc.full_major (); | |
Gc.compact (); | |
) massive_sizes | |
(* 2. EXTREME NESTING *) | |
let test_extreme_nesting () = | |
Printf.printf "\nποΈ TESTING EXTREME NESTING\n"; | |
Printf.printf "===========================\n"; | |
let extreme_depths = [2000; 5000; 10000; 20000; 50000; 100000] in | |
List.iter (fun depth -> | |
let title = Printf.sprintf "EXTREME nesting depth %d" depth in | |
measure_extreme title (fun () -> | |
let rec create_nested d = | |
if d <= 0 then React.string "extreme_deep" | |
else React.createElement "div" | |
[React.JSX.String ("id", "id", Printf.sprintf "depth_%d" d)] | |
[create_nested (d - 1)] | |
in | |
let element = create_nested depth in | |
let result = ReactDOM.renderToStaticMarkup element in | |
String.length result | |
) |> ignore; | |
Gc.full_major (); | |
) extreme_depths | |
(* 3. COMPLEX NESTED STRUCTURES *) | |
let test_complex_structures () = | |
Printf.printf "\nπ§© TESTING COMPLEX NESTED STRUCTURES\n"; | |
Printf.printf "====================================\n"; | |
let complex_configs = [ | |
(100, 100, 10, "Medium complexity"); | |
(500, 200, 20, "High complexity"); | |
(1000, 500, 50, "Extreme complexity"); | |
(2000, 1000, 100, "Insane complexity"); | |
] in | |
List.iter (fun (list_size, array_size, depth, desc) -> | |
let title = Printf.sprintf "COMPLEX: %s (L:%d A:%d D:%d)" desc list_size array_size depth in | |
measure_extreme title (fun () -> | |
let rec create_complex d = | |
if d <= 0 then | |
React.string "complex_leaf" | |
else | |
let list_children = List.init (list_size / d) (fun i -> | |
React.createElement "span" | |
[React.JSX.String ("class", "class", Printf.sprintf "list_item_%d_%d" d i)] | |
[React.string (Printf.sprintf "List %d-%d" d i)] | |
) in | |
let array_children = Array.init (array_size / d) (fun i -> | |
React.createElement "span" | |
[React.JSX.String ("class", "class", Printf.sprintf "array_item_%d_%d" d i)] | |
[React.string (Printf.sprintf "Array %d-%d" d i)] | |
) in | |
React.createElement "div" | |
[React.JSX.String ("data-depth", "data-depth", string_of_int d)] | |
[ | |
React.list list_children; | |
React.array array_children; | |
create_complex (d - 1) | |
] | |
in | |
let element = create_complex depth in | |
let result = ReactDOM.renderToStaticMarkup element in | |
String.length result | |
) |> ignore; | |
Gc.full_major (); | |
) complex_configs | |
(* 4. MEMORY PRESSURE SIMULATION *) | |
let test_memory_pressure () = | |
Printf.printf "\nπ₯ TESTING UNDER MEMORY PRESSURE\n"; | |
Printf.printf "================================\n"; | |
(* Create background memory pressure *) | |
let pressure_data = Array.make 100000 (String.make 1000 'X') in | |
Printf.printf "π Created 100MB background pressure\n"; | |
let pressure_tests = [10000; 50000; 100000] in | |
List.iter (fun size -> | |
let title = Printf.sprintf "UNDER PRESSURE: React.list size %d" size in | |
measure_extreme title (fun () -> | |
let list = React.list (List.init size (fun i -> | |
React.createElement "div" | |
[React.JSX.String ("data-pressure", "data-pressure", Printf.sprintf "item_%d" i)] | |
[React.string (Printf.sprintf "Pressure test item %d with some extra text to increase memory usage" i)] | |
)) in | |
let element = React.createElement "div" [] [list] in | |
let result = ReactDOM.renderToStaticMarkup element in | |
String.length result | |
) |> ignore; | |
) pressure_tests; | |
(* Keep pressure_data alive *) | |
Printf.printf "π Background pressure still active: %d bytes\n" (Array.length pressure_data) | |
(* 5. RAPID SUCCESSIVE RENDERS *) | |
let test_rapid_renders () = | |
Printf.printf "\nπ TESTING RAPID SUCCESSIVE RENDERS\n"; | |
Printf.printf "===================================\n"; | |
let start_time = Unix.gettimeofday () in | |
let (rss_start, _) = get_memory_info () in | |
for i = 1 to 1000 do | |
let list = React.list (List.init 1000 (fun j -> | |
React.string (Printf.sprintf "rapid_%d_%d" i j) | |
)) in | |
let element = React.createElement "div" [] [list] in | |
let _ = ReactDOM.renderToStaticMarkup element in | |
if i mod 100 = 0 then ( | |
let (rss_now, _) = get_memory_info () in | |
Printf.printf "π Render %d: RSS=%d KB (+%d KB)\n" | |
i (Option.value rss_now ~default:0) | |
((Option.value rss_now ~default:0) - (Option.value rss_start ~default:0)); | |
); | |
done; | |
let end_time = Unix.gettimeofday () in | |
let (rss_end, _) = get_memory_info () in | |
Printf.printf "β 1000 renders completed in %.3fs\n" (end_time -. start_time); | |
Printf.printf "π Total RSS change: %+d KB\n" | |
((Option.value rss_end ~default:0) - (Option.value rss_start ~default:0)) | |
let main () = | |
Printf.printf "π₯ EXTREME STRESS TEST STARTING\n"; | |
Printf.printf "===============================\n"; | |
Printf.printf "Server specs: %d processors, %s KB stack\n" | |
(Unix.open_process_in "nproc" |> input_line |> int_of_string) | |
(Unix.open_process_in "ulimit -s" |> input_line); | |
Printf.printf "Initial memory:\n"; | |
let ic = Unix.open_process_in "free -h" in | |
(try while true do Printf.printf " %s\n" (input_line ic) done | |
with End_of_file -> let _ = Unix.close_process_in ic in ()); | |
test_massive_lists (); | |
test_extreme_nesting (); | |
test_complex_structures (); | |
test_memory_pressure (); | |
test_rapid_renders (); | |
Printf.printf "\nπ EXTREME STRESS TEST COMPLETED!\n"; | |
Printf.printf "If you made it here without crashes, your code is SOLID! πͺ\n"; | |
Lwt.return () | |
let () = | |
Printexc.record_backtrace true; | |
Lwt_main.run (main ()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment