Skip to content

Commit e4eaa40

Browse files
committed
noop-metrics-benchmark
1 parent 7bdbc47 commit e4eaa40

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

opentelemetry-sdk/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ name = "metric_counter"
6868
harness = false
6969
required-features = ["metrics"]
7070

71+
[[bench]]
72+
name = "noop_metrics"
73+
harness = false
74+
required-features = ["metrics"]
75+
7176
[[bench]]
7277
name = "attribute_set"
7378
harness = false
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use opentelemetry::{
3+
metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _},
4+
KeyValue,
5+
};
6+
7+
use rand::{rngs::SmallRng, Rng, SeedableRng};
8+
9+
fn create_counter() -> Counter<u64> {
10+
let meter_provider: NoopMeterProvider = NoopMeterProvider::default();
11+
let meter = meter_provider.meter("benchmarks");
12+
let counter = meter.u64_counter("counter_bench").init();
13+
counter
14+
}
15+
16+
fn criterion_benchmark(c: &mut Criterion) {
17+
noop_counter_add(c);
18+
create_keyvalue_vector(c);
19+
}
20+
21+
fn noop_counter_add(c: &mut Criterion) {
22+
let attribute_values = [
23+
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
24+
"value10",
25+
];
26+
27+
let mut rng = SmallRng::from_entropy();
28+
let num_samples = 1000; // Arbitrary number of samples for the benchmark
29+
let random_indices: Vec<(usize, usize, usize, usize)> = (0..num_samples)
30+
.map(|_| {
31+
(
32+
rng.gen_range(0..4),
33+
rng.gen_range(0..4),
34+
rng.gen_range(0..10),
35+
rng.gen_range(0..10),
36+
)
37+
})
38+
.collect();
39+
40+
let noop_counter = create_counter();
41+
c.bench_function("Noop_Counter", |b| {
42+
// Use an iterator to cycle through the pre-generated indices.
43+
// This ensures that the benchmark does not exhaust the indices and each iteration gets a "random" set.
44+
let mut indices_iter = random_indices.iter().cycle();
45+
b.iter(|| {
46+
let (
47+
index_first_attribute,
48+
index_second_attribute,
49+
index_third_attribute,
50+
index_forth_attribute,
51+
) = indices_iter.next().unwrap();
52+
noop_counter.add(
53+
1,
54+
&[
55+
KeyValue::new("attribute1", attribute_values[*index_first_attribute]),
56+
KeyValue::new("attribute2", attribute_values[*index_second_attribute]),
57+
KeyValue::new("attribute3", attribute_values[*index_third_attribute]),
58+
KeyValue::new("attribute4", attribute_values[*index_forth_attribute]),
59+
],
60+
);
61+
});
62+
});
63+
64+
c.bench_function("Create_KeyValue", |b| {
65+
let mut indices_iter = random_indices.iter().cycle();
66+
b.iter(|| {
67+
let (
68+
index_first_attribute,
69+
index_second_attribute,
70+
index_third_attribute,
71+
index_forth_attribute,
72+
) = indices_iter.next().unwrap();
73+
let _ = vec![
74+
KeyValue::new("attribute1", attribute_values[*index_first_attribute]),
75+
KeyValue::new("attribute2", attribute_values[*index_second_attribute]),
76+
KeyValue::new("attribute3", attribute_values[*index_third_attribute]),
77+
KeyValue::new("attribute4", attribute_values[*index_forth_attribute]),
78+
];
79+
});
80+
});
81+
}
82+
83+
criterion_group!(benches, criterion_benchmark);
84+
85+
criterion_main!(benches);

0 commit comments

Comments
 (0)