1
1
use criterion:: {
2
2
black_box, criterion_group, criterion_main, measurement:: WallTime , BenchmarkGroup , BenchmarkId ,
3
- Criterion , Throughput ,
3
+ Criterion ,
4
4
} ;
5
5
use opentelemetry:: {
6
6
trace:: { SpanContext , TraceContextExt } ,
7
7
Context ,
8
8
} ;
9
9
10
10
// Run this benchmark with:
11
- // cargo bench --bench current_context
11
+ // cargo bench --bench context_attach
12
12
13
13
fn criterion_benchmark ( c : & mut Criterion ) {
14
14
let span_context = Context :: new ( ) . with_remote_span_context ( SpanContext :: empty_context ( ) ) ;
@@ -29,80 +29,68 @@ fn single_cx_scope(
29
29
context_type : & str ,
30
30
context : & Context ,
31
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
- ) ;
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
+ } ) ;
45
41
}
46
42
47
43
#[ inline( never) ]
48
44
fn single_cx ( cx : Context ) {
49
- let cx = black_box ( cx. attach ( ) ) ;
45
+ let _cx_guard = black_box ( cx. attach ( ) ) ;
50
46
let _ = black_box ( dummy_work ( ) ) ;
51
- drop ( cx) ;
52
47
}
53
48
54
49
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
- ) ;
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
+ } ) ;
68
59
}
69
60
70
61
#[ inline( never) ]
71
62
fn 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 ( ) ) ;
74
65
let _ = black_box ( dummy_work ( ) ) ;
75
- drop ( inner) ;
76
- drop ( outer) ;
77
66
}
78
67
79
68
fn overlapping_cx_scope (
80
69
group : & mut BenchmarkGroup < ' _ , WallTime > ,
81
70
cx_type : & str ,
82
71
context : & Context ,
83
72
) {
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
+ } ) ;
97
85
}
98
86
99
87
#[ 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 ( ) ;
103
91
let _ = black_box ( dummy_work ( ) ) ;
104
- drop ( outer ) ;
105
- drop ( inner ) ;
92
+ drop ( outer_cx_guard ) ;
93
+ drop ( inner_cx_guard ) ;
106
94
}
107
95
108
96
#[ inline( never) ]
0 commit comments