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