|
| 1 | +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; |
| 2 | +use rustnet_monitor::network::types::*; |
| 3 | +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; |
| 4 | + |
| 5 | +/// Create a Connection with a RateTracker filled to `n_samples` entries. |
| 6 | +fn make_connection_with_samples(n_samples: usize) -> Connection { |
| 7 | + let local = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)), 54321); |
| 8 | + let remote = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(93, 184, 216, 34)), 443); |
| 9 | + let mut conn = Connection::new( |
| 10 | + Protocol::Tcp, |
| 11 | + local, |
| 12 | + remote, |
| 13 | + ProtocolState::Tcp(TcpState::Established), |
| 14 | + ); |
| 15 | + |
| 16 | + for i in 0..n_samples { |
| 17 | + conn.bytes_sent += 100; |
| 18 | + conn.bytes_received += 200; |
| 19 | + conn.rate_tracker |
| 20 | + .update(conn.bytes_sent, conn.bytes_received); |
| 21 | + // Sprinkle in some pruning to keep the tracker realistic |
| 22 | + if i % 500 == 0 { |
| 23 | + conn.rate_tracker.prune(); |
| 24 | + } |
| 25 | + } |
| 26 | + conn |
| 27 | +} |
| 28 | + |
| 29 | +/// Benchmark the per-packet `update()` call on RateTracker. |
| 30 | +/// This is the hot path — called for every packet received. |
| 31 | +/// The Arc<VecDeque> change adds an `Arc::make_mut` atomic check here. |
| 32 | +fn bench_rate_update(c: &mut Criterion) { |
| 33 | + let mut group = c.benchmark_group("rate_tracker_update"); |
| 34 | + |
| 35 | + for n_samples in [0, 100, 1000, 5000] { |
| 36 | + // Unique owner: simulates the normal packet-processing path where |
| 37 | + // no snapshot clone is holding a shared reference. |
| 38 | + group.bench_with_input( |
| 39 | + BenchmarkId::new("unique_owner", n_samples), |
| 40 | + &n_samples, |
| 41 | + |b, &n| { |
| 42 | + let mut conn = make_connection_with_samples(n); |
| 43 | + let mut bytes_sent = conn.bytes_sent; |
| 44 | + let mut bytes_recv = conn.bytes_received; |
| 45 | + b.iter(|| { |
| 46 | + bytes_sent += 100; |
| 47 | + bytes_recv += 200; |
| 48 | + conn.rate_tracker.update(bytes_sent, bytes_recv); |
| 49 | + }); |
| 50 | + }, |
| 51 | + ); |
| 52 | + |
| 53 | + // Shared owner: simulates the case right after a snapshot clone, |
| 54 | + // where two Arcs point to the same VecDeque. The first `update()` |
| 55 | + // after a clone triggers a full VecDeque copy via Arc::make_mut. |
| 56 | + group.bench_with_input( |
| 57 | + BenchmarkId::new("after_snapshot_clone", n_samples), |
| 58 | + &n_samples, |
| 59 | + |b, &n| { |
| 60 | + b.iter_batched( |
| 61 | + || { |
| 62 | + let conn = make_connection_with_samples(n); |
| 63 | + let _snapshot = conn.clone(); // create shared Arc |
| 64 | + conn |
| 65 | + }, |
| 66 | + |mut conn| { |
| 67 | + conn.bytes_sent += 100; |
| 68 | + conn.bytes_received += 200; |
| 69 | + conn.rate_tracker |
| 70 | + .update(conn.bytes_sent, conn.bytes_received); |
| 71 | + }, |
| 72 | + criterion::BatchSize::SmallInput, |
| 73 | + ); |
| 74 | + }, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + group.finish(); |
| 79 | +} |
| 80 | + |
| 81 | +/// Benchmark `refresh_rates()` (prune + rate calculation + smoothing). |
| 82 | +/// Called once per second per connection from the refresh loop. |
| 83 | +fn bench_refresh_rates(c: &mut Criterion) { |
| 84 | + let mut group = c.benchmark_group("refresh_rates"); |
| 85 | + |
| 86 | + for n_samples in [0, 100, 1000, 5000] { |
| 87 | + group.bench_with_input( |
| 88 | + BenchmarkId::new("unique_owner", n_samples), |
| 89 | + &n_samples, |
| 90 | + |b, &n| { |
| 91 | + let mut conn = make_connection_with_samples(n); |
| 92 | + b.iter(|| { |
| 93 | + conn.refresh_rates(); |
| 94 | + }); |
| 95 | + }, |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + group.finish(); |
| 100 | +} |
| 101 | + |
| 102 | +/// Benchmark Connection::clone() to measure the impact of Arc<VecDeque> |
| 103 | +/// vs a plain VecDeque. With Arc, clone is O(1) for the samples field |
| 104 | +/// (just a refcount bump). Without Arc, it's O(n_samples). |
| 105 | +fn bench_connection_clone(c: &mut Criterion) { |
| 106 | + let mut group = c.benchmark_group("connection_clone"); |
| 107 | + |
| 108 | + for n_samples in [0, 100, 1000, 5000, 10000] { |
| 109 | + let conn = make_connection_with_samples(n_samples); |
| 110 | + group.bench_with_input(BenchmarkId::new("clone", n_samples), &conn, |b, conn| { |
| 111 | + b.iter(|| conn.clone()); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + group.finish(); |
| 116 | +} |
| 117 | + |
| 118 | +/// Benchmark the snapshot-then-mutate cycle that happens in practice: |
| 119 | +/// 1. Clone N connections for a UI snapshot |
| 120 | +/// 2. Then update each original connection with a new packet |
| 121 | +/// |
| 122 | +/// This measures the real-world cost: cheap clone (Arc refcount) followed |
| 123 | +/// by a CoW deep-copy on first mutation. |
| 124 | +fn bench_snapshot_then_update(c: &mut Criterion) { |
| 125 | + let mut group = c.benchmark_group("snapshot_then_update"); |
| 126 | + |
| 127 | + for n_conns in [100, 1000, 5000] { |
| 128 | + let connections: Vec<Connection> = (0..n_conns) |
| 129 | + .map(|_| make_connection_with_samples(100)) |
| 130 | + .collect(); |
| 131 | + |
| 132 | + group.bench_with_input( |
| 133 | + BenchmarkId::new("clone_all_then_update_all", n_conns), |
| 134 | + &connections, |
| 135 | + |b, connections| { |
| 136 | + b.iter_batched( |
| 137 | + || connections.clone(), |
| 138 | + |mut conns| { |
| 139 | + // Step 1: snapshot clone (simulates UI snapshot) |
| 140 | + let _snapshot: Vec<Connection> = conns.iter().map(|c| c.clone()).collect(); |
| 141 | + // Step 2: mutate originals (simulates incoming packets) |
| 142 | + for conn in &mut conns { |
| 143 | + conn.bytes_sent += 100; |
| 144 | + conn.bytes_received += 200; |
| 145 | + conn.rate_tracker |
| 146 | + .update(conn.bytes_sent, conn.bytes_received); |
| 147 | + } |
| 148 | + }, |
| 149 | + criterion::BatchSize::LargeInput, |
| 150 | + ); |
| 151 | + }, |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + group.finish(); |
| 156 | +} |
| 157 | + |
| 158 | +criterion_group!( |
| 159 | + benches, |
| 160 | + bench_rate_update, |
| 161 | + bench_refresh_rates, |
| 162 | + bench_connection_clone, |
| 163 | + bench_snapshot_then_update, |
| 164 | +); |
| 165 | +criterion_main!(benches); |
0 commit comments