Skip to content

Commit 6730cab

Browse files
committed
Stabilize existing Context benchmark by adding black_box
1 parent b017c7b commit 6730cab

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

opentelemetry-sdk/benches/context.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
1+
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
22
use futures_util::future::BoxFuture;
33
use opentelemetry::{
44
global::BoxedTracer,
@@ -31,27 +31,57 @@ fn criterion_benchmark(c: &mut Criterion) {
3131
group.bench_function(
3232
BenchmarkId::new("has_active_span", param.clone()),
3333
|b| match api {
34-
Api::Alt => b.iter(|| Context::map_current(TraceContextExt::has_active_span)),
35-
Api::Spec => b.iter(|| Context::current().has_active_span()),
34+
Api::Alt => b.iter(has_active_span_alt),
35+
Api::Spec => b.iter(has_active_span_spec),
3636
},
3737
);
3838
group.bench_function(
3939
BenchmarkId::new("is_sampled", param.clone()),
4040
|b| match api {
41-
Api::Alt => {
42-
b.iter(|| Context::map_current(|cx| cx.span().span_context().is_sampled()))
43-
}
44-
Api::Spec => b.iter(|| Context::current().span().span_context().is_sampled()),
41+
Api::Alt => b.iter(is_sampled_alt),
42+
Api::Spec => b.iter(is_sampled_spec),
4543
},
4644
);
4745
group.bench_function(BenchmarkId::new("is_recording", param), |b| match api {
48-
Api::Alt => b.iter(|| Context::map_current(|cx| cx.span().is_recording())),
49-
Api::Spec => b.iter(|| Context::current().span().is_recording()),
46+
Api::Alt => b.iter(is_recording_alt),
47+
Api::Spec => b.iter(is_recording_spec),
5048
});
5149
}
5250
}
5351
}
5452

53+
#[inline(never)]
54+
fn has_active_span_alt() {
55+
let _ = black_box(Context::map_current(TraceContextExt::has_active_span));
56+
}
57+
58+
#[inline(never)]
59+
fn has_active_span_spec() {
60+
let _ = black_box(Context::current().has_active_span());
61+
}
62+
63+
#[inline(never)]
64+
fn is_sampled_alt() {
65+
let _ = black_box(Context::map_current(|cx| {
66+
cx.span().span_context().is_sampled()
67+
}));
68+
}
69+
70+
#[inline(never)]
71+
fn is_sampled_spec() {
72+
let _ = black_box(Context::current().span().span_context().is_sampled());
73+
}
74+
75+
#[inline(never)]
76+
fn is_recording_alt() {
77+
let _ = black_box(Context::map_current(|cx| cx.span().is_recording()));
78+
}
79+
80+
#[inline(never)]
81+
fn is_recording_spec() {
82+
let _ = black_box(Context::current().span().is_recording());
83+
}
84+
5585
#[derive(Copy, Clone)]
5686
enum Api {
5787
/// An alternative way which may be faster than what the spec recommends.

0 commit comments

Comments
 (0)