diff --git a/Cargo.toml b/Cargo.toml index fe6a65db..f4b12590 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,10 @@ hyper = { version = "0.14.16", features = ["server", "http1", "tcp"] } [build-dependencies] prost-build = { version = "0.11.0", optional = true } +[[bench]] +name = "baseline" +harness = false + [[bench]] name = "family" harness = false @@ -54,4 +58,4 @@ required-features = [] name = "proto" path = "benches/encoding/proto.rs" harness = false -required-features = ["protobuf"] +required-features = ["protobuf"] diff --git a/benches/baseline.rs b/benches/baseline.rs new file mode 100644 index 00000000..b948a5d2 --- /dev/null +++ b/benches/baseline.rs @@ -0,0 +1,24 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use prometheus_client::metrics::counter::Counter; +use prometheus_client::metrics::family::Family; + +pub fn baseline(c: &mut Criterion) { + c.bench_function("counter", |b| { + let counter: Counter = Counter::default(); + + b.iter(|| { + counter.inc(); + }) + }); + + c.bench_function("counter via family lookup", |b| { + let family = Family::<(), Counter>::default(); + + b.iter(|| { + family.get_or_create(&()).inc(); + }) + }); +} + +criterion_group!(benches, baseline); +criterion_main!(benches); diff --git a/benches/family.rs b/benches/family.rs index a409cc43..bc08df3c 100644 --- a/benches/family.rs +++ b/benches/family.rs @@ -3,6 +3,32 @@ use prometheus_client::metrics::counter::Counter; use prometheus_client::metrics::family::Family; pub fn family(c: &mut Criterion) { + c.bench_function( + "counter family with [(&'static str, &'static str)] label set", + |b| { + let family = Family::<[(&'static str, &'static str); 2], Counter>::default(); + + b.iter(|| { + family + .get_or_create(&[("method", "GET"), ("status", "200")]) + .inc(); + }) + }, + ); + + c.bench_function( + "counter family with Vec<(&'static str, &'static str)> label set", + |b| { + let family = Family::, Counter>::default(); + + b.iter(|| { + family + .get_or_create(&vec![("method", "GET"), ("status", "200")]) + .inc(); + }) + }, + ); + c.bench_function("counter family with Vec<(String, String)> label set", |b| { let family = Family::, Counter>::default();