|
1 | | -//! Micro-benchmark for the tracing layer's per-span overhead. |
| 1 | +//! Multi-thread bench for the tracing layer. |
2 | 2 | //! |
3 | | -//! Uses a current_thread runtime so block_on runs on the worker thread |
4 | | -//! (which has a TelemetryHandle). This measures the actual encoding cost. |
5 | | -//! |
6 | | -//! Two groups: |
7 | | -//! - `tracing_only`: spans with registry subscriber (no dial9 encoding) |
8 | | -//! - `with_dial9`: spans with Dial9TokioLayer (full encoding path) |
9 | | -//! |
10 | | -//! The difference between the two is the dial9 encoding overhead. |
| 3 | +//! Measures span emission throughput as N threads share one |
| 4 | +//! `Dial9TokioLayer` (and therefore one schemas Mutex). Each thread |
| 5 | +//! emits 100 spans after a barrier sync; the iteration is timed end to |
| 6 | +//! end across N ∈ {1, 2, 4, 8, 16, 32}. |
11 | 7 | //! |
12 | 8 | //! Usage: |
13 | 9 | //! cargo bench --bench tracing_layer_bench --features tracing-layer |
14 | 10 |
|
15 | 11 | use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; |
16 | | -use dial9_tokio_telemetry::telemetry::{NullWriter, TracedRuntime}; |
17 | 12 | use dial9_tokio_telemetry::tracing_layer::Dial9TokioLayer; |
| 13 | +use std::sync::{Arc, Barrier}; |
| 14 | +use tracing::Dispatch; |
18 | 15 | use tracing_subscriber::prelude::*; |
19 | 16 |
|
20 | | -fn bench_tracing_only(c: &mut Criterion) { |
21 | | - let mut group = c.benchmark_group("tracing_only"); |
22 | | - |
23 | | - // current_thread runtime: block_on IS the worker thread |
24 | | - let mut builder = tokio::runtime::Builder::new_current_thread(); |
25 | | - builder.enable_all(); |
26 | | - let (runtime, _guard) = TracedRuntime::builder() |
27 | | - .build_and_start(builder, NullWriter) |
28 | | - .unwrap(); |
29 | | - |
30 | | - // Registry only, no dial9 layer. Use set_default (thread-local) since |
31 | | - // set_global_default can only be called once. |
32 | | - let subscriber = tracing_subscriber::registry(); |
33 | | - let _sub_guard = tracing::subscriber::set_default(subscriber); |
34 | | - |
35 | | - group.bench_function("baseline", |b| { |
36 | | - b.iter(|| { |
37 | | - runtime.block_on(async { std::hint::black_box(42) }); |
38 | | - }); |
39 | | - }); |
40 | | - |
41 | | - for depth in [1, 3, 5] { |
42 | | - group.bench_with_input(BenchmarkId::new("depth", depth), &depth, |b, &depth| { |
43 | | - b.iter(|| { |
44 | | - runtime.block_on(async { nested_spans(depth) }); |
45 | | - }); |
46 | | - }); |
47 | | - } |
48 | | - |
49 | | - group.bench_function("with_fields", |b| { |
50 | | - b.iter(|| { |
51 | | - runtime.block_on(async { |
52 | | - let span = tracing::info_span!( |
53 | | - "fielded", |
54 | | - user_id = 42, |
55 | | - method = "GET", |
56 | | - path = "/api/v1/users" |
57 | | - ); |
58 | | - let _enter = span.enter(); |
59 | | - }); |
60 | | - }); |
61 | | - }); |
62 | | - |
63 | | - group.finish(); |
64 | | -} |
65 | | - |
66 | | -fn bench_with_dial9(c: &mut Criterion) { |
67 | | - let mut group = c.benchmark_group("with_dial9"); |
68 | | - |
69 | | - let mut builder = tokio::runtime::Builder::new_current_thread(); |
70 | | - builder.enable_all(); |
71 | | - let (runtime, _guard) = TracedRuntime::builder() |
72 | | - .build_and_start(builder, NullWriter) |
73 | | - .unwrap(); |
74 | | - |
75 | | - let subscriber = tracing_subscriber::registry().with(Dial9TokioLayer::new()); |
76 | | - let _sub_guard = tracing::subscriber::set_default(subscriber); |
77 | | - |
78 | | - group.bench_function("baseline", |b| { |
79 | | - b.iter(|| { |
80 | | - runtime.block_on(async { std::hint::black_box(42) }); |
81 | | - }); |
82 | | - }); |
83 | | - |
84 | | - for depth in [1, 3, 5] { |
85 | | - group.bench_with_input(BenchmarkId::new("depth", depth), &depth, |b, &depth| { |
86 | | - b.iter(|| { |
87 | | - runtime.block_on(async { nested_spans(depth) }); |
88 | | - }); |
89 | | - }); |
90 | | - } |
91 | | - |
92 | | - group.bench_function("with_fields", |b| { |
93 | | - b.iter(|| { |
94 | | - runtime.block_on(async { |
95 | | - let span = tracing::info_span!( |
96 | | - "fielded", |
97 | | - user_id = 42, |
98 | | - method = "GET", |
99 | | - path = "/api/v1/users" |
100 | | - ); |
101 | | - let _enter = span.enter(); |
102 | | - }); |
103 | | - }); |
104 | | - }); |
105 | | - |
106 | | - group.finish(); |
107 | | -} |
108 | | - |
109 | | -fn nested_spans(depth: usize) { |
110 | | - if depth == 0 { |
111 | | - return; |
112 | | - } |
113 | | - let span = tracing::info_span!("nested", level = depth); |
114 | | - let _enter = span.enter(); |
115 | | - nested_spans(depth - 1); |
116 | | -} |
117 | | - |
118 | 17 | fn bench_multi_thread(c: &mut Criterion) { |
119 | | - use std::sync::{Arc, Barrier}; |
120 | | - use tracing::Dispatch; |
121 | | - |
122 | 18 | let mut group = c.benchmark_group("multi_thread"); |
123 | 19 | group.measurement_time(std::time::Duration::from_secs(8)); |
124 | 20 |
|
@@ -157,10 +53,5 @@ fn bench_multi_thread(c: &mut Criterion) { |
157 | 53 | group.finish(); |
158 | 54 | } |
159 | 55 |
|
160 | | -criterion_group!( |
161 | | - benches, |
162 | | - bench_tracing_only, |
163 | | - bench_with_dial9, |
164 | | - bench_multi_thread |
165 | | -); |
| 56 | +criterion_group!(benches, bench_multi_thread); |
166 | 57 | criterion_main!(benches); |
0 commit comments