|
| 1 | +//! Benchmarks for WRAITH obfuscation layer. |
| 2 | +
|
| 3 | +use criterion::{Criterion, Throughput, black_box, criterion_group, criterion_main}; |
| 4 | +use wraith_obfuscation::*; |
| 5 | + |
| 6 | +fn bench_padding(c: &mut Criterion) { |
| 7 | + let mut group = c.benchmark_group("padding"); |
| 8 | + |
| 9 | + for size in [128, 512, 1024, 4096] { |
| 10 | + let data = vec![0u8; size]; |
| 11 | + group.throughput(Throughput::Bytes(size as u64)); |
| 12 | + |
| 13 | + group.bench_with_input(format!("size_classes_{}", size), &data, |b, data| { |
| 14 | + let mut engine = PaddingEngine::new(PaddingMode::SizeClasses); |
| 15 | + b.iter(|| { |
| 16 | + let mut buf = data.clone(); |
| 17 | + let target = engine.padded_size(data.len()); |
| 18 | + engine.pad(&mut buf, target); |
| 19 | + black_box(buf); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + group.bench_with_input(format!("statistical_{}", size), &data, |b, data| { |
| 24 | + let mut engine = PaddingEngine::new(PaddingMode::Statistical); |
| 25 | + b.iter(|| { |
| 26 | + let mut buf = data.clone(); |
| 27 | + let target = engine.padded_size(data.len()); |
| 28 | + engine.pad(&mut buf, target); |
| 29 | + black_box(buf); |
| 30 | + }); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + group.finish(); |
| 35 | +} |
| 36 | + |
| 37 | +fn bench_tls_wrap(c: &mut Criterion) { |
| 38 | + let mut group = c.benchmark_group("tls_mimicry"); |
| 39 | + |
| 40 | + for size in [128, 512, 1024, 4096] { |
| 41 | + let payload = vec![0u8; size]; |
| 42 | + group.throughput(Throughput::Bytes(size as u64)); |
| 43 | + |
| 44 | + group.bench_with_input(format!("wrap_{}", size), &payload, |b, payload| { |
| 45 | + let mut wrapper = TlsRecordWrapper::new(); |
| 46 | + b.iter(|| { |
| 47 | + let wrapped = wrapper.wrap(black_box(payload)); |
| 48 | + black_box(wrapped); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + group.bench_with_input(format!("unwrap_{}", size), &payload, |b, payload| { |
| 53 | + let mut wrapper = TlsRecordWrapper::new(); |
| 54 | + let record = wrapper.wrap(payload); |
| 55 | + b.iter(|| { |
| 56 | + let unwrapped = wrapper.unwrap(black_box(&record)).unwrap(); |
| 57 | + black_box(unwrapped); |
| 58 | + }); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + group.finish(); |
| 63 | +} |
| 64 | + |
| 65 | +fn bench_websocket_wrap(c: &mut Criterion) { |
| 66 | + let mut group = c.benchmark_group("websocket_mimicry"); |
| 67 | + |
| 68 | + for size in [128, 512, 1024, 4096] { |
| 69 | + let payload = vec![0u8; size]; |
| 70 | + group.throughput(Throughput::Bytes(size as u64)); |
| 71 | + |
| 72 | + group.bench_with_input(format!("wrap_server_{}", size), &payload, |b, payload| { |
| 73 | + let wrapper = WebSocketFrameWrapper::new(false); |
| 74 | + b.iter(|| { |
| 75 | + let wrapped = wrapper.wrap(black_box(payload)); |
| 76 | + black_box(wrapped); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + group.bench_with_input(format!("wrap_client_{}", size), &payload, |b, payload| { |
| 81 | + let wrapper = WebSocketFrameWrapper::new(true); |
| 82 | + b.iter(|| { |
| 83 | + let wrapped = wrapper.wrap(black_box(payload)); |
| 84 | + black_box(wrapped); |
| 85 | + }); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + group.finish(); |
| 90 | +} |
| 91 | + |
| 92 | +fn bench_doh_tunnel(c: &mut Criterion) { |
| 93 | + let mut group = c.benchmark_group("doh_tunnel"); |
| 94 | + |
| 95 | + for size in [128, 512, 1024, 4096] { |
| 96 | + let payload = vec![0u8; size]; |
| 97 | + group.throughput(Throughput::Bytes(size as u64)); |
| 98 | + |
| 99 | + group.bench_with_input(format!("create_query_{}", size), &payload, |b, payload| { |
| 100 | + let tunnel = DohTunnel::new("https://dns.example.com/dns-query".to_string()); |
| 101 | + b.iter(|| { |
| 102 | + let query = tunnel.create_dns_query(black_box("test.com"), black_box(payload)); |
| 103 | + black_box(query); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + group.bench_with_input( |
| 108 | + format!("parse_response_{}", size), |
| 109 | + &payload, |
| 110 | + |b, payload| { |
| 111 | + let tunnel = DohTunnel::new("https://dns.example.com/dns-query".to_string()); |
| 112 | + let query = tunnel.create_dns_query("test.com", payload); |
| 113 | + b.iter(|| { |
| 114 | + let parsed = tunnel.parse_dns_response(black_box(&query)).unwrap(); |
| 115 | + black_box(parsed); |
| 116 | + }); |
| 117 | + }, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + group.finish(); |
| 122 | +} |
| 123 | + |
| 124 | +fn bench_timing_obfuscator(c: &mut Criterion) { |
| 125 | + let mut group = c.benchmark_group("timing"); |
| 126 | + |
| 127 | + use std::time::Duration; |
| 128 | + |
| 129 | + group.bench_function("none", |b| { |
| 130 | + let mut obfuscator = TimingObfuscator::new(TimingMode::None); |
| 131 | + b.iter(|| { |
| 132 | + let delay = obfuscator.next_delay(); |
| 133 | + black_box(delay); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + group.bench_function("fixed", |b| { |
| 138 | + let mut obfuscator = TimingObfuscator::new(TimingMode::Fixed(Duration::from_millis(10))); |
| 139 | + b.iter(|| { |
| 140 | + let delay = obfuscator.next_delay(); |
| 141 | + black_box(delay); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + group.bench_function("uniform", |b| { |
| 146 | + let mut obfuscator = TimingObfuscator::new(TimingMode::Uniform { |
| 147 | + min: Duration::from_millis(5), |
| 148 | + max: Duration::from_millis(15), |
| 149 | + }); |
| 150 | + b.iter(|| { |
| 151 | + let delay = obfuscator.next_delay(); |
| 152 | + black_box(delay); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + group.bench_function("normal", |b| { |
| 157 | + let mut obfuscator = TimingObfuscator::new(TimingMode::Normal { |
| 158 | + mean: Duration::from_millis(10), |
| 159 | + stddev: Duration::from_millis(2), |
| 160 | + }); |
| 161 | + b.iter(|| { |
| 162 | + let delay = obfuscator.next_delay(); |
| 163 | + black_box(delay); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + group.bench_function("exponential", |b| { |
| 168 | + let mut obfuscator = TimingObfuscator::new(TimingMode::Exponential { |
| 169 | + mean: Duration::from_millis(10), |
| 170 | + }); |
| 171 | + b.iter(|| { |
| 172 | + let delay = obfuscator.next_delay(); |
| 173 | + black_box(delay); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + group.finish(); |
| 178 | +} |
| 179 | + |
| 180 | +fn bench_adaptive_profile(c: &mut Criterion) { |
| 181 | + c.bench_function("profile_from_threat_level", |b| { |
| 182 | + b.iter(|| { |
| 183 | + let profile = ObfuscationProfile::from_threat_level(black_box(ThreatLevel::High)); |
| 184 | + black_box(profile); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + c.bench_function("profile_estimated_overhead", |b| { |
| 189 | + let profile = ObfuscationProfile::from_threat_level(ThreatLevel::High); |
| 190 | + b.iter(|| { |
| 191 | + let overhead = profile.estimated_overhead(); |
| 192 | + black_box(overhead); |
| 193 | + }); |
| 194 | + }); |
| 195 | +} |
| 196 | + |
| 197 | +criterion_group!( |
| 198 | + benches, |
| 199 | + bench_padding, |
| 200 | + bench_tls_wrap, |
| 201 | + bench_websocket_wrap, |
| 202 | + bench_doh_tunnel, |
| 203 | + bench_timing_obfuscator, |
| 204 | + bench_adaptive_profile |
| 205 | +); |
| 206 | +criterion_main!(benches); |
0 commit comments