|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +// Rust guideline compliant 2026-03-30 |
| 3 | +//! NFR-15 latency benchmark for `anvil_ssh::ssh_config::resolve()`. |
| 4 | +//! |
| 5 | +//! Asserts that resolving a typical user `ssh_config(5)` (≤100 directives, |
| 6 | +//! no `Include`s) completes in ≤ 5 ms cold per [Gitway PRD §10 NFR-15]. |
| 7 | +//! Hard-fails via `panic!` if the median exceeds the budget so the |
| 8 | +//! regression shows up in CI as a build failure rather than a noisy |
| 9 | +//! Criterion print. |
| 10 | +//! |
| 11 | +//! # Running |
| 12 | +//! |
| 13 | +//! ```sh |
| 14 | +//! cargo bench --bench ssh_config_latency |
| 15 | +//! ``` |
| 16 | +//! |
| 17 | +//! No environment variables required — the benchmark is fully hermetic |
| 18 | +//! (writes its fixture to a [`tempfile::TempDir`] and tears it down on |
| 19 | +//! drop). |
| 20 | +//! |
| 21 | +//! # Why a separate bench harness |
| 22 | +//! |
| 23 | +//! `cargo test` measures correctness; this measures *speed*. We want |
| 24 | +//! the latency budget enforced on every release, but we do not want it |
| 25 | +//! gated on `GITWAY_INTEGRATION_TESTS=1` the way the throughput bench |
| 26 | +//! is — the resolver path involves zero network I/O. |
| 27 | +
|
| 28 | +use std::fs; |
| 29 | +use std::path::PathBuf; |
| 30 | +use std::time::Duration; |
| 31 | + |
| 32 | +use anvil_ssh::ssh_config::{resolve, SshConfigPaths}; |
| 33 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 34 | +use tempfile::TempDir; |
| 35 | + |
| 36 | +/// NFR-15 budget: ≤ 5 ms cold per resolve call. |
| 37 | +const LATENCY_BUDGET: Duration = Duration::from_millis(5); |
| 38 | + |
| 39 | +/// Builds a representative `ssh_config(5)` covering the directives Anvil |
| 40 | +/// understands today plus a handful of additional `Host` blocks so the |
| 41 | +/// matcher actually walks more than one section. |
| 42 | +fn write_typical_config(dir: &TempDir) -> PathBuf { |
| 43 | + let path = dir.path().join("config"); |
| 44 | + let body = "\ |
| 45 | +# Global defaults — apply to every host. |
| 46 | +User defaultuser |
| 47 | +ServerAliveInterval 30 |
| 48 | +
|
| 49 | +Host gh |
| 50 | + HostName github.com |
| 51 | + User git |
| 52 | + Port 22 |
| 53 | + IdentityFile ~/.ssh/id_ed25519 |
| 54 | + IdentityFile ~/.ssh/id_rsa |
| 55 | + IdentitiesOnly yes |
| 56 | + StrictHostKeyChecking yes |
| 57 | + UserKnownHostsFile ~/.ssh/known_hosts |
| 58 | + HostKeyAlgorithms ssh-ed25519,rsa-sha2-512 |
| 59 | + KexAlgorithms curve25519-sha256 |
| 60 | + Ciphers chacha20-poly1305@openssh.com |
| 61 | + MACs hmac-sha2-256-etm@openssh.com |
| 62 | + ConnectTimeout 30 |
| 63 | + ConnectionAttempts 3 |
| 64 | +
|
| 65 | +Host gl |
| 66 | + HostName gitlab.com |
| 67 | + User git |
| 68 | + Port 22 |
| 69 | + IdentityFile ~/.ssh/id_ed25519 |
| 70 | +
|
| 71 | +Host *.work.example.com |
| 72 | + User work |
| 73 | + ProxyJump bastion.work.example.com |
| 74 | +
|
| 75 | +Host bastion.work.example.com |
| 76 | + User bastion |
| 77 | + ProxyCommand ssh -W %h:%p jump.work.example.com |
| 78 | +
|
| 79 | +Host work |
| 80 | + HostName work.example.com |
| 81 | + User worker |
| 82 | + Port 2222 |
| 83 | +
|
| 84 | +Host !legacy * |
| 85 | + PreferredAuthentications publickey |
| 86 | +"; |
| 87 | + fs::write(&path, body).expect("write fixture"); |
| 88 | + path |
| 89 | +} |
| 90 | + |
| 91 | +fn bench_resolve_typical(c: &mut Criterion) { |
| 92 | + let dir = tempfile::tempdir().expect("tempdir"); |
| 93 | + let conf = write_typical_config(&dir); |
| 94 | + let paths = SshConfigPaths { |
| 95 | + user: Some(conf), |
| 96 | + system: None, |
| 97 | + }; |
| 98 | + |
| 99 | + c.bench_function("resolve_typical_user_config", |b| { |
| 100 | + b.iter(|| { |
| 101 | + // The resolver re-reads the file each call; that is intentional |
| 102 | + // — NFR-15 is about cold latency and the hot-path mtime cache |
| 103 | + // listed as a fallback in PRD §10 has not been implemented. |
| 104 | + resolve("gh", &paths).expect("resolve") |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + // Hard-fail enforcement: re-measure outside Criterion's statistical |
| 109 | + // pipeline and panic if the median exceeds the budget. Criterion's |
| 110 | + // own threshold detection emits warnings but does not fail the run. |
| 111 | + enforce_budget(&paths); |
| 112 | +} |
| 113 | + |
| 114 | +fn bench_resolve_no_match(c: &mut Criterion) { |
| 115 | + // Worst-case path within a single-file config: walk every Host block |
| 116 | + // and find no match, so the resolver returns just the Global block's |
| 117 | + // directives. |
| 118 | + let dir = tempfile::tempdir().expect("tempdir"); |
| 119 | + let conf = write_typical_config(&dir); |
| 120 | + let paths = SshConfigPaths { |
| 121 | + user: Some(conf), |
| 122 | + system: None, |
| 123 | + }; |
| 124 | + |
| 125 | + c.bench_function("resolve_no_match", |b| { |
| 126 | + b.iter(|| resolve("nothing-matches.example.org", &paths).expect("resolve")); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +/// Median of 32 cold runs must stay under the NFR-15 budget; hard-fail |
| 131 | +/// otherwise. Runs after the Criterion measurement so the panic appears |
| 132 | +/// after the per-bench summary in stdout. |
| 133 | +fn enforce_budget(paths: &SshConfigPaths) { |
| 134 | + const RUNS: usize = 32; |
| 135 | + let mut measurements: Vec<Duration> = Vec::with_capacity(RUNS); |
| 136 | + for _ in 0..RUNS { |
| 137 | + let start = std::time::Instant::now(); |
| 138 | + let _ = resolve("gh", paths).expect("resolve"); |
| 139 | + measurements.push(start.elapsed()); |
| 140 | + } |
| 141 | + measurements.sort(); |
| 142 | + let median = measurements[RUNS / 2]; |
| 143 | + |
| 144 | + eprintln!( |
| 145 | + "ssh_config_latency: median resolve() = {} µs (budget = {} µs)", |
| 146 | + median.as_micros(), |
| 147 | + LATENCY_BUDGET.as_micros(), |
| 148 | + ); |
| 149 | + |
| 150 | + assert!( |
| 151 | + median <= LATENCY_BUDGET, |
| 152 | + "ssh_config_latency: NFR-15 budget exceeded — median {} µs > {} µs. \ |
| 153 | + If this is intentional (e.g. richer matrix coverage), bump the \ |
| 154 | + budget in benches/ssh_config_latency.rs.", |
| 155 | + median.as_micros(), |
| 156 | + LATENCY_BUDGET.as_micros(), |
| 157 | + ); |
| 158 | +} |
| 159 | + |
| 160 | +criterion_group!(benches, bench_resolve_typical, bench_resolve_no_match); |
| 161 | +criterion_main!(benches); |
0 commit comments