|
| 1 | +// Project: hyperi-rustlib |
| 2 | +// File: benches/filter_benchmark.rs |
| 3 | +// Purpose: Criterion benchmarks for transport filter engine performance |
| 4 | +// Language: Rust |
| 5 | +// |
| 6 | +// License: FSL-1.1-ALv2 |
| 7 | +// Copyright: (c) 2026 HYPERI PTY LIMITED |
| 8 | + |
| 9 | +//! Benchmarks for the transport filter engine. |
| 10 | +//! |
| 11 | +//! Validates the design assumption: Tier 1 filters are ~50-100ns/msg via SIMD, |
| 12 | +//! and the no-filter overhead is negligible. |
| 13 | +
|
| 14 | +use criterion::{Criterion, Throughput, criterion_group, criterion_main}; |
| 15 | + |
| 16 | +use hyperi_rustlib::transport::filter::{ |
| 17 | + FilterAction, FilterDisposition, FilterRule, TransportFilterEngine, TransportFilterTierConfig, |
| 18 | +}; |
| 19 | + |
| 20 | +const SAMPLE_PAYLOAD: &[u8] = br#"{"_table":"events","host":"prod-web01","source_type":"syslog","severity":3,"id":12345,"timestamp":"2026-04-10T12:00:00Z","message":"Sample log event with some realistic padding for benchmarking"}"#; |
| 21 | + |
| 22 | +const POISON_PAYLOAD: &[u8] = br#"{"_table":"events","status":"poison","data":"x"}"#; |
| 23 | + |
| 24 | +fn bench_no_filters_baseline(c: &mut Criterion) { |
| 25 | + let engine = TransportFilterEngine::empty(); |
| 26 | + |
| 27 | + let mut group = c.benchmark_group("filter_no_filters"); |
| 28 | + group.throughput(Throughput::Elements(1)); |
| 29 | + group.bench_function("apply_inbound_no_filters", |b| { |
| 30 | + b.iter(|| std::hint::black_box(engine.apply_inbound(SAMPLE_PAYLOAD))); |
| 31 | + }); |
| 32 | + group.finish(); |
| 33 | +} |
| 34 | + |
| 35 | +fn bench_tier1_field_exists(c: &mut Criterion) { |
| 36 | + let engine = TransportFilterEngine::new( |
| 37 | + &[FilterRule { |
| 38 | + expression: "has(_table)".into(), |
| 39 | + action: FilterAction::Drop, |
| 40 | + }], |
| 41 | + &[], |
| 42 | + &TransportFilterTierConfig::default(), |
| 43 | + ) |
| 44 | + .unwrap(); |
| 45 | + |
| 46 | + let mut group = c.benchmark_group("filter_tier1_field_exists"); |
| 47 | + group.throughput(Throughput::Elements(1)); |
| 48 | + group.bench_function("match", |b| { |
| 49 | + b.iter(|| std::hint::black_box(engine.apply_inbound(SAMPLE_PAYLOAD))); |
| 50 | + }); |
| 51 | + group.finish(); |
| 52 | +} |
| 53 | + |
| 54 | +fn bench_tier1_field_equals(c: &mut Criterion) { |
| 55 | + let engine = TransportFilterEngine::new( |
| 56 | + &[FilterRule { |
| 57 | + expression: r#"status == "poison""#.into(), |
| 58 | + action: FilterAction::Drop, |
| 59 | + }], |
| 60 | + &[], |
| 61 | + &TransportFilterTierConfig::default(), |
| 62 | + ) |
| 63 | + .unwrap(); |
| 64 | + |
| 65 | + let mut group = c.benchmark_group("filter_tier1_field_equals"); |
| 66 | + group.throughput(Throughput::Elements(1)); |
| 67 | + group.bench_function("no_match_pass", |b| { |
| 68 | + b.iter(|| { |
| 69 | + let result = engine.apply_inbound(SAMPLE_PAYLOAD); |
| 70 | + assert_eq!(result, FilterDisposition::Pass); |
| 71 | + std::hint::black_box(result) |
| 72 | + }); |
| 73 | + }); |
| 74 | + group.bench_function("match_drop", |b| { |
| 75 | + b.iter(|| { |
| 76 | + let result = engine.apply_inbound(POISON_PAYLOAD); |
| 77 | + assert_eq!(result, FilterDisposition::Drop); |
| 78 | + std::hint::black_box(result) |
| 79 | + }); |
| 80 | + }); |
| 81 | + group.finish(); |
| 82 | +} |
| 83 | + |
| 84 | +fn bench_tier1_starts_with(c: &mut Criterion) { |
| 85 | + let engine = TransportFilterEngine::new( |
| 86 | + &[FilterRule { |
| 87 | + expression: r#"host.startsWith("prod-")"#.into(), |
| 88 | + action: FilterAction::Drop, |
| 89 | + }], |
| 90 | + &[], |
| 91 | + &TransportFilterTierConfig::default(), |
| 92 | + ) |
| 93 | + .unwrap(); |
| 94 | + |
| 95 | + let mut group = c.benchmark_group("filter_tier1_starts_with"); |
| 96 | + group.throughput(Throughput::Elements(1)); |
| 97 | + group.bench_function("match", |b| { |
| 98 | + b.iter(|| std::hint::black_box(engine.apply_inbound(SAMPLE_PAYLOAD))); |
| 99 | + }); |
| 100 | + group.finish(); |
| 101 | +} |
| 102 | + |
| 103 | +fn bench_tier1_dotted_path(c: &mut Criterion) { |
| 104 | + let nested_payload = |
| 105 | + br#"{"metadata":{"source":"aws","region":"ap-southeast-2"},"event":"login"}"#; |
| 106 | + let engine = TransportFilterEngine::new( |
| 107 | + &[FilterRule { |
| 108 | + expression: r#"metadata.source == "aws""#.into(), |
| 109 | + action: FilterAction::Drop, |
| 110 | + }], |
| 111 | + &[], |
| 112 | + &TransportFilterTierConfig::default(), |
| 113 | + ) |
| 114 | + .unwrap(); |
| 115 | + |
| 116 | + let mut group = c.benchmark_group("filter_tier1_dotted_path"); |
| 117 | + group.throughput(Throughput::Elements(1)); |
| 118 | + group.bench_function("nested_match", |b| { |
| 119 | + b.iter(|| std::hint::black_box(engine.apply_inbound(nested_payload))); |
| 120 | + }); |
| 121 | + group.finish(); |
| 122 | +} |
| 123 | + |
| 124 | +fn bench_tier1_first_match_wins(c: &mut Criterion) { |
| 125 | + // 5 filters, message matches the third one |
| 126 | + let rules = vec![ |
| 127 | + FilterRule { |
| 128 | + expression: "has(no_match_1)".into(), |
| 129 | + action: FilterAction::Drop, |
| 130 | + }, |
| 131 | + FilterRule { |
| 132 | + expression: "has(no_match_2)".into(), |
| 133 | + action: FilterAction::Drop, |
| 134 | + }, |
| 135 | + FilterRule { |
| 136 | + expression: "has(_table)".into(), |
| 137 | + action: FilterAction::Drop, |
| 138 | + }, |
| 139 | + FilterRule { |
| 140 | + expression: "has(no_match_3)".into(), |
| 141 | + action: FilterAction::Drop, |
| 142 | + }, |
| 143 | + FilterRule { |
| 144 | + expression: "has(no_match_4)".into(), |
| 145 | + action: FilterAction::Drop, |
| 146 | + }, |
| 147 | + ]; |
| 148 | + let engine = |
| 149 | + TransportFilterEngine::new(&rules, &[], &TransportFilterTierConfig::default()).unwrap(); |
| 150 | + |
| 151 | + let mut group = c.benchmark_group("filter_tier1_first_match_wins"); |
| 152 | + group.throughput(Throughput::Elements(1)); |
| 153 | + group.bench_function("match_at_position_3", |b| { |
| 154 | + b.iter(|| std::hint::black_box(engine.apply_inbound(SAMPLE_PAYLOAD))); |
| 155 | + }); |
| 156 | + group.finish(); |
| 157 | +} |
| 158 | + |
| 159 | +#[cfg(feature = "expression")] |
| 160 | +fn bench_tier2_compound_cel(c: &mut Criterion) { |
| 161 | + let tier_config = TransportFilterTierConfig { |
| 162 | + allow_cel_filters_in: true, |
| 163 | + ..Default::default() |
| 164 | + }; |
| 165 | + let engine = TransportFilterEngine::new( |
| 166 | + &[FilterRule { |
| 167 | + expression: r#"severity > 3 && source != "internal""#.into(), |
| 168 | + action: FilterAction::Drop, |
| 169 | + }], |
| 170 | + &[], |
| 171 | + &tier_config, |
| 172 | + ) |
| 173 | + .unwrap(); |
| 174 | + |
| 175 | + let payload = br#"{"severity":5,"source":"external","data":"x"}"#; |
| 176 | + |
| 177 | + let mut group = c.benchmark_group("filter_tier2_compound_cel"); |
| 178 | + group.throughput(Throughput::Elements(1)); |
| 179 | + group.bench_function("compound_cel_match", |b| { |
| 180 | + b.iter(|| std::hint::black_box(engine.apply_inbound(payload))); |
| 181 | + }); |
| 182 | + group.finish(); |
| 183 | +} |
| 184 | + |
| 185 | +#[cfg(feature = "expression")] |
| 186 | +fn bench_tier3_regex_cel(c: &mut Criterion) { |
| 187 | + let tier_config = TransportFilterTierConfig { |
| 188 | + allow_complex_filters_in: true, |
| 189 | + ..Default::default() |
| 190 | + }; |
| 191 | + let engine = TransportFilterEngine::new( |
| 192 | + &[FilterRule { |
| 193 | + expression: r#"host.matches("^prod-.*$")"#.into(), |
| 194 | + action: FilterAction::Drop, |
| 195 | + }], |
| 196 | + &[], |
| 197 | + &tier_config, |
| 198 | + ) |
| 199 | + .unwrap(); |
| 200 | + |
| 201 | + let mut group = c.benchmark_group("filter_tier3_regex_cel"); |
| 202 | + group.throughput(Throughput::Elements(1)); |
| 203 | + group.bench_function("regex_match", |b| { |
| 204 | + b.iter(|| std::hint::black_box(engine.apply_inbound(SAMPLE_PAYLOAD))); |
| 205 | + }); |
| 206 | + group.finish(); |
| 207 | +} |
| 208 | + |
| 209 | +#[cfg(feature = "expression")] |
| 210 | +criterion_group!( |
| 211 | + benches, |
| 212 | + bench_no_filters_baseline, |
| 213 | + bench_tier1_field_exists, |
| 214 | + bench_tier1_field_equals, |
| 215 | + bench_tier1_starts_with, |
| 216 | + bench_tier1_dotted_path, |
| 217 | + bench_tier1_first_match_wins, |
| 218 | + bench_tier2_compound_cel, |
| 219 | + bench_tier3_regex_cel, |
| 220 | +); |
| 221 | + |
| 222 | +#[cfg(not(feature = "expression"))] |
| 223 | +criterion_group!( |
| 224 | + benches, |
| 225 | + bench_no_filters_baseline, |
| 226 | + bench_tier1_field_exists, |
| 227 | + bench_tier1_field_equals, |
| 228 | + bench_tier1_starts_with, |
| 229 | + bench_tier1_dotted_path, |
| 230 | + bench_tier1_first_match_wins, |
| 231 | +); |
| 232 | + |
| 233 | +criterion_main!(benches); |
0 commit comments