-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
appender: add initial set of benches (#2128)
* appender: add initial set of benches This patch adds blocking and nonblocking benchmarks. This code is from an old PR (#703) that was never merged, and now ported to TOT so that it compiles. Co-authored-by: Zeki Sherif <9832640+zekisherif@users.noreply.github.com> * switch to no-op writers in benchmarks * fix macro resolution issue Co-authored-by: Zeki Sherif <9832640+zekisherif@users.noreply.github.com> Co-authored-by: David Barsky <me@davidbarsky.com>
- Loading branch information
Showing
3 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use std::{ | ||
thread::{self, JoinHandle}, | ||
time::Instant, | ||
}; | ||
use tracing::{event, Level}; | ||
use tracing_appender::non_blocking; | ||
use tracing_subscriber::fmt::MakeWriter; | ||
|
||
// a no-op writer is used in order to measure the overhead incurred by | ||
// tracing-subscriber. | ||
#[derive(Clone)] | ||
struct NoOpWriter; | ||
|
||
impl NoOpWriter { | ||
fn new() -> NoOpWriter { | ||
NoOpWriter | ||
} | ||
} | ||
|
||
impl<'a> MakeWriter<'a> for NoOpWriter { | ||
type Writer = NoOpWriter; | ||
|
||
fn make_writer(&self) -> Self::Writer { | ||
self.clone() | ||
} | ||
} | ||
|
||
impl std::io::Write for NoOpWriter { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
Ok(buf.len()) | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn synchronous_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("synchronous"); | ||
group.bench_function("single_thread", |b| { | ||
let subscriber = tracing_subscriber::fmt().with_writer(NoOpWriter::new()); | ||
tracing::subscriber::with_default(subscriber.finish(), || { | ||
b.iter(|| event!(Level::INFO, "event")) | ||
}); | ||
}); | ||
|
||
group.bench_function("multiple_writers", |b| { | ||
b.iter_custom(|iters| { | ||
let mut handles: Vec<JoinHandle<()>> = Vec::new(); | ||
|
||
let start = Instant::now(); | ||
|
||
let make_writer = NoOpWriter::new(); | ||
let cloned_make_writer = make_writer.clone(); | ||
|
||
handles.push(thread::spawn(move || { | ||
let subscriber = tracing_subscriber::fmt().with_writer(make_writer); | ||
tracing::subscriber::with_default(subscriber.finish(), || { | ||
for _ in 0..iters { | ||
event!(Level::INFO, "event"); | ||
} | ||
}); | ||
})); | ||
|
||
handles.push(thread::spawn(move || { | ||
let subscriber = tracing_subscriber::fmt().with_writer(cloned_make_writer); | ||
tracing::subscriber::with_default(subscriber.finish(), || { | ||
for _ in 0..iters { | ||
event!(Level::INFO, "event"); | ||
} | ||
}); | ||
})); | ||
|
||
for handle in handles { | ||
let _ = handle.join(); | ||
} | ||
|
||
start.elapsed() | ||
}); | ||
}); | ||
} | ||
|
||
fn non_blocking_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("non_blocking"); | ||
|
||
group.bench_function("single_thread", |b| { | ||
let (non_blocking, _guard) = non_blocking(NoOpWriter::new()); | ||
let subscriber = tracing_subscriber::fmt().with_writer(non_blocking); | ||
|
||
tracing::subscriber::with_default(subscriber.finish(), || { | ||
b.iter(|| event!(Level::INFO, "event")) | ||
}); | ||
}); | ||
|
||
group.bench_function("multiple_writers", |b| { | ||
b.iter_custom(|iters| { | ||
let (non_blocking, _guard) = non_blocking(NoOpWriter::new()); | ||
|
||
let mut handles: Vec<JoinHandle<()>> = Vec::new(); | ||
|
||
let start = Instant::now(); | ||
|
||
let cloned_make_writer = non_blocking.clone(); | ||
|
||
handles.push(thread::spawn(move || { | ||
let subscriber = tracing_subscriber::fmt().with_writer(non_blocking); | ||
tracing::subscriber::with_default(subscriber.finish(), || { | ||
for _ in 0..iters { | ||
event!(Level::INFO, "event"); | ||
} | ||
}); | ||
})); | ||
|
||
handles.push(thread::spawn(move || { | ||
let subscriber = tracing_subscriber::fmt().with_writer(cloned_make_writer); | ||
tracing::subscriber::with_default(subscriber.finish(), || { | ||
for _ in 0..iters { | ||
event!(Level::INFO, "event"); | ||
} | ||
}); | ||
})); | ||
|
||
for handle in handles { | ||
let _ = handle.join(); | ||
} | ||
|
||
start.elapsed() | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, synchronous_benchmark, non_blocking_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters