Skip to content

Commit ac73810

Browse files
committed
Stabilize existing Context benchmark by adding black_box
1 parent 8c5daa2 commit ac73810

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

opentelemetry-sdk/benches/context.rs

Lines changed: 16 additions & 8 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,22 +31,30 @@ 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 => {
35+
b.iter(|| black_box(Context::map_current(TraceContextExt::has_active_span)))
36+
}
37+
Api::Spec => b.iter(|| black_box(Context::current().has_active_span())),
3638
},
3739
);
3840
group.bench_function(
3941
BenchmarkId::new("is_sampled", param.clone()),
4042
|b| match api {
41-
Api::Alt => {
42-
b.iter(|| Context::map_current(|cx| cx.span().span_context().is_sampled()))
43+
Api::Alt => b.iter(|| {
44+
black_box(Context::map_current(|cx| {
45+
cx.span().span_context().is_sampled()
46+
}))
47+
}),
48+
Api::Spec => {
49+
b.iter(|| black_box(Context::current().span().span_context().is_sampled()))
4350
}
44-
Api::Spec => b.iter(|| Context::current().span().span_context().is_sampled()),
4551
},
4652
);
4753
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()),
54+
Api::Alt => {
55+
b.iter(|| black_box(Context::map_current(|cx| cx.span().is_recording())))
56+
}
57+
Api::Spec => b.iter(|| black_box(Context::current().span().is_recording())),
5058
});
5159
}
5260
}

0 commit comments

Comments
 (0)