Skip to content

Commit 6cc327d

Browse files
authored
Update metrics benchmarks (#2502)
1 parent e05979d commit 6cc327d

File tree

2 files changed

+160
-40
lines changed

2 files changed

+160
-40
lines changed

opentelemetry-sdk/benches/metrics_counter.rs

+67-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/*
22
The benchmark results:
33
criterion = "0.5.1"
4-
rustc 1.82.0 (f6e511eec 2024-10-15)
5-
OS: Ubuntu 22.04.3 LTS (5.15.167.4-microsoft-standard-WSL2)
6-
Hardware: AMD EPYC 7763 64-Core Processor - 2.44 GHz, 16vCPUs,
4+
rustc 1.83.0 (90b35a623 2024-11-26)
5+
OS: Ubuntu 22.04.4 LTS (5.15.167.4-microsoft-standard-WSL2)
6+
Hardware: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz 2.79 GHz
77
RAM: 64.0 GB
8-
| Test | Average time|
9-
|--------------------------------|-------------|
10-
| Counter_Add_Sorted | 172 ns |
11-
| Counter_Add_Unsorted | 183 ns |
12-
| Counter_Overflow | 562 ns |
13-
| ThreadLocal_Random_Generator_5 | 37 ns |
8+
| Test | Average time|
9+
|-------------------------------------------------------|-------------|
10+
| Counter_Add_Sorted | 160 ns |
11+
| Counter_Add_Unsorted | 164 ns |
12+
| Counter_Add_Sorted_With_Non_Static_Values | 238 ns |
13+
| Counter_Overflow | 562 ns |
14+
| ThreadLocal_Random_Generator_5 | 37 ns |
1415
*/
1516

1617
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
@@ -51,6 +52,15 @@ fn create_counter(name: &'static str) -> Counter<u64> {
5152
fn criterion_benchmark(c: &mut Criterion) {
5253
counter_add_sorted(c);
5354
counter_add_unsorted(c);
55+
56+
let attribute_values: [String; 10] = (1..=10)
57+
.map(|i| format!("value{}", i))
58+
.collect::<Vec<String>>()
59+
.try_into()
60+
.expect("Expected a Vec of length 10");
61+
62+
counter_add_sorted_with_non_static_values(c, attribute_values);
63+
5464
counter_overflow(c);
5565
random_generator(c);
5666
}
@@ -127,6 +137,54 @@ fn counter_add_unsorted(c: &mut Criterion) {
127137
});
128138
}
129139

