Skip to content

Commit 04c863e

Browse files
authored
Add focused benchmark for metric hotpath (#1389)
1 parent 3229979 commit 04c863e

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

opentelemetry-sdk/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ harness = false
6161
name = "span_builder"
6262
harness = false
6363

64+
[[bench]]
65+
name = "metric_counter"
66+
harness = false
67+
68+
[[bench]]
69+
name = "attribute_set"
70+
harness = false
71+
6472
[[bench]]
6573
name = "trace"
6674
harness = false
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use opentelemetry::KeyValue;
3+
use opentelemetry_sdk::AttributeSet;
4+
5+
// Run this benchmark with:
6+
// cargo bench --bench metric_counter
7+
8+
fn criterion_benchmark(c: &mut Criterion) {
9+
attribute_set(c);
10+
}
11+
12+
fn attribute_set(c: &mut Criterion) {
13+
c.bench_function("AttributeSet_without_duplicates", |b| {
14+
b.iter(|| {
15+
let attributes: &[KeyValue] = &[
16+
KeyValue::new("attribute1", "value1"),
17+
KeyValue::new("attribute2", "value2"),
18+
KeyValue::new("attribute3", "value3"),
19+
KeyValue::new("attribute4", "value4"),
20+
];
21+
let _attribute_set: AttributeSet = attributes.into();
22+
});
23+
});
24+
25+
c.bench_function("AttributeSet_with_duplicates", |b| {
26+
b.iter(|| {
27+
let attributes: &[KeyValue] = &[
28+
KeyValue::new("attribute1", "value1"),
29+
KeyValue::new("attribute3", "value3"),
30+
KeyValue::new("attribute3", "value3"),
31+
KeyValue::new("attribute4", "value4"),
32+
];
33+
let _attribute_set: AttributeSet = attributes.into();
34+
});
35+
});
36+
}
37+
38+
criterion_group!(benches, criterion_benchmark);
39+
40+
criterion_main!(benches);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use opentelemetry::{
3+
metrics::{Counter, MeterProvider as _},
4+
KeyValue,
5+
};
6+
use opentelemetry_sdk::metrics::{ManualReader, SdkMeterProvider};
7+
use rand::{rngs::SmallRng, Rng, SeedableRng};
8+
9+
// Run this benchmark with:
10+
// cargo bench --bench metric_counter --features=metrics,testing
11+
fn create_counter() -> Counter<u64> {
12+
let meter_provider: SdkMeterProvider = SdkMeterProvider::builder()
13+
.with_reader(ManualReader::builder().build())
14+
.build();
15+
let meter = meter_provider.meter("benchmarks");
16+
let counter = meter.u64_counter("counter_bench").init();
17+
counter
18+
}
19+
20+
fn criterion_benchmark(c: &mut Criterion) {
21+
counter_add(c);
22+
}
23+
24+
fn counter_add(c: &mut Criterion) {
25+
let attribute_values = [
26+
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
27+
"value10",
28+
];
29+
30+
let counter = create_counter();
31+
c.bench_function("Counter_Add_Sorted", |b| {
32+
b.iter(|| {
33+
let mut rng = SmallRng::from_entropy();
34+
// 4*4*10*10 = 1600 time series.
35+
let index_first_attribute = rng.gen_range(0..4);
36+
let index_second_attribute = rng.gen_range(0..4);
37+
let index_third_attribute = rng.gen_range(0..10);
38+
let index_forth_attribute = rng.gen_range(0..10);
39+
counter.add(
40+
1,
41+
&[
42+
KeyValue::new("attribute1", attribute_values[index_first_attribute]),
43+
KeyValue::new("attribute2", attribute_values[index_second_attribute]),
44+
KeyValue::new("attribute3", attribute_values[index_third_attribute]),
45+
KeyValue::new("attribute4", attribute_values[index_forth_attribute]),
46+
],
47+
);
48+
});
49+
});
50+
51+
c.bench_function("Counter_Add_Unsorted", |b| {
52+
b.iter(|| {
53+
let mut rng = SmallRng::from_entropy();
54+
// 4*4*10*10 = 1600 time series.
55+
let index_first_attribute = rng.gen_range(0..4);
56+
let index_second_attribute = rng.gen_range(0..4);
57+
let index_third_attribute = rng.gen_range(0..10);
58+
let index_forth_attribute = rng.gen_range(0..10);
59+
counter.add(
60+
1,
61+
&[
62+
KeyValue::new("attribute2", attribute_values[index_second_attribute]),
63+
KeyValue::new("attribute3", attribute_values[index_third_attribute]),
64+
KeyValue::new("attribute1", attribute_values[index_first_attribute]),
65+
KeyValue::new("attribute4", attribute_values[index_forth_attribute]),
66+
],
67+
);
68+
});
69+
});
70+
}
71+
72+
criterion_group!(benches, criterion_benchmark);
73+
74+
criterion_main!(benches);

0 commit comments

Comments
 (0)