Skip to content

Add focused benchmark for metric hotpath #1389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
8 changes: 8 additions & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ harness = false
name = "span_builder"
harness = false

[[bench]]
name = "metric_counter"
harness = false

[[bench]]
name = "attribute_set"
harness = false

[[bench]]
name = "trace"
harness = false
Expand Down
40 changes: 40 additions & 0 deletions opentelemetry-sdk/benches/attribute_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use criterion::{criterion_group, criterion_main, Criterion};
use opentelemetry::KeyValue;
use opentelemetry_sdk::AttributeSet;

// Run this benchmark with:
// cargo bench --bench metric_counter

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

fn attribute_set(c: &mut Criterion) {
c.bench_function("AttributeSet_without_duplicates", |b| {
b.iter(|| {
let attributes: &[KeyValue] = &[
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute2", "value2"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
];
let _attribute_set: AttributeSet = attributes.into();
});
});

c.bench_function("AttributeSet_with_duplicates", |b| {
b.iter(|| {
let attributes: &[KeyValue] = &[
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
];
let _attribute_set: AttributeSet = attributes.into();
});
});
}

criterion_group!(benches, criterion_benchmark);

criterion_main!(benches);
74 changes: 74 additions & 0 deletions opentelemetry-sdk/benches/metric_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use criterion::{criterion_group, criterion_main, Criterion};
use opentelemetry::{
metrics::{Counter, MeterProvider as _},
KeyValue,
};
use opentelemetry_sdk::metrics::{ManualReader, SdkMeterProvider};
use rand::{rngs::SmallRng, Rng, SeedableRng};

// Run this benchmark with:
// cargo bench --bench metric_counter --features=metrics,testing
fn create_counter() -> Counter<u64> {
let meter_provider: SdkMeterProvider = SdkMeterProvider::builder()
.with_reader(ManualReader::builder().build())
.build();
let meter = meter_provider.meter("benchmarks");
let counter = meter.u64_counter("counter_bench").init();
counter
}

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

fn counter_add(c: &mut Criterion) {
let attribute_values = [
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10",
];

let counter = create_counter();
c.bench_function("Counter_Add_Sorted", |b| {
b.iter(|| {
let mut rng = SmallRng::from_entropy();
// 4*4*10*10 = 1600 time series.
let index_first_attribute = rng.gen_range(0..4);
let index_second_attribute = rng.gen_range(0..4);
let index_third_attribute = rng.gen_range(0..10);
let index_forth_attribute = rng.gen_range(0..10);
counter.add(
1,
&[
KeyValue::new("attribute1", attribute_values[index_first_attribute]),
KeyValue::new("attribute2", attribute_values[index_second_attribute]),
KeyValue::new("attribute3", attribute_values[index_third_attribute]),
KeyValue::new("attribute4", attribute_values[index_forth_attribute]),
],
);
});
});

c.bench_function("Counter_Add_Unsorted", |b| {
b.iter(|| {
let mut rng = SmallRng::from_entropy();
// 4*4*10*10 = 1600 time series.
let index_first_attribute = rng.gen_range(0..4);
let index_second_attribute = rng.gen_range(0..4);
let index_third_attribute = rng.gen_range(0..10);
let index_forth_attribute = rng.gen_range(0..10);
counter.add(
1,
&[
KeyValue::new("attribute2", attribute_values[index_second_attribute]),
KeyValue::new("attribute3", attribute_values[index_third_attribute]),
KeyValue::new("attribute1", attribute_values[index_first_attribute]),
KeyValue::new("attribute4", attribute_values[index_forth_attribute]),
],
);
});
});
}

criterion_group!(benches, criterion_benchmark);

criterion_main!(benches);