Skip to content

WIP - Benchmarks #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ once_cell = "1.10"
opentelemetry-api = { version = "0.1", path = "../opentelemetry-api/" }
percent-encoding = { version = "2.0", optional = true }
pin-project = { version = "1.0.2", optional = true }
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], optional = true }
rand = { version = "0.8", default-features = false, features = ["std", "std_rng","small_rng"], optional = true }
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
thiserror = "1"
tokio = { version = "1.0", default-features = false, features = ["rt", "time"], optional = true }
Expand All @@ -29,7 +29,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[dev-dependencies]
bincode = "1.2"
criterion = "0.3.1"
criterion = { version = "0.3.1", features = ["html_reports"] }
rand_distr = "0.4.0"

[features]
Expand All @@ -44,6 +44,7 @@ rt-async-std = ["async-std"]
[[bench]]
name = "trace"
harness = false
required-features = ["testing"]

[[bench]]
name = "batch_span_processor"
Expand Down
46 changes: 46 additions & 0 deletions opentelemetry-sdk/benches/trace.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use criterion::{criterion_group, criterion_main, Criterion};
use futures_util::future::BoxFuture;
use opentelemetry_api::trace::{SpanId, TraceId};
use rand::{SeedableRng, Rng,rngs::SmallRng};
use std::cell::RefCell;
use opentelemetry_api::{
trace::{Span, Tracer, TracerProvider},
Key, KeyValue,
};
use opentelemetry_sdk::{
export::trace::{ExportResult, SpanData, SpanExporter},
trace::IdGenerator,
trace as sdktrace,
};

Expand Down Expand Up @@ -107,6 +111,30 @@ impl SpanExporter for VoidExporter {
}
}


/// Test [`IdGenerator`] implementation.
///
/// Generates Trace and Span ids using a random number generator using SmallRng
#[derive(Clone, Debug, Default)]
pub struct SmallRandomIdGenerator {
_private: (),
}

impl IdGenerator for SmallRandomIdGenerator {
fn new_trace_id(&self) -> TraceId {
CURRENT_RNG.with(|rng| TraceId::from(rng.borrow_mut().gen::<[u8; 16]>()))
}

fn new_span_id(&self) -> SpanId {
CURRENT_RNG.with(|rng| SpanId::from(rng.borrow_mut().gen::<[u8; 8]>()))
}
}

thread_local! {
/// Store random number generator for each thread
static CURRENT_RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy());
}

fn trace_benchmark_group<F: Fn(&sdktrace::Tracer)>(c: &mut Criterion, name: &str, f: F) {
let mut group = c.benchmark_group(name);

Expand All @@ -129,6 +157,24 @@ fn trace_benchmark_group<F: Fn(&sdktrace::Tracer)>(c: &mut Criterion, name: &str
b.iter(|| f(&never_sample));
});

group.bench_function("always-sample-smallrng", |b| {
let provider = sdktrace::TracerProvider::builder()
.with_config(sdktrace::config().with_sampler(sdktrace::Sampler::AlwaysOn).with_id_generator(SmallRandomIdGenerator::default()))
.with_simple_exporter(VoidExporter)
.build();
let always_sample = provider.tracer("always-sample-smallrng");

b.iter(|| f(&always_sample));
});

group.bench_function("never-sample-smallrng", |b| {
let provider = sdktrace::TracerProvider::builder()
.with_config(sdktrace::config().with_sampler(sdktrace::Sampler::AlwaysOff).with_id_generator(SmallRandomIdGenerator::default()))
.with_simple_exporter(VoidExporter)
.build();
let never_sample = provider.tracer("never-sample-smallrng");
b.iter(|| f(&never_sample));
});
group.finish();
}

Expand Down