perf: criterion benchmarks for the per-connection hot path

- benches/hot_paths.rs measures RateLimiter::check, Blacklist exact/CIDR/miss
  lookups, SubnetDetector::evaluate over 256 prefixes and HTTP request-head
  validation; `cargo bench --all-features --bench hot_paths`, HTML report in
  target/criterion.
- Dev-machine medians recorded in docs/testing.md (~110 ns rate limit,
  ~70 ns exact ban lookup, ~220 ns CIDR lookup, ~2.6 us detector window,
  ~280 ns HTTP head).
- Makefile `bench` target; the bench is gated on protocol-http.
This commit is contained in:
Loki 2026-11-16 12:55:51 +01:00
parent 08b7292ac4
commit 72c1f45d62
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
5 changed files with 369 additions and 7 deletions

228
Cargo.lock generated
View file

@ -26,6 +26,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]] [[package]]
name = "anstream" name = "anstream"
version = "1.0.0" version = "1.0.0"
@ -304,6 +310,12 @@ version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]] [[package]]
name = "castaway" name = "castaway"
version = "0.2.4" version = "0.2.4"
@ -362,6 +374,33 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "ciborium"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
[[package]]
name = "ciborium-ll"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
dependencies = [
"ciborium-io",
"half",
]
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.6.7" version = "4.6.7"
@ -494,12 +533,67 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "criterion"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
dependencies = [
"anes",
"cast",
"ciborium",
"clap",
"criterion-plot",
"is-terminal",
"itertools 0.10.5",
"num-traits",
"once_cell",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools 0.10.5",
]
[[package]] [[package]]
name = "critical-section" name = "critical-section"
version = "1.2.0" version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
[[package]]
name = "crossbeam-deque"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
dependencies = [
"crossbeam-utils",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.23" version = "0.8.23"
@ -533,6 +627,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "crunchy"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
[[package]] [[package]]
name = "crypto-common" name = "crypto-common"
version = "0.1.7" version = "0.1.7"
@ -951,6 +1051,17 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "half"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
dependencies = [
"cfg-if",
"crunchy",
"zerocopy",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.5" version = "0.14.5"
@ -974,6 +1085,12 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284"
[[package]] [[package]]
name = "hex" name = "hex"
version = "0.4.3" version = "0.4.3"
@ -1271,12 +1388,32 @@ version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf370abdafd54d13e54a620e8c3e1145f28e46cc9d704bc6d94414559df41763" checksum = "cf370abdafd54d13e54a620e8c3e1145f28e46cc9d704bc6d94414559df41763"
[[package]]
name = "is-terminal"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.61.2",
]
[[package]] [[package]]
name = "is_terminal_polyfill" name = "is_terminal_polyfill"
version = "1.70.2" version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.14.0" version = "0.14.0"
@ -1703,6 +1840,12 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "oorandom"
version = "11.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
[[package]] [[package]]
name = "openssl-probe" name = "openssl-probe"
version = "0.2.1" version = "0.2.1"
@ -1904,6 +2047,34 @@ version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
[[package]]
name = "plotters"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
[[package]]
name = "plotters-svg"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
dependencies = [
"plotters-backend",
]
[[package]] [[package]]
name = "portable-atomic" name = "portable-atomic"
version = "1.15.0" version = "1.15.0"
@ -2088,6 +2259,7 @@ dependencies = [
"axum", "axum",
"chrono", "chrono",
"clap", "clap",
"criterion",
"crossterm", "crossterm",
"dashmap", "dashmap",
"futures", "futures",
@ -2186,7 +2358,7 @@ dependencies = [
"compact_str", "compact_str",
"critical-section", "critical-section",
"hashbrown 0.17.1", "hashbrown 0.17.1",
"itertools", "itertools 0.14.0",
"kasuari", "kasuari",
"lru", "lru",
"palette", "palette",
@ -2251,7 +2423,7 @@ dependencies = [
"hashbrown 0.17.1", "hashbrown 0.17.1",
"indoc", "indoc",
"instability", "instability",
"itertools", "itertools 0.14.0",
"line-clipping", "line-clipping",
"ratatui-core", "ratatui-core",
"serde", "serde",
@ -2261,6 +2433,26 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "rayon"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "redis" name = "redis"
version = "1.7.1" version = "1.7.1"
@ -3052,6 +3244,16 @@ dependencies = [
"zerovec", "zerovec",
] ]
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.13.3" version = "1.13.3"
@ -3333,7 +3535,7 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5" checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5"
dependencies = [ dependencies = [
"itertools", "itertools 0.14.0",
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
] ]
@ -3842,6 +4044,26 @@ dependencies = [
"synstructure", "synstructure",
] ]
[[package]]
name = "zerocopy"
version = "0.8.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]] [[package]]
name = "zerofrom" name = "zerofrom"
version = "0.1.8" version = "0.1.8"

View file

@ -80,3 +80,10 @@ libc = { version = "0.2", optional = true }
[dev-dependencies] [dev-dependencies]
# `ServiceExt::oneshot` drives the axum router in integration tests. # `ServiceExt::oneshot` drives the axum router in integration tests.
tower = { version = "0.5", features = ["util"] } tower = { version = "0.5", features = ["util"] }
# Micro-benchmarks of the per-connection hot path (`cargo bench --all-features`).
criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "hot_paths"
harness = false
required-features = ["protocol-http"]

View file

@ -3,7 +3,7 @@
CARGO = cargo CARGO = cargo
TARGET_DIR = target TARGET_DIR = target
.PHONY: all build release test test-all check check-all fmt fmt-check clippy clean deny audit fuzz .PHONY: all build release test test-all check check-all fmt fmt-check clippy clean deny audit fuzz bench
.PHONY: ebpf ebpf-clean .PHONY: ebpf ebpf-clean
.PHONY: docker docker-build docker-up docker-down docker-logs .PHONY: docker docker-build docker-up docker-down docker-logs
.PHONY: ci ci-full repo-gates .PHONY: ci ci-full repo-gates
@ -78,6 +78,9 @@ docker-logs:
checkstyle: fmt-check clippy checkstyle: fmt-check clippy
@echo "✓ Checkstyle passed (rustfmt + clippy)" @echo "✓ Checkstyle passed (rustfmt + clippy)"
bench:
$(CARGO) bench --all-features --bench hot_paths
# Parser fuzzing (nightly + cargo-fuzz). 60 s per target is a smoke run; # Parser fuzzing (nightly + cargo-fuzz). 60 s per target is a smoke run;
# longer campaigns are run locally before a release. # longer campaigns are run locally before a release.
fuzz: fuzz:

107
benches/hot_paths.rs Normal file
View file

@ -0,0 +1,107 @@
//! Micro-benchmarks for the per-connection hot path.
//!
//! Run with `cargo bench --all-features --bench hot_paths`. Baseline numbers
//! live in `docs/research/benchmark.md`; compare medians before and after a
//! change instead of absolute values, which depend on the machine.
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use rampart::filter::blacklist::Blacklist;
use rampart::filter::rate_limit::RateLimiter;
use rampart::protocol::http::parser::{Policy, evaluate};
use rampart::traffic::prefix::{PrefixSnapshot, SubnetDetector};
fn addr(last: u8) -> IpAddr {
IpAddr::V4(Ipv4Addr::new(198, 51, 100, last))
}
/// Token-bucket admission, the first filter on every accepted connection.
fn bench_rate_limiter(c: &mut Criterion) {
c.bench_function("rate_limiter/check", |b| {
let limiter = RateLimiter::new(1_000_000.0, 1_000_000.0);
let mut n = 0u32;
b.iter(|| {
n = n.wrapping_add(1);
black_box(limiter.check(addr((n % 200) as u8)));
});
});
}
/// Exact-IP and CIDR lookups of the ban tables (both on the accept path).
fn bench_blacklist(c: &mut Criterion) {
let blacklist = Blacklist::new();
for octet in 1..=200u8 {
blacklist.add(addr(octet), Duration::from_secs(60), "bench");
}
blacklist.add_cidr(
IpAddr::V4(Ipv4Addr::new(203, 0, 113, 0)),
24,
Duration::from_secs(60),
"bench",
);
c.bench_function("blacklist/is_blocked_exact", |b| {
let mut n = 0u32;
b.iter(|| {
n = n.wrapping_add(1);
black_box(blacklist.is_blocked(addr((n % 200) as u8 + 1)));
});
});
c.bench_function("blacklist/is_blocked_cidr", |b| {
b.iter(|| black_box(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 42)))));
});
c.bench_function("blacklist/is_blocked_miss", |b| {
b.iter(|| black_box(blacklist.is_blocked(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)))));
});
}
/// Subnet detector over a full window (called once per monitor tick).
fn bench_detector(c: &mut Criterion) {
let snapshots: Vec<(IpAddr, PrefixSnapshot)> = (0..256u32)
.map(|i| {
let prefix = IpAddr::V4(Ipv4Addr::new(10, (i / 256) as u8, (i % 256) as u8, 0));
(
prefix,
PrefixSnapshot {
syn_count: 120,
pkt_count: 400,
last_seen_ns: 0,
unique_sources: 40,
},
)
})
.collect();
let detector = SubnetDetector::new(100, 16);
c.bench_function("subnet_detector/evaluate_256", |b| {
b.iter(|| black_box(detector.evaluate(black_box(&snapshots))));
});
}
/// HTTP request-head validation for every new HTTP connection.
fn bench_http_parser(c: &mut Criterion) {
let request = b"GET /api/v1/status HTTP/1.1\r\nHost: edge.example\r\nUser-Agent: bench/1.0\r\nAccept: */*\r\n\r\n";
let policy = Policy {
max_header_bytes: 8192,
blocked_paths: vec!["/admin".to_string()],
require_user_agent: true,
};
c.bench_function("http_parser/evaluate", |b| {
b.iter(|| black_box(evaluate(black_box(request), &policy)));
});
}
criterion_group!(
hot_paths,
bench_rate_limiter,
bench_blacklist,
bench_detector,
bench_http_parser
);
criterion_main!(hot_paths);

