1- use criterion:: { criterion_group, criterion_main, Criterion , BenchmarkId } ;
2- use std:: hint:: black_box;
1+ use criterion:: { criterion_group, criterion_main, BenchmarkId , Criterion } ;
32use purrcrypt:: crypto:: efficient_compression:: { SmartCompressor , StreamingCompressor } ;
3+ use std:: hint:: black_box;
44
55fn bench_compression ( c : & mut Criterion ) {
66 let mut group = c. benchmark_group ( "compression" ) ;
7-
7+
88 let test_data_sizes = [ 100 , 1000 , 10000 , 100000 , 1000000 ] ;
99 let compressor = SmartCompressor :: new ( ) ;
10-
10+
1111 for size in test_data_sizes {
1212 let data = vec ! [ 0x42u8 ; size] ;
13-
13+
1414 group. bench_with_input (
1515 BenchmarkId :: new ( "compress" , format ! ( "{}_bytes" , size) ) ,
1616 & data,
17- |b, data| {
18- b. iter ( || {
19- compressor. compress ( black_box ( data) ) . unwrap ( )
20- } )
21- } ,
17+ |b, data| b. iter ( || compressor. compress ( black_box ( data) ) . unwrap ( ) ) ,
2218 ) ;
2319 }
24-
20+
2521 group. finish ( ) ;
2622}
2723
2824fn bench_decompression ( c : & mut Criterion ) {
2925 let mut group = c. benchmark_group ( "decompression" ) ;
30-
26+
3127 let test_data_sizes = [ 100 , 1000 , 10000 , 100000 , 1000000 ] ;
3228 let compressor = SmartCompressor :: new ( ) ;
33-
29+
3430 for size in test_data_sizes {
3531 let data = vec ! [ 0x42u8 ; size] ;
36-
32+
3733 // Pre-compress the data
3834 let compressed = compressor. compress ( & data) . unwrap ( ) ;
39-
35+
4036 group. bench_with_input (
4137 BenchmarkId :: new ( "decompress" , format ! ( "{}_bytes" , size) ) ,
4238 & compressed,
43- |b, compressed| {
44- b. iter ( || {
45- compressor. decompress ( black_box ( compressed) ) . unwrap ( )
46- } )
47- } ,
39+ |b, compressed| b. iter ( || compressor. decompress ( black_box ( compressed) ) . unwrap ( ) ) ,
4840 ) ;
4941 }
50-
42+
5143 group. finish ( ) ;
5244}
5345
5446fn bench_roundtrip ( c : & mut Criterion ) {
5547 let mut group = c. benchmark_group ( "compression_roundtrip" ) ;
56-
48+
5749 let test_data_sizes = [ 100 , 1000 , 10000 , 100000 , 1000000 ] ;
5850 let compressor = SmartCompressor :: new ( ) ;
59-
51+
6052 for size in test_data_sizes {
6153 let data = vec ! [ 0x42u8 ; size] ;
62-
54+
6355 group. bench_with_input (
6456 BenchmarkId :: new ( "roundtrip" , format ! ( "{}_bytes" , size) ) ,
6557 & data,
@@ -71,154 +63,169 @@ fn bench_roundtrip(c: &mut Criterion) {
7163 } ,
7264 ) ;
7365 }
74-
66+
7567 group. finish ( ) ;
7668}
7769
7870fn bench_compression_quality ( c : & mut Criterion ) {
7971 let mut group = c. benchmark_group ( "compression_quality" ) ;
80-
72+
8173 let test_cases = [
8274 ( "highly_compressible" , vec ! [ 0x41u8 ; 10000 ] ) ,
83- ( "moderately_compressible" , ( 0 ..10000 ) . map ( |i| if i % 2 == 0 { 0x41 } else { 0x42 } ) . collect ( ) ) ,
84- ( "low_compressibility" , ( 0 ..10000 ) . map ( |_| rand:: random :: < u8 > ( ) ) . collect ( ) ) ,
85- ( "text_data" , b"Hello World! This is a test message for compression quality testing. " . repeat ( 200 ) . into_iter ( ) . collect :: < Vec < u8 > > ( ) ) ,
86- ( "repeated_patterns" , vec ! [ 0x41u8 , 0x42u8 , 0x43u8 ] . repeat ( 3333 ) ) ,
75+ (
76+ "moderately_compressible" ,
77+ ( 0 ..10000 )
78+ . map ( |i| if i % 2 == 0 { 0x41 } else { 0x42 } )
79+ . collect ( ) ,
80+ ) ,
81+ (
82+ "low_compressibility" ,
83+ ( 0 ..10000 ) . map ( |_| rand:: random :: < u8 > ( ) ) . collect ( ) ,
84+ ) ,
85+ (
86+ "text_data" ,
87+ b"Hello World! This is a test message for compression quality testing. "
88+ . repeat ( 200 )
89+ . into_iter ( )
90+ . collect :: < Vec < u8 > > ( ) ,
91+ ) ,
92+ (
93+ "repeated_patterns" ,
94+ [ 0x41u8 , 0x42u8 , 0x43u8 ] . repeat ( 3333 ) ,
95+ ) ,
8796 ] ;
88-
97+
8998 let compressor = SmartCompressor :: new ( ) ;
90-
99+
91100 for ( name, data) in test_cases {
92- group. bench_with_input (
93- BenchmarkId :: new ( "compress" , name) ,
94- & data,
95- |b, data| {
96- b. iter ( || {
97- let compressed = compressor. compress ( black_box ( data) ) . unwrap ( ) ;
98- let ratio = compressed. len ( ) as f64 / data. len ( ) as f64 ;
99- black_box ( ratio)
100- } )
101- } ,
102- ) ;
101+ group. bench_with_input ( BenchmarkId :: new ( "compress" , name) , & data, |b, data| {
102+ b. iter ( || {
103+ let compressed = compressor. compress ( black_box ( data) ) . unwrap ( ) ;
104+ let ratio = compressed. len ( ) as f64 / data. len ( ) as f64 ;
105+ black_box ( ratio)
106+ } )
107+ } ) ;
103108 }
104-
109+
105110 group. finish ( ) ;
106111}
107112
108113fn bench_streaming_compression ( c : & mut Criterion ) {
109114 let mut group = c. benchmark_group ( "streaming_compression" ) ;
110-
115+
111116 let test_data_sizes = [ 100 , 1000 , 10000 , 100000 , 1000000 ] ;
112117 let compressor = StreamingCompressor :: new ( ) ;
113-
118+
114119 for size in test_data_sizes {
115120 let data = vec ! [ 0x42u8 ; size] ;
116-
121+
117122 group. bench_with_input (
118123 BenchmarkId :: new ( "compress_stream" , format ! ( "{}_bytes" , size) ) ,
119124 & data,
120125 |b, data| {
121126 b. iter ( || {
122127 let mut output = Vec :: new ( ) ;
123- compressor. compress_stream (
124- std:: io:: Cursor :: new ( black_box ( data) ) ,
125- & mut output
126- ) . unwrap ( ) ;
128+ compressor
129+ . compress_stream ( std:: io:: Cursor :: new ( black_box ( data) ) , & mut output)
130+ . unwrap ( ) ;
127131 black_box ( output)
128132 } )
129133 } ,
130134 ) ;
131135 }
132-
136+
133137 group. finish ( ) ;
134138}
135139
136140fn bench_streaming_decompression ( c : & mut Criterion ) {
137141 let mut group = c. benchmark_group ( "streaming_decompression" ) ;
138-
142+
139143 let test_data_sizes = [ 100 , 1000 , 10000 , 100000 , 1000000 ] ;
140144 let compressor = StreamingCompressor :: new ( ) ;
141-
145+
142146 for size in test_data_sizes {
143147 let data = vec ! [ 0x42u8 ; size] ;
144-
148+
145149 // Pre-compress the data
146150 let mut compressed = Vec :: new ( ) ;
147- compressor. compress_stream (
148- std:: io:: Cursor :: new ( & data) ,
149- & mut compressed
150- ) . unwrap ( ) ;
151-
151+ compressor
152+ . compress_stream ( std:: io:: Cursor :: new ( & data) , & mut compressed)
153+ . unwrap ( ) ;
154+
152155 group. bench_with_input (
153156 BenchmarkId :: new ( "decompress_stream" , format ! ( "{}_bytes" , size) ) ,
154157 & compressed,
155158 |b, compressed| {
156159 b. iter ( || {
157160 let mut output = Vec :: new ( ) ;
158- compressor. decompress_stream (
159- std:: io:: Cursor :: new ( black_box ( compressed) ) ,
160- & mut output
161- ) . unwrap ( ) ;
161+ compressor
162+ . decompress_stream ( std:: io:: Cursor :: new ( black_box ( compressed) ) , & mut output)
163+ . unwrap ( ) ;
162164 black_box ( output)
163165 } )
164166 } ,
165167 ) ;
166168 }
167-
169+
168170 group. finish ( ) ;
169171}
170172
171173fn bench_entropy_calculation ( c : & mut Criterion ) {
172174 let mut group = c. benchmark_group ( "entropy_calculation" ) ;
173-
175+
174176 let test_cases = [
175177 ( "low_entropy" , vec ! [ 0x41u8 ; 10000 ] ) ,
176- ( "medium_entropy" , ( 0 ..10000 ) . map ( |i| if i % 2 == 0 { 0x41 } else { 0x42 } ) . collect ( ) ) ,
177- ( "high_entropy" , ( 0 ..10000 ) . map ( |_| rand:: random :: < u8 > ( ) ) . collect ( ) ) ,
178- ( "mixed_entropy" , vec ! [ 0x41u8 , 0x42u8 , 0x43u8 , 0x44u8 ] . repeat ( 2500 ) ) ,
178+ (
179+ "medium_entropy" ,
180+ ( 0 ..10000 )
181+ . map ( |i| if i % 2 == 0 { 0x41 } else { 0x42 } )
182+ . collect ( ) ,
183+ ) ,
184+ (
185+ "high_entropy" ,
186+ ( 0 ..10000 ) . map ( |_| rand:: random :: < u8 > ( ) ) . collect ( ) ,
187+ ) ,
188+ (
189+ "mixed_entropy" ,
190+ [ 0x41u8 , 0x42u8 , 0x43u8 , 0x44u8 ] . repeat ( 2500 ) ,
191+ ) ,
179192 ] ;
180-
193+
181194 let compressor = SmartCompressor :: new ( ) ;
182-
195+
183196 for ( name, data) in test_cases {
184- group. bench_with_input (
185- BenchmarkId :: new ( "entropy" , name) ,
186- & data,
187- |b, data| {
188- b. iter ( || {
189- compressor. calculate_entropy ( black_box ( data) )
190- } )
191- } ,
192- ) ;
197+ group. bench_with_input ( BenchmarkId :: new ( "entropy" , name) , & data, |b, data| {
198+ b. iter ( || compressor. calculate_entropy ( black_box ( data) ) )
199+ } ) ;
193200 }
194-
201+
195202 group. finish ( ) ;
196203}
197204
198205fn bench_compression_detection ( c : & mut Criterion ) {
199206 let mut group = c. benchmark_group ( "compression_detection" ) ;
200-
207+
201208 let test_cases = [
202209 ( "uncompressed" , vec ! [ 0x42u8 ; 1000 ] ) ,
203- ( "zlib_signature" , vec ! [ 0x78 , 0x9c , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ] ) ,
204- ( "zstd_signature" , vec ! [ 0x28 , 0xb5 , 0x2f , 0xfd , 0x01 , 0x02 , 0x03 , 0x04 ] ) ,
205- ( "mixed_data" , vec ! [ 0x41u8 , 0x42u8 , 0x43u8 ] . repeat ( 333 ) ) ,
210+ (
211+ "zlib_signature" ,
212+ vec ! [ 0x78 , 0x9c , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ] ,
213+ ) ,
214+ (
215+ "zstd_signature" ,
216+ vec ! [ 0x28 , 0xb5 , 0x2f , 0xfd , 0x01 , 0x02 , 0x03 , 0x04 ] ,
217+ ) ,
218+ ( "mixed_data" , [ 0x41u8 , 0x42u8 , 0x43u8 ] . repeat ( 333 ) ) ,
206219 ] ;
207-
220+
208221 let compressor = SmartCompressor :: new ( ) ;
209-
222+
210223 for ( name, data) in test_cases {
211- group. bench_with_input (
212- BenchmarkId :: new ( "detect" , name) ,
213- & data,
214- |b, data| {
215- b. iter ( || {
216- compressor. is_likely_compressed ( black_box ( data) )
217- } )
218- } ,
219- ) ;
224+ group. bench_with_input ( BenchmarkId :: new ( "detect" , name) , & data, |b, data| {
225+ b. iter ( || compressor. is_likely_compressed ( black_box ( data) ) )
226+ } ) ;
220227 }
221-
228+
222229 group. finish ( ) ;
223230}
224231
0 commit comments