Skip to content

Commit

Permalink
Add benchmark for Context
Browse files Browse the repository at this point in the history
  • Loading branch information
shaun-cox committed Jul 4, 2023
1 parent 52a7559 commit 8868393
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ rt-tokio = ["tokio", "tokio-stream"]
rt-tokio-current-thread = ["tokio", "tokio-stream"]
rt-async-std = ["async-std"]

[[bench]]
name = "context"
harness = false

[[bench]]
name = "key_value_map"
harness = false
Expand Down
96 changes: 96 additions & 0 deletions opentelemetry-sdk/benches/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use std::fmt::Display;

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use futures_util::future::BoxFuture;
use opentelemetry_api::{
trace::{TraceContextExt, Tracer, TracerProvider},
Context,
};
use opentelemetry_sdk::{
export::trace::{ExportResult, SpanData, SpanExporter},
trace as sdktrace,
};
use pprof::criterion::{Output, PProfProfiler};

fn criterion_benchmark(c: &mut Criterion) {
benchmark_group(c, BenchmarkParameter::NoActiveSpan);
benchmark_group(c, BenchmarkParameter::WithActiveSpan);
}

fn benchmark_group(c: &mut Criterion, p: BenchmarkParameter) {
let _guard = match p {
BenchmarkParameter::NoActiveSpan => None,
BenchmarkParameter::WithActiveSpan => {
let (provider, tracer) = tracer();
let guard = Context::current_with_span(tracer.start("span")).attach();
Some((guard, provider))
}
};

let mut group = c.benchmark_group("context");

group.bench_function(BenchmarkId::new("baseline current()", p), |b| {
b.iter(|| {
black_box(Context::current());
})
});

group.bench_function(BenchmarkId::new("current().has_active_span()", p), |b| {
b.iter(|| {
black_box(Context::current().has_active_span());
})
});

group.bench_function(
BenchmarkId::new("map_current(|cx| cx.has_active_span().then_some())", p),
|b| {
b.iter(|| {
black_box(Context::map_current(|cx| {
cx.has_active_span().then_some(())
}));
})
},
);

group.finish();
}

#[derive(Copy, Clone)]
enum BenchmarkParameter {
NoActiveSpan,
WithActiveSpan,
}

impl Display for BenchmarkParameter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
BenchmarkParameter::NoActiveSpan => write!(f, "no-active-span"),
BenchmarkParameter::WithActiveSpan => write!(f, "with-active-span"),
}
}
}

fn tracer() -> (sdktrace::TracerProvider, sdktrace::Tracer) {
let provider = sdktrace::TracerProvider::builder()
.with_config(sdktrace::config().with_sampler(sdktrace::Sampler::AlwaysOn))
.with_simple_exporter(NoopExporter)
.build();
let tracer = provider.tracer(module_path!());
(provider, tracer)
}

#[derive(Debug)]
struct NoopExporter;

impl SpanExporter for NoopExporter {
fn export(&mut self, _spans: Vec<SpanData>) -> BoxFuture<'static, ExportResult> {
Box::pin(futures_util::future::ready(Ok(())))
}
}

criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = criterion_benchmark
}
criterion_main!(benches);

0 comments on commit 8868393

Please sign in to comment.