Skip to content
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

subscriber: add more benchmarks for filters #581

Merged
merged 5 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ rustdoc-args = ["--cfg", "docsrs"]
name = "filter"
harness = false

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

[[bench]]
name = "fmt"
harness = false
185 changes: 107 additions & 78 deletions tracing-subscriber/benches/filter.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use criterion::{criterion_group, criterion_main, Criterion};
use std::{
sync::{Arc, Barrier},
thread,
time::{Duration, Instant},
};
use std::time::Duration;
use tracing::{dispatcher::Dispatch, span, Event, Id, Metadata};
use tracing_subscriber::{prelude::*, EnvFilter};

mod support;
use support::MultithreadedBench;

/// A subscriber that is enabled but otherwise does nothing.
struct EnabledSubscriber;

Expand Down Expand Up @@ -42,42 +41,6 @@ impl tracing::Subscriber for EnabledSubscriber {
}
}

#[derive(Clone)]
struct MultithreadedBench {
start: Arc<Barrier>,
end: Arc<Barrier>,
dispatch: Dispatch,
}

impl MultithreadedBench {
fn new(dispatch: Dispatch) -> Self {
Self {
start: Arc::new(Barrier::new(5)),
end: Arc::new(Barrier::new(5)),
dispatch,
}
}

fn thread(&self, f: impl FnOnce(&Barrier) + Send + 'static) -> &Self {
let this = self.clone();
thread::spawn(move || {
let dispatch = this.dispatch.clone();
tracing::dispatcher::with_default(&dispatch, move || {
f(&*this.start);
this.end.wait();
})
});
self
}

fn run(&self) -> Duration {
self.start.wait();
let t0 = Instant::now();
self.end.wait();
t0.elapsed()
}
}

