|
| 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); |
0 commit comments