My working unpac space for OCaml projects in development
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

v simple bench

+141
+134
bench/bench_zstd.ml
··· 1 + (** Benchmarks for pure OCaml zstd implementation. 2 + 3 + Run with: dune exec bench/bench_zstd.exe -- -quota 5 4 + 5 + To compare with C zstd, run: 6 + python3 -c "print('A' * 102400, end='')" > /tmp/rep100k.bin 7 + zstd -b1 -e9 -i5 /tmp/rep100k.bin 8 + *) 9 + 10 + open Core 11 + open Core_bench 12 + 13 + (** Generate test data with different characteristics *) 14 + module Test_data = struct 15 + (** Highly compressible: repeated pattern *) 16 + let repetitive size = 17 + String.init size ~f:(fun i -> Char.of_int_exn ((i mod 26) + 65)) 18 + 19 + (** Medium compressibility: English-like text with spaces *) 20 + let text_like size = 21 + let words = [| "the "; "quick "; "brown "; "fox "; "jumps "; "over "; 22 + "lazy "; "dog "; "and "; "runs "; "now " |] in 23 + let buf = Buffer.create size in 24 + let i = ref 0 in 25 + while Buffer.length buf < size do 26 + Buffer.add_string buf words.(!i mod Array.length words); 27 + incr i 28 + done; 29 + String.sub (Buffer.contents buf) ~pos:0 ~len:size 30 + 31 + (** Binary-like: sequential bytes with variation *) 32 + let binary_like size = 33 + String.init size ~f:(fun i -> Char.of_int_exn ((i / 4) mod 256)) 34 + end 35 + 36 + (** Benchmark decompression *) 37 + let decompression_benchmarks data_name data = 38 + let size = String.length data in 39 + (* Pre-compress at level 3 *) 40 + let compressed = Zstd.compress ~level:3 data in 41 + let ratio = Float.of_int (String.length compressed) /. Float.of_int size *. 100.0 in 42 + [ 43 + Bench.Test.create 44 + ~name:(sprintf "decompress/%s/%dKB(%.0f%%)" data_name (size / 1024) ratio) 45 + (fun () -> ignore (Zstd.decompress_exn compressed : string)); 46 + ] 47 + 48 + (** Benchmark roundtrip *) 49 + let roundtrip_benchmarks data_name data = 50 + let size = String.length data in 51 + [ 52 + Bench.Test.create 53 + ~name:(sprintf "roundtrip/%s/%dKB" data_name (size / 1024)) 54 + (fun () -> 55 + let compressed = Zstd.compress ~level:3 data in 56 + ignore (Zstd.decompress_exn compressed : string)); 57 + ] 58 + 59 + (** Print compression ratio summary *) 60 + let print_compression_ratios () = 61 + let sizes = [1024; 10240; 102400] in 62 + let data_types = [ 63 + ("repetitive", Test_data.repetitive); 64 + ("text", Test_data.text_like); 65 + ("binary", Test_data.binary_like); 66 + ] in 67 + 68 + printf "\n=== Compression Ratios (size as %% of original) ===\n"; 69 + printf "%-12s %8s %8s %8s %8s %8s\n" 70 + "Data Type" "Size" "L1" "L3" "L6" "L9"; 71 + printf "%s\n" (String.make 56 '-'); 72 + 73 + List.iter sizes ~f:(fun size -> 74 + List.iter data_types ~f:(fun (name, gen) -> 75 + let data = gen size in 76 + let ratios = List.map [1; 3; 6; 9] ~f:(fun level -> 77 + let compressed = Zstd.compress ~level data in 78 + Float.of_int (String.length compressed) /. Float.of_int size *. 100.0 79 + ) in 80 + printf "%-12s %7dB " name size; 81 + List.iter ratios ~f:(fun r -> printf "%7.1f%% " r); 82 + printf "\n" 83 + ) 84 + ); 85 + printf "\n" 86 + 87 + let () = 88 + (* Print compression ratios first *) 89 + print_compression_ratios (); 90 + 91 + (* Generate test data at different sizes *) 92 + let sizes = [ 93 + (1, "1KB"); 94 + (10, "10KB"); 95 + (100, "100KB"); 96 + ] in 97 + 98 + let all_benchmarks = 99 + List.concat_map sizes ~f:(fun (kb, _) -> 100 + let size = kb * 1024 in 101 + 102 + (* Different data types *) 103 + let repetitive = Test_data.repetitive size in 104 + let text_like = Test_data.text_like size in 105 + let binary_like = Test_data.binary_like size in 106 + 107 + List.concat [ 108 + (* Compression benchmarks - only level 3 for each type/size *) 109 + [Bench.Test.create 110 + ~name:(sprintf "compress/rep/%dKB" kb) 111 + (fun () -> ignore (Zstd.compress ~level:3 repetitive : string))]; 112 + [Bench.Test.create 113 + ~name:(sprintf "compress/text/%dKB" kb) 114 + (fun () -> ignore (Zstd.compress ~level:3 text_like : string))]; 115 + [Bench.Test.create 116 + ~name:(sprintf "compress/bin/%dKB" kb) 117 + (fun () -> ignore (Zstd.compress ~level:3 binary_like : string))]; 118 + 119 + (* Decompression benchmarks *) 120 + decompression_benchmarks "rep" repetitive; 121 + decompression_benchmarks "text" text_like; 122 + decompression_benchmarks "bin" binary_like; 123 + 124 + (* Roundtrip benchmarks *) 125 + roundtrip_benchmarks "rep" repetitive; 126 + roundtrip_benchmarks "text" text_like; 127 + roundtrip_benchmarks "bin" binary_like; 128 + ] 129 + ) 130 + in 131 + 132 + printf "Running %d benchmarks...\n" (List.length all_benchmarks); 133 + printf "Use -quota N to set seconds per benchmark (default 10).\n\n"; 134 + Command_unix.run (Bench.make_command all_benchmarks)
+7
bench/dune
··· 1 + (executable 2 + (name bench_zstd) 3 + (libraries zstd core core_bench core_unix) 4 + (preprocess (pps ppx_jane))) 5 + 6 + ; Benchmarks comparing pure OCaml zstd vs C zstd. 7 + ; C zstd is accessed via the 'zstd' CLI tool (must be installed).