Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 74 additions & 9 deletions opentelemetry-sdk/benches/metrics_counter.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/*
The benchmark results:
criterion = "0.5.1"
rustc 1.82.0 (f6e511eec 2024-10-15)
OS: Ubuntu 22.04.3 LTS (5.15.167.4-microsoft-standard-WSL2)
Hardware: AMD EPYC 7763 64-Core Processor - 2.44 GHz, 16vCPUs,
rustc 1.83.0 (90b35a623 2024-11-26)
OS: Ubuntu 22.04.4 LTS (5.15.167.4-microsoft-standard-WSL2)
Hardware: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz 2.79 GHz
RAM: 64.0 GB
| Test | Average time|
|--------------------------------|-------------|
| Counter_Add_Sorted | 172 ns |
| Counter_Add_Unsorted | 183 ns |
| Counter_Overflow | 562 ns |
| ThreadLocal_Random_Generator_5 | 37 ns |
| Test | Average time|
|-------------------------------------------------------|-------------|
| Counter_Add_Sorted | 160 ns |
| Counter_Add_Unsorted | 164 ns |
| Counter_Add_Sorted_With_Non_Static_Values | 238 ns |
| Counter_Overflow | 562 ns |
| ThreadLocal_Random_Generator_5 | 37 ns |
*/

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
Expand Down Expand Up @@ -51,6 +52,22 @@ fn create_counter(name: &'static str) -> Counter<u64> {
fn criterion_benchmark(c: &mut Criterion) {
counter_add_sorted(c);
counter_add_unsorted(c);

let attribute_values = [
"value1".to_owned(),
"value2".to_owned(),
"value3".to_owned(),
"value4".to_owned(),
"value5".to_owned(),
"value6".to_owned(),
"value7".to_owned(),
"value8".to_owned(),
"value9".to_owned(),
"value10".to_owned(),
];

counter_add_sorted_with_non_static_values(c, attribute_values);

counter_overflow(c);
random_generator(c);
}
Expand Down Expand Up @@ -127,6 +144,54 @@ fn counter_add_unsorted(c: &mut Criterion) {
});
}

fn counter_add_sorted_with_non_static_values(c: &mut Criterion, attribute_values: [String; 10]) {
let counter = create_counter("Counter_Add_Sorted_With_Non_Static_Values");
c.bench_function("Counter_Add_Sorted_With_Non_Static_Values", |b| {
b.iter_batched(
|| {
// 4*4*10*10 = 1600 time series.
CURRENT_RNG.with(|rng| {
let mut rng = rng.borrow_mut();
[
rng.gen_range(0..4),
rng.gen_range(0..4),
rng.gen_range(0..10),
rng.gen_range(0..10),
]
})
},
|rands| {
let index_first_attribute = rands[0];
let index_second_attribute = rands[1];
let index_third_attribute = rands[2];
let index_fourth_attribute = rands[3];
counter.add(
1,
&[
KeyValue::new(
"attribute1",
attribute_values[index_first_attribute].as_str().to_owned(),
Copy link
Contributor Author

@utpilla utpilla Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #2089, we would be able to simply pass a string slice and avoid the string allocation:

Suggested change
attribute_values[index_first_attribute].as_str().to_owned(),
attribute_values[index_first_attribute].as_str(),

),
KeyValue::new(
"attribute2",
attribute_values[index_second_attribute].as_str().to_owned(),
),
KeyValue::new(
"attribute3",
attribute_values[index_third_attribute].as_str().to_owned(),
),
KeyValue::new(
"attribute4",
attribute_values[index_fourth_attribute].as_str().to_owned(),
),
],
);
},
BatchSize::SmallInput,
);
});
}

