|
1 | | -use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; |
| 1 | +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; |
2 | 2 | use opentelemetry::{ |
3 | 3 | global::BoxedTracer, |
4 | 4 | trace::{ |
@@ -30,27 +30,57 @@ fn criterion_benchmark(c: &mut Criterion) { |
30 | 30 | group.bench_function( |
31 | 31 | BenchmarkId::new("has_active_span", param.clone()), |
32 | 32 | |b| match api { |
33 | | - Api::Alt => b.iter(|| Context::map_current(TraceContextExt::has_active_span)), |
34 | | - Api::Spec => b.iter(|| Context::current().has_active_span()), |
| 33 | + Api::Alt => b.iter(has_active_span_alt), |
| 34 | + Api::Spec => b.iter(has_active_span_spec), |
35 | 35 | }, |
36 | 36 | ); |
37 | 37 | group.bench_function( |
38 | 38 | BenchmarkId::new("is_sampled", param.clone()), |
39 | 39 | |b| match api { |
40 | | - Api::Alt => { |
41 | | - b.iter(|| Context::map_current(|cx| cx.span().span_context().is_sampled())) |
42 | | - } |
43 | | - Api::Spec => b.iter(|| Context::current().span().span_context().is_sampled()), |
| 40 | + Api::Alt => b.iter(is_sampled_alt), |
| 41 | + Api::Spec => b.iter(is_sampled_spec), |
44 | 42 | }, |
45 | 43 | ); |
46 | 44 | group.bench_function(BenchmarkId::new("is_recording", param), |b| match api { |
47 | | - Api::Alt => b.iter(|| Context::map_current(|cx| cx.span().is_recording())), |
48 | | - Api::Spec => b.iter(|| Context::current().span().is_recording()), |
| 45 | + Api::Alt => b.iter(is_recording_alt), |
| 46 | + Api::Spec => b.iter(is_recording_spec), |
49 | 47 | }); |
50 | 48 | } |
51 | 49 | } |
52 | 50 | } |
53 | 51 |
|
| 52 | +#[inline(never)] |
| 53 | +fn has_active_span_alt() { |
| 54 | + let _ = black_box(Context::map_current(TraceContextExt::has_active_span)); |
| 55 | +} |
| 56 | + |
| 57 | +#[inline(never)] |
| 58 | +fn has_active_span_spec() { |
| 59 | + let _ = black_box(Context::current().has_active_span()); |
| 60 | +} |
| 61 | + |
| 62 | +#[inline(never)] |
| 63 | +fn is_sampled_alt() { |
| 64 | + let _ = black_box(Context::map_current(|cx| { |
| 65 | + cx.span().span_context().is_sampled() |
| 66 | + })); |
| 67 | +} |
| 68 | + |
| 69 | +#[inline(never)] |
| 70 | +fn is_sampled_spec() { |
| 71 | + let _ = black_box(Context::current().span().span_context().is_sampled()); |
| 72 | +} |
| 73 | + |
| 74 | +#[inline(never)] |
| 75 | +fn is_recording_alt() { |
| 76 | + let _ = black_box(Context::map_current(|cx| cx.span().is_recording())); |
| 77 | +} |
| 78 | + |
| 79 | +#[inline(never)] |
| 80 | +fn is_recording_spec() { |
| 81 | + let _ = black_box(Context::current().span().is_recording()); |
| 82 | +} |
| 83 | + |
54 | 84 | #[derive(Copy, Clone)] |
55 | 85 | enum Api { |
56 | 86 | /// An alternative way which may be faster than what the spec recommends. |
|
0 commit comments