Skip to content

Commit 824fc37

Browse files
committed
perf: Stabilize existing Context benchmark by adding black_box
1 parent 81fea07 commit 824fc37

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 opentelemetry::{
33
global::BoxedTracer,
44
trace::{
@@ -30,27 +30,57 @@ fn criterion_benchmark(c: &mut Criterion) {
3030
group.bench_function(
3131
BenchmarkId::new("has_active_span", param.clone()),
3232
|b| match api {
33-
Api::Alt => b.iter(|| Context::map_current(TraceContextExt::has_active_span)),
34-
Api::Spec => b.iter(|| Context::current().has_active_span()),
33+
Api::Alt => b.iter(has_active_span_alt),
34+
Api::Spec => b.iter(has_active_span_spec),
3535
},
3636
);
3737
group.bench_function(
3838
BenchmarkId::new("is_sampled", param.clone()),
3939
|b| match api {
40-
Api::Alt => {
41-
b.iter(|| Context::map_current(|cx| cx.span().span_context().is_sampled()))
42-
}
43-
Api::Spec => b.iter(|| Context::current().span().span_context().is_sampled()),
40+
Api::Alt => b.iter(is_sampled_alt),
41+
Api::Spec => b.iter(is_sampled_spec),
4442
},
4543
);
4644
group.bench_function(BenchmarkId::new("is_recording", param), |b| match api {
47-
Api::Alt => b.iter(|| Context::map_current(|cx| cx.span().is_recording())),
48-
Api::Spec => b.iter(|| Context::current().span().is_recording()),
45+
Api::Alt => b.iter(is_recording_alt),
46+
Api::Spec => b.iter(is_recording_spec),
4947
});
5048
}
5149
}
5250
}
5351

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

0 commit comments

Comments
 (0)