Skip to content

Commit c99d76d

Browse files
committed
Add benchmark for Context.attach
1 parent 0ddc441 commit c99d76d

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

opentelemetry/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ harness = false
5656
[[bench]]
5757
name = "anyvalue"
5858
harness = false
59+
60+
[[bench]]
61+
name = "context_attach"
62+
harness = false
63+
required-features = ["tracing"]
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
use criterion::{
2+
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, BenchmarkId,
3+
Criterion, Throughput,
4+
};
5+
use opentelemetry::{
6+
trace::{SpanContext, TraceContextExt},
7+
Context,
8+
};
9+
10+
// Run this benchmark with:
11+
// cargo bench --bench current_context
12+
13+
fn criterion_benchmark(c: &mut Criterion) {
14+
let span_context = Context::new().with_remote_span_context(SpanContext::empty_context());
15+
let contexts = vec![
16+
("empty_cx", Context::new()),
17+
("single_value_cx", Context::new().with_value(Value(4711))),
18+
("span_cx", span_context),
19+
];
20+
for (name, cx) in contexts {
21+
single_cx_scope(&mut group(c), name, &cx);
22+
nested_cx_scope(&mut group(c), name, &cx);
23+
overlapping_cx_scope(&mut group(c), name, &cx);
24+
}
25+
}
26+
27+
fn single_cx_scope(
28+
group: &mut BenchmarkGroup<'_, WallTime>,
29+
context_type: &str,
30+
context: &Context,
31+
) {
32+
let _restore = Context::current().attach();
33+
group.throughput(Throughput::Elements(1)).bench_function(
34+
BenchmarkId::new("single_cx_scope", context_type),
35+
|b| {
36+
b.iter_batched(
37+
|| context.clone(),
38+
|cx| {
39+
single_cx(cx);
40+
},
41+
criterion::BatchSize::SmallInput,
42+
);
43+
},
44+
);
45+
}
46+
47+
#[inline(never)]
48+
fn single_cx(cx: Context) {
49+
let cx = black_box(cx.attach());
50+
let _ = black_box(dummy_work());
51+
drop(cx);
52+
}
53+
54+
fn nested_cx_scope(group: &mut BenchmarkGroup<'_, WallTime>, cx_type: &str, context: &Context) {
55+
let _restore = Context::current().attach();
56+
group.throughput(Throughput::Elements(1)).bench_function(
57+
BenchmarkId::new("nested_cx_scope", cx_type),
58+
|b| {
59+
b.iter_batched(
60+
|| (context.clone(), context.clone()),
61+
|(cx1, cx2)| {
62+
nested_cx(cx1, cx2);
63+
},
64+
criterion::BatchSize::SmallInput,
65+
);
66+
},
67+
);
68+
}
69+
70+
#[inline(never)]
71+
fn nested_cx(cx1: Context, cx2: Context) {
72+
let outer = black_box(cx1.attach());
73+
let inner = black_box(cx2.attach());
74+
let _ = black_box(dummy_work());
75+
drop(inner);
76+
drop(outer);
77+
}
78+
79+
fn overlapping_cx_scope(
80+
group: &mut BenchmarkGroup<'_, WallTime>,
81+
cx_type: &str,
82+
context: &Context,
83+
) {
84+
let _restore = Context::current().attach();
85+
group.throughput(Throughput::Elements(1)).bench_function(
86+
BenchmarkId::new("overlapping_cx_scope", cx_type),
87+
|b| {
88+
b.iter_batched(
89+
|| (context.clone(), context.clone()),
90+
|(cx1, cx2)| {
91+
overlapping_cx(cx1, cx2);
92+
},
93+
criterion::BatchSize::SmallInput,
94+
);
95+
},
96+
);
97+
}
98+
99+
#[inline(never)]
100+
fn overlapping_cx(cx1: Context, cx2: Context) {
101+
let outer = cx1.attach();
102+
let inner = cx2.attach();
103+
let _ = black_box(dummy_work());
104+
drop(outer);
105+
drop(inner);
106+
}
107+
108+
#[inline(never)]
109+
fn dummy_work() -> i32 {
110+
black_box(1 + 1)
111+
}
112+
113+
fn group(c: &mut Criterion) -> BenchmarkGroup<WallTime> {
114+
c.benchmark_group("context_attach")
115+
}
116+
117+
#[derive(Debug, PartialEq)]
118+
struct Value(i32);
119+
120+
criterion_group!(benches, criterion_benchmark);
121+
122+
criterion_main!(benches);

0 commit comments

Comments
 (0)