Skip to content

Commit 1fd6940

Browse files
committed
perf(sampling): add glob matching benchmarks
1 parent 44e519f commit 1fd6940

5 files changed

Lines changed: 145 additions & 2 deletions

File tree

benchmark/run_benchmarks_ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pushd "${PROJECT_DIR}" > /dev/null
2222

2323
# Run benchmarks
2424
message "Running benchmarks"
25-
cargo bench --workspace --features libdd-crashtracker/benchmarking,libdd-sampling/v04_span -- --warm-up-time 1 --measurement-time 5 --sample-size=200
25+
cargo bench --workspace --features libdd-crashtracker/benchmarking,libdd-sampling/v04_span,libdd-sampling/bench-internals -- --warm-up-time 1 --measurement-time 5 --sample-size=200
2626
message "Finished running benchmarks"
2727

2828
# Copy the benchmark results to the output directory

libdd-sampling/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ harness = false
2222
path = "benches/sampling_bench.rs"
2323
required-features = ["v04_span"]
2424

25+
[[bench]]
26+
name = "glob_matcher_bench"
27+
harness = false
28+
path = "benches/glob_matcher_bench.rs"
29+
required-features = ["bench-internals"]
30+
2531
[dependencies]
2632
serde = { version = "1.0", features = ["derive"] }
2733
serde_json = "1.0"
@@ -31,6 +37,9 @@ libdd-trace-utils = { path = "../libdd-trace-utils", version = "3.0.1", optional
3137

3238
[features]
3339
v04_span = ["dep:libdd-trace-utils"]
40+
# Exposes internal modules (e.g. `glob_matcher`) for benchmarks. Not intended for downstream
41+
# consumers — enable only when running benches in this crate.
42+
bench-internals = []
3443

3544
[dev-dependencies]
3645
criterion = "0.5"
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Microbenchmarks for `GlobMatcher` covering the `*` short-circuit, ASCII fast path (with and
5+
//! without wildcards, including backtracking), and Unicode fallback path.
6+
7+
use std::alloc::System;
8+
use std::hint::black_box;
9+
10+
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
11+
use libdd_common::bench_utils::{
12+
memory_allocated_measurement, AllocatedBytesMeasurement, ReportingAllocator,
13+
};
14+
use libdd_sampling::glob_matcher::GlobMatcher;
15+
16+
#[global_allocator]
17+
static GLOBAL: ReportingAllocator<System> = ReportingAllocator::new(System);
18+
19+
struct BenchCase {
20+
name: &'static str,
21+
pattern: &'static str,
22+
subject: &'static str,
23+
}
24+
25+
fn cases() -> Vec<BenchCase> {
26+
vec![
27+
BenchCase {
28+
name: "star_short_circuit",
29+
pattern: "*",
30+
subject: "anything-goes-here",
31+
},
32+
BenchCase {
33+
name: "ascii_exact_match",
34+
pattern: "my-service",
35+
subject: "my-service",
36+
},
37+
BenchCase {
38+
name: "ascii_exact_miss",
39+
pattern: "my-service",
40+
subject: "other-service",
41+
},
42+
BenchCase {
43+
name: "ascii_case_insensitive_match",
44+
pattern: "my-service",
45+
subject: "MY-SERVICE",
46+
},
47+
BenchCase {
48+
name: "ascii_wildcard_star_match",
49+
pattern: "svc-*",
50+
subject: "svc-web",
51+
},
52+
BenchCase {
53+
name: "ascii_wildcard_question_match",
54+
pattern: "svc-???",
55+
subject: "svc-web",
56+
},
57+
BenchCase {
58+
name: "ascii_wildcard_backtrack_match",
59+
pattern: "*-controller",
60+
subject: "users-controller",
61+
},
62+
// Worst-case shape for the two-pointer backtracking algorithm.
63+
BenchCase {
64+
name: "ascii_wildcard_heavy_backtrack",
65+
pattern: "a*a*a*a*b",
66+
subject: "aaaaaaaaaaaaaaaaaaaab",
67+
},
68+
BenchCase {
69+
name: "unicode_pattern_wildcard_match",
70+
pattern: "caf\u{00e9}-*",
71+
subject: "CAF\u{00c9}-PAYMENT",
72+
},
73+
BenchCase {
74+
name: "unicode_pattern_ascii_subject",
75+
pattern: "caf\u{00e9}-*",
76+
subject: "CAFE-PAYMENT",
77+
},
78+
BenchCase {
79+
name: "ascii_pattern_unicode_subject",
80+
pattern: "caf*",
81+
subject: "caf\u{00e9}-controller",
82+
},
83+
BenchCase {
84+
name: "unicode_exact_match",
85+
pattern: "caf\u{00e9}",
86+
subject: "CAF\u{00c9}",
87+
},
88+
]
89+
}
90+
91+
fn bench_wall_time(c: &mut Criterion) {
92+
for case in cases() {
93+
let matcher = GlobMatcher::new(case.pattern);
94+
c.bench_function(&format!("glob_matcher/{}/wall_time", case.name), |b| {
95+
b.iter_batched(
96+
|| (),
97+
|_| {
98+
black_box(matcher.matches(black_box(case.subject)));
99+
},
100+
BatchSize::SmallInput,
101+
)
102+
});
103+
}
104+
}
105+
106+
fn bench_allocs(c: &mut Criterion<AllocatedBytesMeasurement<System>>) {
107+
for case in cases() {
108+
let matcher = GlobMatcher::new(case.pattern);
109+
c.bench_function(
110+
&format!("glob_matcher/{}/allocated_bytes", case.name),
111+
|b| {
112+
b.iter_batched(
113+
|| (),
114+
|_| {
115+
black_box(matcher.matches(black_box(case.subject)));
116+
},
117+
BatchSize::SmallInput,
118+
)
119+
},
120+
);
121+
}
122+
}
123+
124+
criterion_group!(benches, bench_wall_time);
125+
criterion_group!(
126+
name = alloc_benches;
127+
config = memory_allocated_measurement(&GLOBAL);
128+
targets = bench_allocs
129+
);
130+
criterion_main!(alloc_benches, benches);

libdd-sampling/benches/sampling_bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,4 @@ criterion_group!(
435435
config = memory_allocated_measurement(&GLOBAL);
436436
targets = criterion_benchmark_allocs
437437
);
438-
criterion_main!(benches, alloc_benches);
438+
criterion_main!(alloc_benches, benches);

libdd-sampling/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub(crate) mod constants;
2929
pub(crate) mod datadog_sampler;
3030
pub mod dd_constants;
3131
pub mod dd_sampling;
32+
#[cfg(not(feature = "bench-internals"))]
3233
pub(crate) mod glob_matcher;
34+
#[cfg(feature = "bench-internals")]
35+
#[doc(hidden)]
36+
pub mod glob_matcher;
3337
pub(crate) mod rate_limiter;
3438
pub(crate) mod rate_sampler;
3539
pub(crate) mod rules_sampler;

0 commit comments

Comments
 (0)