|
| 1 | +(* Simple Performance Benchmark, not related with the CSS runtime, just used for manually testing some internal functions *) |
| 2 | + |
| 3 | +let time_function name f = |
| 4 | + let start_time = Sys.time () in |
| 5 | + let result = f () in |
| 6 | + let end_time = Sys.time () in |
| 7 | + Printf.printf "%s: %.4f seconds\n" name (end_time -. start_time); |
| 8 | + result |
| 9 | + |
| 10 | +let memory_stats name f = |
| 11 | + Gc.compact (); |
| 12 | + let stat_before = Gc.stat () in |
| 13 | + let result = f () in |
| 14 | + Gc.compact (); |
| 15 | + let stat_after = Gc.stat () in |
| 16 | + |
| 17 | + let allocations = |
| 18 | + stat_after.minor_words |
| 19 | + +. stat_after.major_words |
| 20 | + -. stat_before.minor_words |
| 21 | + -. stat_before.major_words |
| 22 | + in |
| 23 | + |
| 24 | + Printf.printf "%s: %.0f words allocated (%.2f MB)\n" name allocations |
| 25 | + (allocations *. 8.0 /. 1024.0 /. 1024.0); |
| 26 | + result |
| 27 | + |
| 28 | +let generate_hashes n = |
| 29 | + let rec loop acc i = |
| 30 | + if i <= 0 then acc |
| 31 | + else loop (Printf.sprintf "css-hash-%d-abcdef" i :: acc) (i - 1) |
| 32 | + in |
| 33 | + loop [] n |
| 34 | + |
| 35 | +let test_camel_strings = |
| 36 | + [| |
| 37 | + "backgroundColor"; |
| 38 | + "borderTopLeftRadius"; |
| 39 | + "marginBottomRight"; |
| 40 | + "paddingTopBottomLeftRight"; |
| 41 | + "textTransformUppercase"; |
| 42 | + "fontWeightBoldItalic"; |
| 43 | + "boxShadowInsetOutset"; |
| 44 | + "transformOriginCenter"; |
| 45 | + "animationDurationTimingFunction"; |
| 46 | + "borderImageOutsetSliceWidthSource"; |
| 47 | + |] |
| 48 | + |
| 49 | +(* OLD IMPLEMENTATIONS *) |
| 50 | +let explode s = |
| 51 | + let rec explode_rec acc i = |
| 52 | + if i < 0 then acc else explode_rec (s.[i] :: acc) (i - 1) |
| 53 | + in |
| 54 | + explode_rec [] (String.length s - 1) |
| 55 | + |
| 56 | +let old_camelCaseToKebabCase str = |
| 57 | + let insert_dash acc letter = |
| 58 | + match letter with |
| 59 | + | 'A' .. 'Z' as letter -> |
| 60 | + ("-" ^ String.make 1 (Char.lowercase_ascii letter)) :: acc |
| 61 | + | _ -> String.make 1 letter :: acc |
| 62 | + in |
| 63 | + String.concat "" (List.rev (List.fold_left insert_dash [] (explode str))) |
| 64 | + |
| 65 | +let old_get_string_style_hashes hashes = |
| 66 | + List.fold_left |
| 67 | + (fun accumulator hash -> |
| 68 | + String.trim @@ Printf.sprintf "%s %s" accumulator hash) |
| 69 | + "" hashes |
| 70 | + |
| 71 | +(* NEW IMPLEMENTATIONS *) |
| 72 | +let new_camelCaseToKebabCase str = |
| 73 | + let len = String.length str in |
| 74 | + let buffer = Buffer.create (len + 10) in |
| 75 | + for i = 0 to len - 1 do |
| 76 | + let c = str.[i] in |
| 77 | + match c with |
| 78 | + | 'A' .. 'Z' -> |
| 79 | + if i > 0 then Buffer.add_char buffer '-'; |
| 80 | + Buffer.add_char buffer (Char.lowercase_ascii c) |
| 81 | + | _ -> Buffer.add_char buffer c |
| 82 | + done; |
| 83 | + Buffer.contents buffer |
| 84 | + |
| 85 | +let new_get_string_style_hashes hashes = |
| 86 | + let buffer = Buffer.create 1024 in |
| 87 | + let first = ref true in |
| 88 | + List.iter |
| 89 | + (fun hash -> |
| 90 | + if not !first then Buffer.add_char buffer ' '; |
| 91 | + Buffer.add_string buffer (String.trim hash); |
| 92 | + first := false) |
| 93 | + hashes; |
| 94 | + Buffer.contents buffer |
| 95 | + |
| 96 | +(* BENCHMARKS *) |
| 97 | +let benchmark_camelcase n = |
| 98 | + Printf.printf "\n=== CamelCase Conversion (%d iterations) ===\n" n; |
| 99 | + let test_strings = Array.to_list (Array.make n test_camel_strings.(0)) in |
| 100 | + |
| 101 | + let old_result = |
| 102 | + memory_stats "OLD camelCase" (fun () -> |
| 103 | + time_function "OLD time" (fun () -> |
| 104 | + List.map old_camelCaseToKebabCase test_strings)) |
| 105 | + in |
| 106 | + |
| 107 | + let new_result = |
| 108 | + memory_stats "NEW camelCase" (fun () -> |
| 109 | + time_function "NEW time" (fun () -> |
| 110 | + List.map new_camelCaseToKebabCase test_strings)) |
| 111 | + in |
| 112 | + |
| 113 | + Printf.printf "Results match: %b\n" |
| 114 | + (List.for_all2 String.equal old_result new_result) |
| 115 | + |
| 116 | +let benchmark_hash_concat n = |
| 117 | + Printf.printf "\n=== Hash Concatenation (%d hashes) ===\n" n; |
| 118 | + let test_hashes = generate_hashes n in |
| 119 | + |
| 120 | + let old_result = |
| 121 | + memory_stats "OLD hash concat" (fun () -> |
| 122 | + time_function "OLD time" (fun () -> |
| 123 | + old_get_string_style_hashes test_hashes)) |
| 124 | + in |
| 125 | + |
| 126 | + let new_result = |
| 127 | + memory_stats "NEW hash concat" (fun () -> |
| 128 | + time_function "NEW time" (fun () -> |
| 129 | + new_get_string_style_hashes test_hashes)) |
| 130 | + in |
| 131 | + |
| 132 | + Printf.printf "Results match: %b\n" (String.equal old_result new_result); |
| 133 | + Printf.printf "Result length: %d chars\n" (String.length old_result) |
| 134 | + |
| 135 | +let stress_test () = |
| 136 | + Printf.printf "\n=== STRESS TEST ===\n"; |
| 137 | + let large_hashes = generate_hashes 5000 in |
| 138 | + let large_camel_list = |
| 139 | + Array.to_list (Array.make 2000 "veryLongCamelCasePropertyName") |
| 140 | + in |
| 141 | + |
| 142 | + Printf.printf "Testing with 5000 hashes + 2000 camelCase conversions...\n"; |
| 143 | + |
| 144 | + let _ = |
| 145 | + memory_stats "OLD stress test" (fun () -> |
| 146 | + time_function "OLD total time" (fun () -> |
| 147 | + let hash_result = old_get_string_style_hashes large_hashes in |
| 148 | + let camel_results = |
| 149 | + List.map old_camelCaseToKebabCase large_camel_list |
| 150 | + in |
| 151 | + hash_result, camel_results)) |
| 152 | + in |
| 153 | + |
| 154 | + let _ = |
| 155 | + memory_stats "NEW stress test" (fun () -> |
| 156 | + time_function "NEW total time" (fun () -> |
| 157 | + let hash_result = new_get_string_style_hashes large_hashes in |
| 158 | + let camel_results = |
| 159 | + List.map new_camelCaseToKebabCase large_camel_list |
| 160 | + in |
| 161 | + hash_result, camel_results)) |
| 162 | + in |
| 163 | + |
| 164 | + Printf.printf "Stress test completed\n" |
| 165 | + |
| 166 | +let () = |
| 167 | + Printf.printf "=== CSS Performance Benchmark ===\n"; |
| 168 | + |
| 169 | + benchmark_camelcase 100; |
| 170 | + benchmark_camelcase 1000; |
| 171 | + |
| 172 | + benchmark_hash_concat 100; |
| 173 | + benchmark_hash_concat 1000; |
| 174 | + benchmark_hash_concat 5000; |
| 175 | + |
| 176 | + stress_test () |
0 commit comments