|
| 1 | +/* |
| 2 | + Benchmark Results: |
| 3 | + criterion = "0.5.1" |
| 4 | + OS: macOS 15 |
| 5 | + Hardware: Apple M4 Pro, 14 cores, RAM: 24 GB |
| 6 | + | Test | Average time| |
| 7 | + |--------------------------------------------------|-------------| |
| 8 | + | CreateSpan_NoopTracer | 29.2 ns | |
| 9 | + | CreateSpan_NoopTracer_4Attributes | 29.4 ns | |
| 10 | + | CreateSpan_NoopTracer_AddEvent | 29.2 ns | |
| 11 | + | CreateSpan_NoopTracer_AddLink | 29.2 ns | |
| 12 | + | CreateSpan_NoopTracer_SetActive | 73.3 ns | |
| 13 | + | CreateSpan_NoopTracer_WithActiveParent | 102.9 ns | |
| 14 | + | CreateSpan_NoopTracer_InSpan | 78.0 ns | |
| 15 | + | SpanBuilder_Creation | 22.8 ns | |
| 16 | + | SpanBuilder_WithAttributes | 51.7 ns | |
| 17 | + | SpanBuilder_WithLinks | 69.0 ns | |
| 18 | +*/ |
| 19 | + |
| 20 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 21 | +use opentelemetry::{ |
| 22 | + global, |
| 23 | + trace::{ |
| 24 | + noop::NoopTracerProvider, Span, SpanContext, SpanId, SpanKind, TraceFlags, TraceId, |
| 25 | + TraceState, Tracer, |
| 26 | + }, |
| 27 | + KeyValue, |
| 28 | +}; |
| 29 | + |
| 30 | +// Run this benchmark with: |
| 31 | +// cargo bench --bench trace --features=trace |
| 32 | + |
| 33 | +fn valid_span_context() -> SpanContext { |
| 34 | + SpanContext::new( |
| 35 | + TraceId::from(12345u128), |
| 36 | + SpanId::from(12345u64), |
| 37 | + TraceFlags::SAMPLED, |
| 38 | + true, |
| 39 | + TraceState::default(), |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +fn criterion_benchmark(c: &mut Criterion) { |
| 44 | + global::set_tracer_provider(NoopTracerProvider::new()); |
| 45 | + let tracer = global::tracer("bench"); |
| 46 | + |
| 47 | + span_lifecycle(c, &tracer); |
| 48 | + span_builder(c, &tracer); |
| 49 | +} |
| 50 | + |
| 51 | +fn span_lifecycle( |
| 52 | + c: &mut Criterion, |
| 53 | + tracer: &impl Tracer<Span = impl Span + Send + Sync + 'static>, |
| 54 | +) { |
| 55 | + c.bench_function("CreateSpan_NoopTracer", |b| { |
| 56 | + b.iter(|| { |
| 57 | + let mut span = tracer.start("test-span"); |
| 58 | + span.end(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + c.bench_function("CreateSpan_NoopTracer_4Attributes", |b| { |
| 63 | + b.iter(|| { |
| 64 | + let mut span = tracer.start("test-span"); |
| 65 | + if span.is_recording() { |
| 66 | + span.set_attribute(KeyValue::new("key1", false)); |
| 67 | + span.set_attribute(KeyValue::new("key2", "hello")); |
| 68 | + span.set_attribute(KeyValue::new("key3", 123)); |
| 69 | + span.set_attribute(KeyValue::new("key4", 123.456)); |
| 70 | + } |
| 71 | + span.end(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + c.bench_function("CreateSpan_NoopTracer_AddEvent", |b| { |
| 76 | + b.iter(|| { |
| 77 | + let mut span = tracer.start("test-span"); |
| 78 | + if span.is_recording() { |
| 79 | + span.add_event( |
| 80 | + "my-event", |
| 81 | + vec![ |
| 82 | + KeyValue::new("key1", "value1"), |
| 83 | + KeyValue::new("key2", "value2"), |
| 84 | + ], |
| 85 | + ); |
| 86 | + } |
| 87 | + span.end(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + c.bench_function("CreateSpan_NoopTracer_AddLink", |b| { |
| 92 | + let link_context = valid_span_context(); |
| 93 | + b.iter(|| { |
| 94 | + let mut span = tracer.start("test-span"); |
| 95 | + if span.is_recording() { |
| 96 | + span.add_link( |
| 97 | + link_context.clone(), |
| 98 | + vec![ |
| 99 | + KeyValue::new("key1", "value1"), |
| 100 | + KeyValue::new("key2", "value2"), |
| 101 | + ], |
| 102 | + ); |
| 103 | + } |
| 104 | + span.end(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + c.bench_function("CreateSpan_NoopTracer_SetActive", |b| { |
| 109 | + b.iter(|| { |
| 110 | + let span = tracer.start("test-span"); |
| 111 | + let _guard = opentelemetry::trace::mark_span_as_active(span); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + c.bench_function("CreateSpan_NoopTracer_WithActiveParent", |b| { |
| 116 | + b.iter(|| { |
| 117 | + let parent = tracer.start("parent"); |
| 118 | + let _guard = opentelemetry::trace::mark_span_as_active(parent); |
| 119 | + let mut child = tracer.start("child"); |
| 120 | + child.end(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + c.bench_function("CreateSpan_NoopTracer_InSpan", |b| { |
| 125 | + b.iter(|| { |
| 126 | + tracer.in_span("test-span", |_cx| {}); |
| 127 | + }); |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +fn span_builder(c: &mut Criterion, tracer: &impl Tracer<Span = impl Span + Send + Sync + 'static>) { |
| 132 | + c.bench_function("SpanBuilder_Creation", |b| { |
| 133 | + b.iter(|| { |
| 134 | + let span = tracer |
| 135 | + .span_builder("test-span") |
| 136 | + .with_kind(SpanKind::Client) |
| 137 | + .start(tracer); |
| 138 | + black_box(span); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + c.bench_function("SpanBuilder_WithAttributes", |b| { |
| 143 | + b.iter(|| { |
| 144 | + let span = tracer |
| 145 | + .span_builder("test-span") |
| 146 | + .with_kind(SpanKind::Client) |
| 147 | + .with_attributes(vec![ |
| 148 | + KeyValue::new("key1", false), |
| 149 | + KeyValue::new("key2", "hello"), |
| 150 | + KeyValue::new("key3", 123), |
| 151 | + KeyValue::new("key4", 123.456), |
| 152 | + ]) |
| 153 | + .start(tracer); |
| 154 | + black_box(span); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + c.bench_function("SpanBuilder_WithLinks", |b| { |
| 159 | + let link_context = valid_span_context(); |
| 160 | + b.iter(|| { |
| 161 | + let span = tracer |
| 162 | + .span_builder("test-span") |
| 163 | + .with_links(vec![opentelemetry::trace::Link::new( |
| 164 | + link_context.clone(), |
| 165 | + vec![ |
| 166 | + KeyValue::new("key1", "value1"), |
| 167 | + KeyValue::new("key2", "value2"), |
| 168 | + ], |
| 169 | + 0, |
| 170 | + )]) |
| 171 | + .start(tracer); |
| 172 | + black_box(span); |
| 173 | + }); |
| 174 | + }); |
| 175 | +} |
| 176 | + |
| 177 | +criterion_group! { |
| 178 | + name = benches; |
| 179 | + config = Criterion::default() |
| 180 | + .warm_up_time(std::time::Duration::from_secs(1)) |
| 181 | + .measurement_time(std::time::Duration::from_secs(2)); |
| 182 | + targets = criterion_benchmark |
| 183 | +} |
| 184 | +criterion_main!(benches); |
0 commit comments