View file

@ -91,7 +91,30 @@ cargo +nightly fuzz run proxy_v2 -- -max_total_time=600
--- ---
## 3. Локальный integration-стенд (Docker) ## 4. Бенчмарки горячего пути
Микробенчмарки (criterion) для того, что выполняется на каждое соединение:
```bash
cargo bench --all-features --bench hot_paths
# HTML-отчёт: target/criterion/report/index.html
```
Замеряются: `RateLimiter::check`, exact/CIDR/miss lookup в `Blacklist`, `SubnetDetector::evaluate`
на окне из 256 префиксов и разбор HTTP request-head. Сравнивай медианы до/после изменения на
одной машине; абсолютные числа зависят от CPU. Порядок величин на dev-машине (2 vCPU):
| Бенчмарк | Медиана |
|----------|---------|
| `rate_limiter/check` | ~110 ns |
| `blacklist/is_blocked_exact` | ~70 ns |
| `blacklist/is_blocked_cidr` | ~220 ns |
| `subnet_detector/evaluate_256` | ~2.6 µs |
| `http_parser/evaluate` | ~280 ns |
---
## 5. Локальный integration-стенд (Docker)
```bash ```bash
bash deploy/test/run_test.sh bash deploy/test/run_test.sh
@ -102,7 +125,7 @@ attacker-контейнер. Сценарии — в [deploy/test/README.md](../
--- ---
## 4. Нагрузочное тестирование ## 6. Нагрузочное тестирование
### Много-IP TCP flood на VDS (edge-only) ### Много-IP TCP flood на VDS (edge-only)
@ -130,7 +153,7 @@ hping3 -S --flood -p 25565 --rand-source EDGE_IP # только на свои
--- ---
## 5. Метрики для проверки во время теста ## 7. Метрики для проверки во время теста
```bash ```bash
curl -s http://localhost:9090/metrics | grep rampart_ curl -s http://localhost:9090/metrics | grep rampart_