add bench for tracing layer + fix record_bool bug

This commit is contained in:
Toby Lawrence 2020-10-28 20:22:37 -04:00
parent 540fcfd25b
commit 6faae7718f
5 changed files with 70 additions and 28 deletions

View File

@ -22,6 +22,10 @@ bench = false
name = "visit"
harness = false
[[bench]]
name = "layer"
harness = false
[dependencies]
metrics = { version = "0.13.0-alpha.1", path = "../metrics", features = ["std"] }
metrics-util = { version = "0.4.0-alpha.1", path = "../metrics-util" }

View File

@ -0,0 +1,46 @@
use criterion::{criterion_group, criterion_main, Benchmark, Criterion};
use metrics::{Key, KeyData, Label, NoopRecorder, Recorder};
use metrics_tracing_context::{MetricsLayer, TracingContextLayer};
use metrics_util::layers::Layer;
use tracing::{
dispatcher::{with_default, Dispatch},
span, Level,
};
use tracing_subscriber::{layer::SubscriberExt, Registry};
fn layer_benchmark(c: &mut Criterion) {
c.bench(
"layer",
Benchmark::new("all/enhance_key", |b| {
let subscriber = Registry::default().with(MetricsLayer::new());
let dispatcher = Dispatch::new(subscriber);
with_default(&dispatcher, || {
let user = "ferris";
let email = "ferris@rust-lang.org";
let span = span!(Level::TRACE, "login", user, user.email = email);
let _guard = span.enter();
let tracing_layer = TracingContextLayer::all();
let recorder = tracing_layer.layer(NoopRecorder);
static LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")];
static KEY_DATA: KeyData = KeyData::from_static_parts("key", &LABELS);
b.iter(|| {
recorder.increment_counter(Key::Borrowed(&KEY_DATA), 1);
})
})
})
.with_function("noop recorder overhead (increment_counter)", |b| {
let recorder = NoopRecorder;
static LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")];
static KEY_DATA: KeyData = KeyData::from_static_parts("key", &LABELS);
b.iter(|| {
recorder.increment_counter(Key::Borrowed(&KEY_DATA), 1);
})
}),
);
}
criterion_group!(benches, layer_benchmark);
criterion_main!(benches);

View File

@ -20,7 +20,7 @@ impl Visit for Labels {
}
fn record_bool(&mut self, field: &Field, value: bool) {
let label = Label::new(field.name(), if value { "true" } else { "false " });
let label = Label::new(field.name(), if value { "true" } else { "false" });
self.0.push(label);
}

View File

@ -75,7 +75,7 @@ pub struct Snapshotter {
registry: Arc<Registry<DifferentiatedKey, Handle>>,
metrics: Option<Arc<Mutex<IndexMap<DifferentiatedKey, ()>>>>,
units: UnitMap,
descriptions: DescriptionMap,
descs: DescriptionMap,
}
impl Snapshotter {
@ -115,27 +115,15 @@ impl Snapshotter {
metrics.clone()
};
for (dkey, _) in metrics.into_iter() {
if let Some(handle) = handles.get(&dkey) {
collect_metric(
dkey,
handle,
&self.units,
&self.descriptions,
&mut snapshot,
);
for (dk, _) in metrics.into_iter() {
if let Some(h) = handles.get(&dk) {
collect_metric(dk, h, &self.units, &self.descs, &mut snapshot);
}
}
}
None => {
for (dkey, handle) in handles.into_iter() {
collect_metric(
dkey,
&handle,
&self.units,
&self.descriptions,
&mut snapshot,
);
for (dk, h) in handles.into_iter() {
collect_metric(dk, &h, &self.units, &self.descs, &mut snapshot);
}
}
}
@ -152,7 +140,7 @@ pub struct DebuggingRecorder {
registry: Arc<Registry<DifferentiatedKey, Handle>>,
metrics: Option<Arc<Mutex<IndexMap<DifferentiatedKey, ()>>>>,
units: Arc<Mutex<HashMap<DifferentiatedKey, Unit>>>,
descriptions: Arc<Mutex<HashMap<DifferentiatedKey, &'static str>>>,
descs: Arc<Mutex<HashMap<DifferentiatedKey, &'static str>>>,
}
impl DebuggingRecorder {
@ -177,7 +165,7 @@ impl DebuggingRecorder {
registry: Arc::new(Registry::new()),
metrics,
units: Arc::new(Mutex::new(HashMap::new())),
descriptions: Arc::new(Mutex::new(HashMap::new())),
descs: Arc::new(Mutex::new(HashMap::new())),
}
}
@ -187,7 +175,7 @@ impl DebuggingRecorder {
registry: self.registry.clone(),
metrics: self.metrics.clone(),
units: self.units.clone(),
descriptions: self.descriptions.clone(),
descs: self.descs.clone(),
}
}
@ -202,17 +190,17 @@ impl DebuggingRecorder {
&self,
rkey: DifferentiatedKey,
unit: Option<Unit>,
description: Option<&'static str>,
desc: Option<&'static str>,
) {
if let Some(unit) = unit {
let mut units = self.units.lock().expect("units lock poisoned");
let uentry = units.entry(rkey.clone()).or_insert_with(|| unit.clone());
*uentry = unit;
}
if let Some(description) = description {
let mut descriptions = self.descriptions.lock().expect("description lock poisoned");
let dentry = descriptions.entry(rkey).or_insert_with(|| description);
*dentry = description;
if let Some(desc) = desc {
let mut descs = self.descs.lock().expect("description lock poisoned");
let dentry = descs.entry(rkey).or_insert_with(|| desc);
*dentry = desc;
}
}

View File

@ -51,7 +51,11 @@ pub trait Recorder {
fn record_histogram(&self, key: Key, value: u64);
}
struct NoopRecorder;
/// A no-op recorder.
///
/// Used as the default recorder when one has not been installed yet. Useful for acting as the root
/// recorder when testing layers.
pub struct NoopRecorder;
impl Recorder for NoopRecorder {
fn register_counter(&self, _key: Key, _unit: Option<Unit>, _description: Option<&'static str>) {