fn bench_static(c: &mut Criterion) {
let mut group = c.benchmark_group("static");

Expand All @@ -104,27 +67,81 @@ fn bench_static(c: &mut Criterion) {
})
});
});
group.bench_function("enabled_one", |b| {
let filter = "static_filter=info"
.parse::<EnvFilter>()
.expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::info!(target: "static_filter", "hi");
})
});
});
group.bench_function("enabled_many", |b| {
let filter = "foo=debug,bar=trace,baz=error,quux=warn,static_filter=info"
.parse::<EnvFilter>()
.expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::info!(target: "static_filter", "hi");
})
});
});
group.bench_function("disabled_level_one", |b| {
let filter = "static_filter=info"
.parse::<EnvFilter>()
.expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::debug!(target: "static_filter", "hi");
})
});
});
group.bench_function("disabled_level_many", |b| {
let filter = "foo=debug,bar=info,baz=error,quux=warn,static_filter=info"
.parse::<EnvFilter>()
.expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::trace!(target: "static_filter", "hi");
})
});
});
group.bench_function("disabled_one", |b| {
let filter = "foo=info".parse::<EnvFilter>().expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::info!(target: "static_filter", "hi");
})
});
});
group.bench_function("disabled_many", |b| {
let filter = "foo=debug,bar=trace,baz=error,quux=warn,whibble=info"
.parse::<EnvFilter>()
.expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::info!(target: "static_filter", "hi");
})
});
});
group.bench_function("baseline_multithreaded", |b| {
let dispatch = tracing::dispatcher::Dispatch::new(EnabledSubscriber);
let dispatch = Dispatch::new(EnabledSubscriber);
b.iter_custom(|iters| {
let mut total = Duration::from_secs(0);
for _ in 0..iters {
let bench = MultithreadedBench::new(dispatch.clone());
let elapsed = bench
.thread(|start| {
start.wait();
.thread(|| {
tracing::info!(target: "static_filter", "hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::debug!(target: "static_filter", "hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::warn!(target: "static_filter", "hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::warn!(target: "foo", "hi");
})
.run();
Expand All @@ -137,26 +154,22 @@ fn bench_static(c: &mut Criterion) {
let filter = "static_filter=info"
.parse::<EnvFilter>()
.expect("should parse");
let dispatch = tracing::dispatcher::Dispatch::new(EnabledSubscriber.with(filter));
let dispatch = Dispatch::new(EnabledSubscriber.with(filter));
b.iter_custom(|iters| {
let mut total = Duration::from_secs(0);
for _ in 0..iters {
let bench = MultithreadedBench::new(dispatch.clone());
let elapsed = bench
.thread(|start| {
start.wait();
.thread(|| {
tracing::info!(target: "static_filter", "hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::debug!(target: "static_filter", "hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::warn!(target: "static_filter", "hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::warn!(target: "foo", "hi");
})
.run();
Expand Down Expand Up @@ -201,32 +214,28 @@ fn bench_dynamic(c: &mut Criterion) {
});
});
group.bench_function("baseline_multithreaded", |b| {
let dispatch = tracing::dispatcher::Dispatch::new(EnabledSubscriber);
let dispatch = Dispatch::new(EnabledSubscriber);
b.iter_custom(|iters| {
let mut total = Duration::from_secs(0);
for _ in 0..iters {
let bench = MultithreadedBench::new(dispatch.clone());
let elapsed = bench
.thread(|start| {
.thread(|| {
let span = tracing::info_span!("foo");
start.wait();
let _ = span.enter();
tracing::info!("hi");
})
.thread(|start| {
.thread(|| {
let span = tracing::info_span!("foo");
start.wait();
let _ = span.enter();
tracing::debug!("hi");
})
.thread(|start| {
.thread(|| {
let span = tracing::info_span!("bar");
start.wait();
let _ = span.enter();
tracing::debug!("hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::trace!("hi");
})
.run();
Expand All @@ -237,32 +246,28 @@ fn bench_dynamic(c: &mut Criterion) {
});
group.bench_function("multithreaded", |b| {
let filter = "[foo]=trace".parse::<EnvFilter>().expect("should parse");
let dispatch = tracing::dispatcher::Dispatch::new(EnabledSubscriber.with(filter));
let dispatch = Dispatch::new(EnabledSubscriber.with(filter));
b.iter_custom(|iters| {
let mut total = Duration::from_secs(0);
for _ in 0..iters {
let bench = MultithreadedBench::new(dispatch.clone());
let elapsed = bench
.thread(|start| {
.thread(|| {
let span = tracing::info_span!("foo");
start.wait();
let _ = span.enter();
tracing::info!("hi");
})
.thread(|start| {
.thread(|| {
let span = tracing::info_span!("foo");
start.wait();
let _ = span.enter();
tracing::debug!("hi");
})
.thread(|start| {
.thread(|| {
let span = tracing::info_span!("bar");
start.wait();
let _ = span.enter();
tracing::debug!("hi");
})
.thread(|start| {
start.wait();
.thread(|| {
tracing::trace!("hi");
})
.run();
Expand All @@ -275,5 +280,29 @@ fn bench_dynamic(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, bench_static, bench_dynamic);
fn bench_mixed(c: &mut Criterion) {
let mut group = c.benchmark_group("mixed");
group.bench_function("disabled", |b| {
let filter = "[foo]=trace,bar[quux]=debug,[{baz}]=debug,asdf=warn,wibble=info"
.parse::<EnvFilter>()
.expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::info!(target: "static_filter", "hi");
})
});
});
group.bench_function("disabled_by_level", |b| {
let filter = "[foo]=info,bar[quux]=debug,asdf=warn,static_filter=info"
.parse::<EnvFilter>()
.expect("should parse");
tracing::subscriber::with_default(EnabledSubscriber.with(filter), || {
b.iter(|| {
tracing::trace!(target: "static_filter", "hi");
})
});
});
}

criterion_group!(benches, bench_static, bench_dynamic, bench_mixed);
criterion_main!(benches);
Loading