Skip to content

Commit

Permalink
Adding noop metrics benchmark (#1607)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Mar 11, 2024
1 parent 1904d34 commit e816cb9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
6 changes: 6 additions & 0 deletions opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ otel_unstable = []

[dev-dependencies]
opentelemetry_sdk = { path = "../opentelemetry-sdk" } # for documentation tests
criterion = { version = "0.4", features = ["html_reports"] }

[[bench]]
name = "noop_metrics"
harness = false
required-features = ["metrics"]
94 changes: 94 additions & 0 deletions opentelemetry/benches/noop_metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use criterion::{criterion_group, criterion_main, Criterion};
use opentelemetry::{
metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _},
KeyValue,
};

fn create_counter() -> Counter<u64> {
let meter_provider: NoopMeterProvider = NoopMeterProvider::default();
let meter = meter_provider.meter("benchmarks");
let counter = meter.u64_counter("counter_bench").init();
counter
}

fn criterion_benchmark(c: &mut Criterion) {
noop_counter_add(c);
}

fn noop_counter_add(c: &mut Criterion) {
let noop_counter = create_counter();

c.bench_function("NoopCounter_NoAttributes", |b| {
b.iter(|| {
noop_counter.add(1, &[]);
});
});

c.bench_function("NoopCounter_AddWithInlineStaticAttributes", |b| {
b.iter(|| {
noop_counter.add(
1,
&[
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute2", "value2"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
],
);
});
});

c.bench_function("NoopCounter_AddWithStaticArray", |b| {
b.iter(|| {
let kv = [
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute2", "value2"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
];

noop_counter.add(1, &kv);
});
});

c.bench_function("NoopCounter_AddWithDynamicAttributes", |b| {
b.iter(|| {
let kv = vec![
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute2", "value2"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
];

noop_counter.add(1, &kv);
});
});

#[allow(clippy::useless_vec)]
c.bench_function("CreateVector_KeyValue", |b| {
b.iter(|| {
let _v1 = vec![
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute2", "value2"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
];
});
});

#[allow(clippy::useless_vec)]
c.bench_function("CreateDynamicVector_StringPair", |b| {
b.iter(|| {
let _v1 = vec![
("attribute1", "value1"),
("attribute2", "value2"),
("attribute3", "value3"),
("attribute4", "value4"),
];
});
});
}

criterion_group!(benches, criterion_benchmark);

criterion_main!(benches);

0 comments on commit e816cb9

Please sign in to comment.