140+
fn counter_add_sorted_with_non_static_values(c: &mut Criterion, attribute_values: [String; 10]) {
141+
let counter = create_counter("Counter_Add_Sorted_With_Non_Static_Values");
142+
c.bench_function("Counter_Add_Sorted_With_Non_Static_Values", |b| {
143+
b.iter_batched(
144+
|| {
145+
// 4*4*10*10 = 1600 time series.
146+
CURRENT_RNG.with(|rng| {
147+
let mut rng = rng.borrow_mut();
148+
[
149+
rng.gen_range(0..4),
150+
rng.gen_range(0..4),
151+
rng.gen_range(0..10),
152+
rng.gen_range(0..10),
153+
]
154+
})
155+
},
156+
|rands| {
157+
let index_first_attribute = rands[0];
158+
let index_second_attribute = rands[1];
159+
let index_third_attribute = rands[2];
160+
let index_fourth_attribute = rands[3];
161+
counter.add(
162+
1,
163+
&[
164+
KeyValue::new(
165+
"attribute1",
166+
attribute_values[index_first_attribute].as_str().to_owned(),
167+
),
168+
KeyValue::new(
169+
"attribute2",
170+
attribute_values[index_second_attribute].as_str().to_owned(),
171+
),
172+
KeyValue::new(
173+
"attribute3",
174+
attribute_values[index_third_attribute].as_str().to_owned(),
175+
),
176+
KeyValue::new(
177+
"attribute4",
178+
attribute_values[index_fourth_attribute].as_str().to_owned(),
179+
),
180+
],
181+
);
182+
},
183+
BatchSize::SmallInput,
184+
);
185+
});
186+
}
187+
130188
fn counter_overflow(c: &mut Criterion) {
131189
let counter = create_counter("Counter_Overflow");
132190
// Cause overflow.

opentelemetry-sdk/benches/metrics_histogram.rs

+93-31
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/*
22
The benchmark results:
33
criterion = "0.5.1"
4-
rustc 1.82.0 (f6e511eec 2024-10-15)
4+
rustc 1.83.0 (90b35a623 2024-11-26)
55
OS: Ubuntu 22.04.4 LTS (5.15.167.4-microsoft-standard-WSL2)
6-
Hardware: AMD EPYC 7763 64-Core Processor - 2.44 GHz, 16vCPUs,
6+
Hardware: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz 2.79 GHz
77
RAM: 64.0 GB
8-
| Test | Average time|
9-
|--------------------------------|-------------|
10-
| Histogram_Record | 225.04 ns |
8+
| Test | Average time|
9+
|-------------------------------------------------------|-------------|
10+
| Histogram_Record | 186.24 ns |
11+
| Histogram_Record_With_Non_Static_Values | 264.70 ns |
1112
1213
*/
1314

14-
use criterion::{criterion_group, criterion_main, Criterion};
15+
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
1516
use opentelemetry::{
1617
metrics::{Histogram, MeterProvider as _},
1718
KeyValue,
@@ -48,36 +49,97 @@ fn create_histogram(name: &'static str) -> Histogram<u64> {
4849

4950
fn criterion_benchmark(c: &mut Criterion) {
5051
histogram_record(c);
52+
53+
let attribute_values: [String; 10] = (1..=10)
54+
.map(|i| format!("value{}", i))
55+
.collect::<Vec<String>>()
56+
.try_into()
57+
.expect("Expected a Vec of length 10");
58+
59+
histogram_record_with_non_static_values(c, attribute_values);
5160
}
5261

5362
fn histogram_record(c: &mut Criterion) {
5463
let histogram = create_histogram("Histogram_Record");
5564
c.bench_function("Histogram_Record", |b| {
56-
b.iter(|| {
57-
// 4*4*10*10 = 1600 time series.
58-
let rands = CURRENT_RNG.with(|rng| {
59-
let mut rng = rng.borrow_mut();
60-
[
61-
rng.gen_range(0..4),
62-
rng.gen_range(0..4),
63-
rng.gen_range(0..10),
64-
rng.gen_range(0..10),
65-
]
66-
});
67-
let index_first_attribute = rands[0];
68-
let index_second_attribute = rands[1];
69-
let index_third_attribute = rands[2];
70-
let index_fourth_attribute = rands[3];
71-
histogram.record(
72-
1,
73-
&[
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_fourth_attribute]),
78-
],
79-
);
80-
});
65+
b.iter_batched(
66+
|| {
67+
// 4*4*10*10 = 1600 time series.
68+
CURRENT_RNG.with(|rng| {
69+
let mut rng = rng.borrow_mut();
70+
[
71+
rng.gen_range(0..4),
72+
rng.gen_range(0..4),
73+
rng.gen_range(0..10),
74+
rng.gen_range(0..10),
75+
]
76+
})
77+
},
78+
|rands| {
79+
let index_first_attribute = rands[0];
80+
let index_second_attribute = rands[1];
81+
let index_third_attribute = rands[2];
82+
let index_fourth_attribute = rands[3];
83+
histogram.record(
84+
1,
85+
&[
86+
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
87+
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
88+
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
89+
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
90+
],
91+
);
92+
},
93+
BatchSize::SmallInput,
94+
);
95+
});
96+
}
97+
98+
fn histogram_record_with_non_static_values(c: &mut Criterion, attribute_values: [String; 10]) {
99+
let histogram = create_histogram("Histogram_Record_With_Non_Static_Values");
100+
c.bench_function("Histogram_Record_With_Non_Static_Values", |b| {
101+
b.iter_batched(
102+
|| {
103+
// 4*4*10*10 = 1600 time series.
104+
CURRENT_RNG.with(|rng| {
105+
let mut rng = rng.borrow_mut();
106+
[
107+
rng.gen_range(0..4),
108+
rng.gen_range(0..4),
109+
rng.gen_range(0..10),
110+
rng.gen_range(0..10),
111+
]
112+
})
113+
},
114+
|rands| {
115+
let index_first_attribute = rands[0];
116+
let index_second_attribute = rands[1];
117+
let index_third_attribute = rands[2];
118+
let index_fourth_attribute = rands[3];
119+
histogram.record(
120+
1,
121+
&[
122+
KeyValue::new(
123+
"attribute1",
124+
attribute_values[index_first_attribute].as_str().to_owned(),
125+
),
126+
KeyValue::new(
127+
"attribute2",
128+
attribute_values[index_second_attribute].as_str().to_owned(),
129+
),
130+
KeyValue::new(
131+
"attribute3",
132+
attribute_values[index_third_attribute].as_str().to_owned(),
133+
),
134+
KeyValue::new(
135+
"attribute4",
136+
attribute_values[index_fourth_attribute].as_str().to_owned(),
137+
),
138+
],
139+
);
140+
},
141+
BatchSize::SmallInput,
142+
);
81143
});
82144
}
83145

0 commit comments

Comments
 (0)