11use criterion:: {
22 black_box, criterion_group, criterion_main, measurement:: WallTime , BenchmarkGroup , BenchmarkId ,
3- Criterion , Throughput ,
3+ Criterion ,
44} ;
55use opentelemetry:: {
66 trace:: { SpanContext , TraceContextExt } ,
77 Context ,
88} ;
99
1010// Run this benchmark with:
11- // cargo bench --bench current_context
11+ // cargo bench --bench context_attach
1212
1313fn criterion_benchmark ( c : & mut Criterion ) {
1414 let span_context = Context :: new ( ) . with_remote_span_context ( SpanContext :: empty_context ( ) ) ;
@@ -29,80 +29,68 @@ fn single_cx_scope(
2929 context_type : & str ,
3030 context : & Context ,
3131) {
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- ) ;
32+ group. bench_function ( BenchmarkId :: new ( "single_cx" , context_type) , |b| {
33+ b. iter_batched (
34+ || context. clone ( ) ,
35+ |cx| {
36+ single_cx ( cx) ;
37+ } ,
38+ criterion:: BatchSize :: SmallInput ,
39+ ) ;
40+ } ) ;
4541}
4642
4743#[ inline( never) ]
4844fn single_cx ( cx : Context ) {
49- let cx = black_box ( cx. attach ( ) ) ;
45+ let _cx_guard = black_box ( cx. attach ( ) ) ;
5046 let _ = black_box ( dummy_work ( ) ) ;
51- drop ( cx) ;
5247}
5348
5449fn 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- ) ;
50+ group. bench_function ( BenchmarkId :: new ( "nested_cx" , cx_type) , |b| {
51+ b. iter_batched (
52+ || ( context. clone ( ) , context. clone ( ) ) ,
53+ |( cx1, cx2) | {
54+ nested_cx ( cx1, cx2) ;
55+ } ,
56+ criterion:: BatchSize :: SmallInput ,
57+ ) ;
58+ } ) ;
6859}
6960
7061#[ inline( never) ]
7162fn nested_cx ( cx1 : Context , cx2 : Context ) {
72- let outer = black_box ( cx1. attach ( ) ) ;
73- let inner = black_box ( cx2. attach ( ) ) ;
63+ let _outer_cx_guard = black_box ( cx1. attach ( ) ) ;
64+ let _inner_cx_guard = black_box ( cx2. attach ( ) ) ;
7465 let _ = black_box ( dummy_work ( ) ) ;
75- drop ( inner) ;
76- drop ( outer) ;
7766}
7867
7968fn overlapping_cx_scope (
8069 group : & mut BenchmarkGroup < ' _ , WallTime > ,
8170 cx_type : & str ,
8271 context : & Context ,
8372) {
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- ) ;
73+ // This is to ensure that the context is restored after the benchmark,
74+ // see https://github.com/open-telemetry/opentelemetry-rust/issues/1887
75+ let _restore_cx_guard = Context :: current ( ) . attach ( ) ;
76+ group. bench_function ( BenchmarkId :: new ( "out_of_order_cx_drop" , cx_type) , |b| {
77+ b. iter_batched (
78+ || ( context. clone ( ) , context. clone ( ) ) ,
79+ |( cx1, cx2) | {
80+ out_of_order_cx_drop ( cx1, cx2) ;
81+ } ,
82+ criterion:: BatchSize :: SmallInput ,
83+ ) ;
84+ } ) ;
9785}
9886
9987#[ inline( never) ]
100- fn overlapping_cx ( cx1 : Context , cx2 : Context ) {
101- let outer = cx1. attach ( ) ;
102- let inner = cx2. attach ( ) ;
88+ fn out_of_order_cx_drop ( cx1 : Context , cx2 : Context ) {
89+ let outer_cx_guard = cx1. attach ( ) ;
90+ let inner_cx_guard = cx2. attach ( ) ;
10391 let _ = black_box ( dummy_work ( ) ) ;
104- drop ( outer ) ;
105- drop ( inner ) ;
92+ drop ( outer_cx_guard ) ;
93+ drop ( inner_cx_guard ) ;
10694}
10795
10896#[ inline( never) ]
0 commit comments