|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | +use opentelemetry::{metrics::MeterProvider as _, Key, KeyValue}; |
| 3 | +use opentelemetry_sdk::metrics::{Instrument, ManualReader, SdkMeterProvider, Stream, Temporality}; |
| 4 | + |
| 5 | +// Run this benchmark with: |
| 6 | +// cargo bench --bench bound_instruments --features metrics,experimental_metrics_custom_reader,experimental_metrics_bound_instruments,spec_unstable_metrics_views |
| 7 | +// |
| 8 | +// Apple M4 Max, 16 cores (12 performance + 4 efficiency), macOS 15.4 |
| 9 | +// |
| 10 | +// Results (3 attributes: method, status, path): |
| 11 | +// Counter_Unbound_Delta time: [50.20 ns] |
| 12 | +// Counter_Bound_Delta time: [ 1.80 ns] ~28x faster |
| 13 | +// Counter_Bound_With_View_Delta time: [ 1.82 ns] view filter applied at bind, not on hot path |
| 14 | +// Counter_Bound_AtOverflow_Delta time: [ 1.82 ns] bind() at cardinality limit binds directly to the overflow |
| 15 | +// tracker — perf parity with a normal bind, no per-call resolution |
| 16 | +// Histogram_Unbound_Delta time: [58.64 ns] |
| 17 | +// Histogram_Bound_Delta time: [ 6.50 ns] ~9.0x faster |
| 18 | +// Histogram_Bound_AtOverflow_Delta time: [ 6.58 ns] perf parity with a normal bind |
| 19 | +// Counter_Bound_Multithread/2 time: [21.59 µs] (100 adds/thread) |
| 20 | +// Counter_Bound_Multithread/4 time: [37.21 µs] (100 adds/thread) |
| 21 | +// Counter_Bound_Multithread/8 time: [71.70 µs] (100 adds/thread) |
| 22 | +// |
| 23 | +// Note: criterion does not fail CI on regression by itself. These numbers are |
| 24 | +// reference values for human review; use `cargo criterion --baseline` locally |
| 25 | +// if you need automated comparison against a saved baseline. |
| 26 | + |
| 27 | +fn create_provider(temporality: Temporality) -> SdkMeterProvider { |
| 28 | + let reader = ManualReader::builder() |
| 29 | + .with_temporality(temporality) |
| 30 | + .build(); |
| 31 | + SdkMeterProvider::builder().with_reader(reader).build() |
| 32 | +} |
| 33 | + |
| 34 | +fn bench_bound_instruments(c: &mut Criterion) { |
| 35 | + let mut group = c.benchmark_group("BoundInstruments"); |
| 36 | + group.sample_size(100); |
| 37 | + |
| 38 | + let attrs = [ |
| 39 | + KeyValue::new("method", "GET"), |
| 40 | + KeyValue::new("status", "200"), |
| 41 | + KeyValue::new("path", "/api/v1/users"), |
| 42 | + ]; |
| 43 | + |
| 44 | + // Counter: Unbound vs Bound (Delta) |
| 45 | + { |
| 46 | + let provider = create_provider(Temporality::Delta); |
| 47 | + let meter = provider.meter("bench"); |
| 48 | + let counter = meter.u64_counter("unbound").build(); |
| 49 | + group.bench_function("Counter_Unbound_Delta", |b| { |
| 50 | + b.iter(|| counter.add(1, &attrs)); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + { |
| 55 | + let provider = create_provider(Temporality::Delta); |
| 56 | + let meter = provider.meter("bench"); |
| 57 | + let counter = meter.u64_counter("bound").build(); |
| 58 | + let bound = counter.bind(&attrs); |
| 59 | + group.bench_function("Counter_Bound_Delta", |b| { |
| 60 | + b.iter(|| bound.add(1)); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // Counter: Bound with a View filter — confirms the filter is applied at |
| 65 | + // bind() time and the hot path stays free of attribute processing. |
| 66 | + { |
| 67 | + let view = |i: &opentelemetry_sdk::metrics::Instrument| { |
| 68 | + if i.name() == "bound_with_view" { |
| 69 | + Stream::builder() |
| 70 | + .with_allowed_attribute_keys(vec![ |
| 71 | + Key::new("method"), |
| 72 | + Key::new("status"), |
| 73 | + Key::new("path"), |
| 74 | + ]) |
| 75 | + .build() |
| 76 | + .ok() |
| 77 | + } else { |
| 78 | + None |
| 79 | + } |
| 80 | + }; |
| 81 | + let reader = ManualReader::builder() |
| 82 | + .with_temporality(Temporality::Delta) |
| 83 | + .build(); |
| 84 | + let provider = SdkMeterProvider::builder() |
| 85 | + .with_reader(reader) |
| 86 | + .with_view(view) |
| 87 | + .build(); |
| 88 | + let meter = provider.meter("bench"); |
| 89 | + let counter = meter.u64_counter("bound_with_view").build(); |
| 90 | + let bound = counter.bind(&attrs); |
| 91 | + group.bench_function("Counter_Bound_With_View_Delta", |b| { |
| 92 | + b.iter(|| bound.add(1)); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + // Counter: Bound at overflow — confirms that binding when the cardinality |
| 97 | + // limit is exhausted yields the same hot-path performance as a normal bind |
| 98 | + // (writes go directly to the overflow tracker, no per-call resolution). |
| 99 | + { |
| 100 | + let cardinality_limit = 4; |
| 101 | + let view = move |i: &Instrument| { |
| 102 | + if i.name() == "bound_at_overflow" { |
| 103 | + Stream::builder() |
| 104 | + .with_cardinality_limit(cardinality_limit) |
| 105 | + .build() |
| 106 | + .ok() |
| 107 | + } else { |
| 108 | + None |
| 109 | + } |
| 110 | + }; |
| 111 | + let reader = ManualReader::builder() |
| 112 | + .with_temporality(Temporality::Delta) |
| 113 | + .build(); |
| 114 | + let provider = SdkMeterProvider::builder() |
| 115 | + .with_reader(reader) |
| 116 | + .with_view(view) |
| 117 | + .build(); |
| 118 | + let meter = provider.meter("bench"); |
| 119 | + let counter = meter.u64_counter("bound_at_overflow").build(); |
| 120 | + // Saturate cardinality with unbound calls so bind() lands in overflow. |
| 121 | + for i in 0..cardinality_limit { |
| 122 | + counter.add(1, &[KeyValue::new("filler", i as i64)]); |
| 123 | + } |
| 124 | + let bound = counter.bind(&attrs); |
| 125 | + group.bench_function("Counter_Bound_AtOverflow_Delta", |b| { |
| 126 | + b.iter(|| bound.add(1)); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + // Histogram: Unbound vs Bound (Delta) |
| 131 | + { |
| 132 | + let provider = create_provider(Temporality::Delta); |
| 133 | + let meter = provider.meter("bench"); |
| 134 | + let histogram = meter.f64_histogram("unbound_hist").build(); |
| 135 | + group.bench_function("Histogram_Unbound_Delta", |b| { |
| 136 | + b.iter(|| histogram.record(1.5, &attrs)); |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + { |
| 141 | + let provider = create_provider(Temporality::Delta); |
| 142 | + let meter = provider.meter("bench"); |
| 143 | + let histogram = meter.f64_histogram("bound_hist").build(); |
| 144 | + let bound = histogram.bind(&attrs); |
| 145 | + group.bench_function("Histogram_Bound_Delta", |b| { |
| 146 | + b.iter(|| bound.record(1.5)); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + // Histogram: Bound at overflow — same property as the counter version. |
| 151 | + { |
| 152 | + let cardinality_limit = 4; |
| 153 | + let view = move |i: &Instrument| { |
| 154 | + if i.name() == "bound_hist_at_overflow" { |
| 155 | + Stream::builder() |
| 156 | + .with_cardinality_limit(cardinality_limit) |
| 157 | + .build() |
| 158 | + .ok() |
| 159 | + } else { |
| 160 | + None |
| 161 | + } |
| 162 | + }; |
| 163 | + let reader = ManualReader::builder() |
| 164 | + .with_temporality(Temporality::Delta) |
| 165 | + .build(); |
| 166 | + let provider = SdkMeterProvider::builder() |
| 167 | + .with_reader(reader) |
| 168 | + .with_view(view) |
| 169 | + .build(); |
| 170 | + let meter = provider.meter("bench"); |
| 171 | + let histogram = meter.f64_histogram("bound_hist_at_overflow").build(); |
| 172 | + for i in 0..cardinality_limit { |
| 173 | + histogram.record(1.5, &[KeyValue::new("filler", i as i64)]); |
| 174 | + } |
| 175 | + let bound = histogram.bind(&attrs); |
| 176 | + group.bench_function("Histogram_Bound_AtOverflow_Delta", |b| { |
| 177 | + b.iter(|| bound.record(1.5)); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + // Multi-threaded bound counter |
| 182 | + for num_threads in [2, 4, 8] { |
| 183 | + let provider = create_provider(Temporality::Delta); |
| 184 | + let meter = provider.meter("bench"); |
| 185 | + let counter = meter.u64_counter("mt_bound").build(); |
| 186 | + let bound = counter.bind(&attrs); |
| 187 | + |
| 188 | + group.bench_function(format!("Counter_Bound_Multithread/{num_threads}"), |b| { |
| 189 | + b.iter(|| { |
| 190 | + std::thread::scope(|s| { |
| 191 | + for _ in 0..num_threads { |
| 192 | + s.spawn(|| { |
| 193 | + for _ in 0..100 { |
| 194 | + bound.add(1); |
| 195 | + } |
| 196 | + }); |
| 197 | + } |
| 198 | + }); |
| 199 | + }); |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + group.finish(); |
| 204 | +} |
| 205 | + |
| 206 | +criterion_group!(benches, bench_bound_instruments); |
| 207 | +criterion_main!(benches); |
0 commit comments