fn counter_overflow(c: &mut Criterion) {
let counter = create_counter("Counter_Overflow");
// Cause overflow.
Expand Down
130 changes: 99 additions & 31 deletions opentelemetry-sdk/benches/metrics_histogram.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/*
The benchmark results:
criterion = "0.5.1"
rustc 1.82.0 (f6e511eec 2024-10-15)
rustc 1.83.0 (90b35a623 2024-11-26)
OS: Ubuntu 22.04.4 LTS (5.15.167.4-microsoft-standard-WSL2)
Hardware: AMD EPYC 7763 64-Core Processor - 2.44 GHz, 16vCPUs,
Hardware: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz 2.79 GHz
RAM: 64.0 GB
| Test | Average time|
|--------------------------------|-------------|
| Histogram_Record | 225.04 ns |
| Test | Average time|
|-------------------------------------------------------|-------------|
| Histogram_Record | 186.24 ns |
| Histogram_Record_With_Non_Static_Values | 264.70 ns |

*/

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use opentelemetry::{
metrics::{Histogram, MeterProvider as _},
KeyValue,
Expand Down Expand Up @@ -48,36 +49,103 @@ fn create_histogram(name: &'static str) -> Histogram<u64> {

fn criterion_benchmark(c: &mut Criterion) {
histogram_record(c);

let attribute_values = [
"value1".to_owned(),
"value2".to_owned(),
"value3".to_owned(),
"value4".to_owned(),
"value5".to_owned(),
"value6".to_owned(),
"value7".to_owned(),
"value8".to_owned(),
"value9".to_owned(),
"value10".to_owned(),
];
histogram_record_with_non_static_values(c, attribute_values);
}

fn histogram_record(c: &mut Criterion) {
let histogram = create_histogram("Histogram_Record");
c.bench_function("Histogram_Record", |b| {
b.iter(|| {
// 4*4*10*10 = 1600 time series.
let rands = CURRENT_RNG.with(|rng| {
let mut rng = rng.borrow_mut();
[
rng.gen_range(0..4),
rng.gen_range(0..4),
rng.gen_range(0..10),
rng.gen_range(0..10),
]
});
let index_first_attribute = rands[0];
let index_second_attribute = rands[1];
let index_third_attribute = rands[2];
let index_fourth_attribute = rands[3];
histogram.record(
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_fourth_attribute]),
],
);
});
b.iter_batched(
|| {
// 4*4*10*10 = 1600 time series.
CURRENT_RNG.with(|rng| {
let mut rng = rng.borrow_mut();
[
rng.gen_range(0..4),
rng.gen_range(0..4),
rng.gen_range(0..10),
rng.gen_range(0..10),
]
})
},
|rands| {
let index_first_attribute = rands[0];
let index_second_attribute = rands[1];
let index_third_attribute = rands[2];
let index_fourth_attribute = rands[3];
histogram.record(
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_fourth_attribute]),
],
);
},
BatchSize::SmallInput,
);
});
}

fn histogram_record_with_non_static_values(c: &mut Criterion, attribute_values: [String; 10]) {
let histogram = create_histogram("Histogram_Record_With_Non_Static_Values");
c.bench_function("Histogram_Record_With_Non_Static_Values", |b| {
b.iter_batched(
|| {
// 4*4*10*10 = 1600 time series.
CURRENT_RNG.with(|rng| {
let mut rng = rng.borrow_mut();
[
rng.gen_range(0..4),
rng.gen_range(0..4),
rng.gen_range(0..10),
rng.gen_range(0..10),
]
})
},
|rands| {
let index_first_attribute = rands[0];
let index_second_attribute = rands[1];
let index_third_attribute = rands[2];
let index_fourth_attribute = rands[3];
histogram.record(
1,
&[
KeyValue::new(
"attribute1",
attribute_values[index_first_attribute].as_str().to_owned(),
),
KeyValue::new(
"attribute2",
attribute_values[index_second_attribute].as_str().to_owned(),
),
KeyValue::new(
"attribute3",
attribute_values[index_third_attribute].as_str().to_owned(),
),
KeyValue::new(
"attribute4",
attribute_values[index_fourth_attribute].as_str().to_owned(),
),
],
);
},
BatchSize::SmallInput,
);
});
}

Expand Down
Loading