1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#![allow(dead_code)]
use criterion::{black_box, measurement::WallTime, Bencher};
use tracing::{field, span, Event, Id, Metadata};
use std::{
fmt::{self, Write},
sync::{Mutex, MutexGuard},
};
pub fn for_all_recording(
group: &mut criterion::BenchmarkGroup<'_, WallTime>,
mut iter: impl FnMut(&mut Bencher<'_, WallTime>),
) {
// first, run benchmarks with no subscriber
group.bench_function("none", &mut iter);
// then, run benchmarks with a scoped default subscriber
tracing::subscriber::with_default(EnabledSubscriber, || {
group.bench_function("scoped", &mut iter)
});
let subscriber = VisitingSubscriber(Mutex::new(String::from("")));
tracing::subscriber::with_default(subscriber, || {
group.bench_function("scoped_recording", &mut iter);
});
// finally, set a global default subscriber, and run the benchmarks again.
tracing::subscriber::set_global_default(EnabledSubscriber)
.expect("global default should not have already been set!");
let _ = log::set_logger(&NOP_LOGGER);
log::set_max_level(log::LevelFilter::Trace);
group.bench_function("global", &mut iter);
}
pub fn for_all_dispatches(
group: &mut criterion::BenchmarkGroup<'_, WallTime>,
mut iter: impl FnMut(&mut Bencher<'_, WallTime>),
) {
// first, run benchmarks with no subscriber
group.bench_function("none", &mut iter);
// then, run benchmarks with a scoped default subscriber
tracing::subscriber::with_default(EnabledSubscriber, || {
group.bench_function("scoped", &mut iter)
});
// finally, set a global default subscriber, and run the benchmarks again.
tracing::subscriber::set_global_default(EnabledSubscriber)
.expect("global default should not have already been set!");
let _ = log::set_logger(&NOP_LOGGER);
log::set_max_level(log::LevelFilter::Trace);
group.bench_function("global", &mut iter);
}
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(())
}
}
/// 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) {
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;
}
}
/// 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;
}
}
|