|
| 1 | +use std::hash::{Hash, Hasher}; |
| 2 | +use std::hint::black_box; |
| 3 | +use std::thread; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +#[cfg(feature = "hotpath")] |
| 7 | +use hotpath::{measure_block, Format, GuardBuilder}; |
| 8 | + |
| 9 | +#[cfg(not(feature = "hotpath"))] |
| 10 | +macro_rules! measure_block { |
| 11 | + ($label:expr, $expr:expr) => {{ |
| 12 | + let _ = $label; |
| 13 | + $expr |
| 14 | + }}; |
| 15 | +} |
| 16 | + |
| 17 | +#[cfg(feature = "hotpath-alloc-count-total")] |
| 18 | +use std::collections::HashMap; |
| 19 | + |
| 20 | +fn main() { |
| 21 | + run_benchmark(); |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(feature = "hotpath")] |
| 25 | +fn run_benchmark() { |
| 26 | + let _guard = GuardBuilder::new("shadowmap::benchmark") |
| 27 | + .percentiles(&[75, 90, 95, 99]) |
| 28 | + .format(Format::Json) |
| 29 | + .limit(32) |
| 30 | + .build(); |
| 31 | + |
| 32 | + orchestrate_shadowmap_pipeline(); |
| 33 | +} |
| 34 | + |
| 35 | +#[cfg(not(feature = "hotpath"))] |
| 36 | +fn run_benchmark() { |
| 37 | + eprintln!( |
| 38 | + "Hotpath profiling is disabled; rerun with `--features hotpath,hotpath-ci` to record metrics." |
| 39 | + ); |
| 40 | + orchestrate_shadowmap_pipeline(); |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg_attr(feature = "hotpath", hotpath::measure)] |
| 44 | +fn orchestrate_shadowmap_pipeline() { |
| 45 | + let scenarios = [ |
| 46 | + ("shadowmap.io", 18usize), |
| 47 | + ("api.shadowmap.io", 22), |
| 48 | + ("assets.shadowmap.io", 16), |
| 49 | + ("billing.shadowmap.io", 20), |
| 50 | + ]; |
| 51 | + |
| 52 | + for (seed, batch) in scenarios { |
| 53 | + let inventory = enumerate_domains(seed, batch); |
| 54 | + correlate_risk(seed, inventory); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg_attr(feature = "hotpath", hotpath::measure)] |
| 59 | +fn enumerate_domains(seed: &str, batch: usize) -> usize { |
| 60 | + let mut discovered = 0usize; |
| 61 | + |
| 62 | + for i in 0..batch { |
| 63 | + let candidate = format!("{seed}-{i:02}"); |
| 64 | + |
| 65 | + measure_block!("dns_resolution", { |
| 66 | + discovered ^= hash_candidate(&candidate); |
| 67 | + if i % 4 == 0 { |
| 68 | + thread::sleep(Duration::from_micros(110)); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + if i % 3 == 0 { |
| 73 | + let artifacts = synthesize_fingerprint(i, candidate.len()); |
| 74 | + discovered ^= artifacts; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + discovered |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg_attr(feature = "hotpath", hotpath::measure)] |
| 82 | +fn synthesize_fingerprint(iteration: usize, width: usize) -> usize { |
| 83 | + let size = 128 + (iteration % 8) * 16 + width; |
| 84 | + let mut buffer = Vec::with_capacity(size); |
| 85 | + |
| 86 | + for offset in 0..size { |
| 87 | + buffer.push(((offset * 37 + iteration) % 255) as u8); |
| 88 | + } |
| 89 | + |
| 90 | + measure_block!("normalize_headers", { |
| 91 | + buffer.sort_unstable(); |
| 92 | + let chunk = (buffer.len() / 8).max(1); |
| 93 | + let mut rolling = 0u32; |
| 94 | + |
| 95 | + for piece in buffer.chunks(chunk) { |
| 96 | + rolling ^= piece.iter().fold(0u32, |acc, byte| acc ^ (*byte as u32)); |
| 97 | + } |
| 98 | + |
| 99 | + black_box(rolling); |
| 100 | + thread::sleep(Duration::from_micros(80)); |
| 101 | + }); |
| 102 | + |
| 103 | + buffer.len() |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg_attr(feature = "hotpath", hotpath::measure)] |
| 107 | +fn correlate_risk(seed: &str, coverage: usize) { |
| 108 | + let iterations = (coverage % 6) + 5; |
| 109 | + let mut total = 0usize; |
| 110 | + |
| 111 | + for offset in 0..iterations { |
| 112 | + total += evaluate_certificate(seed, offset, coverage); |
| 113 | + } |
| 114 | + |
| 115 | + black_box(total); |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg_attr(feature = "hotpath", hotpath::measure)] |
| 119 | +fn evaluate_certificate(seed: &str, offset: usize, coverage: usize) -> usize { |
| 120 | + let mut bytes = seed.as_bytes().to_vec(); |
| 121 | + bytes.resize(bytes.len() + (offset % 5) + 4, b'*'); |
| 122 | + |
| 123 | + measure_block!("tls_parse", { |
| 124 | + let len = bytes.len().max(1); |
| 125 | + bytes.rotate_left(offset % len); |
| 126 | + if coverage % 2 == 0 { |
| 127 | + thread::sleep(Duration::from_micros(60)); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + #[cfg(feature = "hotpath-alloc-count-total")] |
| 132 | + { |
| 133 | + let mut map = HashMap::with_capacity(offset + 4); |
| 134 | + for (idx, byte) in bytes.iter().enumerate() { |
| 135 | + map.insert(idx, *byte); |
| 136 | + } |
| 137 | + black_box(map.len()); |
| 138 | + } |
| 139 | + |
| 140 | + bytes.iter().filter(|ch| **ch == b'*').count() + offset |
| 141 | +} |
| 142 | + |
| 143 | +fn hash_candidate(input: &str) -> usize { |
| 144 | + let mut hasher = std::collections::hash_map::DefaultHasher::new(); |
| 145 | + input.hash(&mut hasher); |
| 146 | + hasher.finish() as usize |
| 147 | +} |
0 commit comments