diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/tracing/benches | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tracing/benches')
-rw-r--r-- | vendor/tracing/benches/global_subscriber.rs | 136 | ||||
-rw-r--r-- | vendor/tracing/benches/no_subscriber.rs | 101 | ||||
-rw-r--r-- | vendor/tracing/benches/subscriber.rs | 189 |
3 files changed, 426 insertions, 0 deletions
diff --git a/vendor/tracing/benches/global_subscriber.rs b/vendor/tracing/benches/global_subscriber.rs new file mode 100644 index 000000000..83519610a --- /dev/null +++ b/vendor/tracing/benches/global_subscriber.rs @@ -0,0 +1,136 @@ +use std::fmt::Write; + +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tracing::Level; + +use tracing::{span, Event, Id, Metadata}; + +/// A subscriber that is enabled but otherwise does nothing. +struct EnabledSubscriber; + +impl tracing::Subscriber for EnabledSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let _ = span; + Id::from_u64(0xDEAD_FACE) + } + + fn event(&self, event: &Event<'_>) { + let _ = event; + } + + fn record(&self, span: &Id, values: &span::Record<'_>) { + let _ = (span, values); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +const NOP_LOGGER: NopLogger = NopLogger; + +struct NopLogger; + +impl log::Log for NopLogger { + fn enabled(&self, _metadata: &log::Metadata) -> bool { + true + } + + fn log(&self, record: &log::Record) { + if self.enabled(record.metadata()) { + let mut this = self; + let _ = write!(this, "{}", record.args()); + } + } + + fn flush(&self) {} +} + +impl Write for &NopLogger { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + black_box(s); + Ok(()) + } +} + +const N_SPANS: usize = 100; + +fn criterion_benchmark(c: &mut Criterion) { + let mut c = c.benchmark_group("global/subscriber"); + let _ = tracing::subscriber::set_global_default(EnabledSubscriber); + let _ = log::set_logger(&NOP_LOGGER); + log::set_max_level(log::LevelFilter::Trace); + c.bench_function("span_no_fields", |b| b.iter(|| span!(Level::TRACE, "span"))); + + c.bench_function("event", |b| { + b.iter(|| { + tracing::event!(Level::TRACE, "hello"); + }) + }); + + c.bench_function("enter_span", |b| { + let span = span!(Level::TRACE, "span"); + #[allow(clippy::unit_arg)] + b.iter(|| black_box(span.in_scope(|| {}))) + }); + + c.bench_function("span_repeatedly", |b| { + #[inline] + fn mk_span(i: u64) -> tracing::Span { + span!(Level::TRACE, "span", i = i) + } + + let n = black_box(N_SPANS); + b.iter(|| (0..n).fold(mk_span(0), |_, i| mk_span(i as u64))) + }); + + c.bench_function("span_with_fields", |b| { + b.iter(|| { + span!( + Level::TRACE, + "span", + foo = "foo", + bar = "bar", + baz = 3, + quuux = tracing::field::debug(0.99) + ) + }); + }); +} + +fn bench_dispatch(c: &mut Criterion) { + let mut group = c.benchmark_group("global/dispatch"); + let _ = tracing::subscriber::set_global_default(EnabledSubscriber); + let _ = log::set_logger(&NOP_LOGGER); + log::set_max_level(log::LevelFilter::Trace); + group.bench_function("get_ref", |b| { + b.iter(|| { + tracing::dispatcher::get_default(|current| { + black_box(¤t); + }) + }) + }); + group.bench_function("get_clone", |b| { + b.iter(|| { + let current = tracing::dispatcher::get_default(|current| current.clone()); + black_box(current); + }) + }); + group.finish(); +} + +criterion_group!(benches, criterion_benchmark, bench_dispatch); +criterion_main!(benches); diff --git a/vendor/tracing/benches/no_subscriber.rs b/vendor/tracing/benches/no_subscriber.rs new file mode 100644 index 000000000..e0f82b56a --- /dev/null +++ b/vendor/tracing/benches/no_subscriber.rs @@ -0,0 +1,101 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tracing::Level; + +struct FakeEmptySpan { + inner: Option<(usize, std::sync::Arc<()>)>, + meta: Option<&'static ()>, +} + +impl FakeEmptySpan { + fn new() -> Self { + Self { + inner: None, + meta: None, + } + } +} + +impl Drop for FakeEmptySpan { + fn drop(&mut self) { + black_box(&self.inner); + black_box(&self.meta); + } +} + +fn bench_no_subscriber(c: &mut Criterion) { + use std::sync::atomic::{AtomicUsize, Ordering}; + + let mut group = c.benchmark_group("no_subscriber"); + + group.bench_function("span", |b| { + b.iter(|| { + let span = tracing::span!(Level::TRACE, "span"); + black_box(&span); + }) + }); + group.bench_function("span_enter", |b| { + b.iter(|| { + let span = tracing::span!(Level::TRACE, "span"); + let _e = span.enter(); + }) + }); + group.bench_function("empty_span", |b| { + b.iter(|| { + let span = tracing::span::Span::none(); + black_box(&span); + }); + }); + group.bench_function("empty_struct", |b| { + b.iter(|| { + let span = FakeEmptySpan::new(); + black_box(&span); + }) + }); + group.bench_function("event", |b| { + b.iter(|| { + tracing::event!(Level::TRACE, "hello"); + }) + }); + group.bench_function("relaxed_load", |b| { + let foo = AtomicUsize::new(1); + b.iter(|| black_box(foo.load(Ordering::Relaxed))); + }); + group.bench_function("acquire_load", |b| { + let foo = AtomicUsize::new(1); + b.iter(|| black_box(foo.load(Ordering::Acquire))) + }); + group.bench_function("log", |b| { + b.iter(|| { + log::log!(log::Level::Info, "log"); + }) + }); + group.finish(); +} + +fn bench_fields(c: &mut Criterion) { + let mut group = c.benchmark_group("no_subscriber_field"); + group.bench_function("span", |b| { + b.iter(|| { + black_box(tracing::span!( + Level::TRACE, + "span", + foo = tracing::field::display(format!("bar {:?}", 2)) + )); + }) + }); + group.bench_function("event", |b| { + b.iter(|| { + tracing::event!( + Level::TRACE, + foo = tracing::field::display(format!("bar {:?}", 2)) + ); + }) + }); + group.bench_function("log", |b| { + b.iter(|| log::log!(log::Level::Trace, "{}", format!("bar {:?}", 2))) + }); + group.finish(); +} + +criterion_group!(benches, bench_no_subscriber, bench_fields); +criterion_main!(benches); diff --git a/vendor/tracing/benches/subscriber.rs b/vendor/tracing/benches/subscriber.rs new file mode 100644 index 000000000..c6418010f --- /dev/null +++ b/vendor/tracing/benches/subscriber.rs @@ -0,0 +1,189 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use tracing::Level; + +use std::{ + fmt, + sync::{Mutex, MutexGuard}, +}; +use tracing::{field, span, Event, Id, Metadata}; + +/// A subscriber that is enabled but otherwise does nothing. +struct EnabledSubscriber; + +impl tracing::Subscriber for EnabledSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let _ = span; + Id::from_u64(0xDEAD_FACE) + } + + fn event(&self, event: &Event<'_>) { + let _ = event; + } + + fn record(&self, span: &Id, values: &span::Record<'_>) { + let _ = (span, values); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +/// Simulates a subscriber that records span data. +struct VisitingSubscriber(Mutex<String>); + +struct Visitor<'a>(MutexGuard<'a, String>); + +impl<'a> field::Visit for Visitor<'a> { + fn record_debug(&mut self, _field: &field::Field, value: &dyn fmt::Debug) { + use std::fmt::Write; + let _ = write!(&mut *self.0, "{:?}", value); + } +} + +impl tracing::Subscriber for VisitingSubscriber { + fn new_span(&self, span: &span::Attributes<'_>) -> Id { + let mut visitor = Visitor(self.0.lock().unwrap()); + span.record(&mut visitor); + Id::from_u64(0xDEAD_FACE) + } + + fn record(&self, _span: &Id, values: &span::Record<'_>) { + let mut visitor = Visitor(self.0.lock().unwrap()); + values.record(&mut visitor); + } + + fn event(&self, event: &Event<'_>) { + let mut visitor = Visitor(self.0.lock().unwrap()); + event.record(&mut visitor); + } + + fn record_follows_from(&self, span: &Id, follows: &Id) { + let _ = (span, follows); + } + + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + let _ = metadata; + true + } + + fn enter(&self, span: &Id) { + let _ = span; + } + + fn exit(&self, span: &Id) { + let _ = span; + } +} + +const N_SPANS: usize = 100; + +fn criterion_benchmark(c: &mut Criterion) { + c.bench_function("span_no_fields", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| span!(Level::TRACE, "span")) + }); + }); + + c.bench_function("enter_span", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + let span = span!(Level::TRACE, "span"); + #[allow(clippy::unit_arg)] + b.iter(|| black_box(span.in_scope(|| {}))) + }); + }); + + c.bench_function("span_repeatedly", |b| { + #[inline] + fn mk_span(i: u64) -> tracing::Span { + span!(Level::TRACE, "span", i = i) + } + + let n = black_box(N_SPANS); + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| (0..n).fold(mk_span(0), |_, i| mk_span(i as u64))) + }); + }); + + c.bench_function("span_with_fields", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + span!( + Level::TRACE, + "span", + foo = "foo", + bar = "bar", + baz = 3, + quuux = tracing::field::debug(0.99) + ) + }) + }); + }); + + c.bench_function("span_with_fields_record", |b| { + let subscriber = VisitingSubscriber(Mutex::new(String::from(""))); + tracing::subscriber::with_default(subscriber, || { + b.iter(|| { + span!( + Level::TRACE, + "span", + foo = "foo", + bar = "bar", + baz = 3, + quuux = tracing::field::debug(0.99) + ) + }) + }); + }); +} + +fn bench_dispatch(c: &mut Criterion) { + let mut group = c.benchmark_group("dispatch"); + group.bench_function("no_dispatch_get_ref", |b| { + b.iter(|| { + tracing::dispatcher::get_default(|current| { + black_box(¤t); + }) + }) + }); + group.bench_function("no_dispatch_get_clone", |b| { + b.iter(|| { + let current = tracing::dispatcher::get_default(|current| current.clone()); + black_box(current); + }) + }); + group.bench_function("get_ref", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + tracing::dispatcher::get_default(|current| { + black_box(¤t); + }) + }) + }) + }); + group.bench_function("get_clone", |b| { + tracing::subscriber::with_default(EnabledSubscriber, || { + b.iter(|| { + let current = tracing::dispatcher::get_default(|current| current.clone()); + black_box(current); + }) + }) + }); + group.finish(); +} + +criterion_group!(benches, criterion_benchmark, bench_dispatch); +criterion_main!(benches); |