Skip to content

Commit 5d43929

Browse files
committed
Add benchmark for Context.attach
1 parent e0159ad commit 5d43929

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

opentelemetry/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ harness = false
5757
[[bench]]
5858
name = "anyvalue"
5959
harness = false
60+
61+
[[bench]]
62+
name = "context_attach"
63+
harness = false
64+
required-features = ["tracing"]
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 _ = black_box(cx.attach());
50+
let _ = black_box(dummy_work());
51+
}
52+
53+
fn nested_cx_scope(group: &mut BenchmarkGroup<'_, WallTime>, cx_type: &str, context: &Context) {
54+
let _restore = Context::current().attach();
55+
group.throughput(Throughput::Elements(1)).bench_function(
56+
BenchmarkId::new("nested_cx_scope", cx_type),
57+
|b| {
58+
b.iter_batched(
59+
|| (context.clone(), context.clone()),
60+
|(cx1, cx2)| {
61+
nested_cx(cx1, cx2);
62+
},
63+
criterion::BatchSize::SmallInput,
64+
);
65+
},
66+
);
67+
}
68+
69+
#[inline(never)]
70+
fn nested_cx(cx1: Context, cx2: Context) {
71+
let _ = black_box(cx1.attach());
72+
let _ = black_box(cx2.attach());
73+
let _ = black_box(dummy_work());
74+
}
75+
76+
fn overlapping_cx_scope(
77+
group: &mut BenchmarkGroup<'_, WallTime>,
78+
cx_type: &str,
79+
context: &Context,
80+
) {
81+
let _restore = Context::current().attach();
82+
group.throughput(Throughput::Elements(1)).bench_function(
83+
BenchmarkId::new("overlapping_cx_scope", cx_type),
84+
|b| {
85+
b.iter_batched(
86+
|| (context.clone(), context.clone()),
87+
|(cx1, cx2)| {
88+
overlapping_cx(cx1, cx2);
89+
},
90+
criterion::BatchSize::SmallInput,
91+
);
92+
},
93+
);
94+
}
95+
96+
#[inline(never)]
97+
fn overlapping_cx(cx1: Context, cx2: Context) {
98+
let outer = cx1.attach();
99+
let inner = cx2.attach();
100+
let _ = black_box(dummy_work());
101+
drop(outer);
102+
drop(inner);
103+
}
104+
105+
#[inline(never)]
106+
fn dummy_work() -> i32 {
107+
black_box(1 + 1)
108+
}
109+
110+
fn group(c: &mut Criterion) -> BenchmarkGroup<WallTime> {
111+
c.benchmark_group("context_attach")
112+
}
113+
114+
#[derive(Debug, PartialEq)]
115+
struct Value(i32);
116+
117+
criterion_group!(benches, criterion_benchmark);
118+
119+
criterion_main!(benches);

0 commit comments

Comments
 (0)