Skip to content
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

Metric benchmark - more isolation #1991

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Changes from all 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
70 changes: 40 additions & 30 deletions opentelemetry-sdk/benches/metric_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
RAM: 64.0 GB
| Test | Average time|
|--------------------------------|-------------|
| Counter_Add_Sorted | 586 ns |
| Counter_Add_Unsorted | 592 ns |
| Counter_Overflow | 600 ns |
| Counter_Add_Sorted | 560 ns |
| Counter_Add_Unsorted | 565 ns |
| Counter_Overflow | 568 ns |
| ThreadLocal_Random_Generator_5 | 37 ns |
*/

Expand All @@ -30,6 +30,11 @@ thread_local! {
static CURRENT_RNG: RefCell<rngs::SmallRng> = RefCell::new(rngs::SmallRng::from_entropy());
}

static ATTRIBUTE_VALUES: [&str; 10] = [
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10",
];

// Run this benchmark with:
// cargo bench --bench metric_counter
fn create_counter(name: &'static str) -> Counter<u64> {
Expand All @@ -38,21 +43,20 @@ fn create_counter(name: &'static str) -> Counter<u64> {
.build();
let meter = meter_provider.meter("benchmarks");

println!("Counter_Created");
meter.u64_counter(name).init()
}

fn criterion_benchmark(c: &mut Criterion) {
counter_add(c);
counter_add_sorted(c);
counter_add_unsorted(c);
counter_overflow(c);
random_generator(c);
}

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

fn counter_add_sorted(c: &mut Criterion) {
let counter = create_counter("Counter_Add_Sorted");
c.bench_function("Counter_Add_Sorted", |b| {
let counter = create_counter("Counter_Add_Sorted");
b.iter(|| {
// 4*4*10*10 = 1600 time series.
let rands = CURRENT_RNG.with(|rng| {
Expand All @@ -71,17 +75,19 @@ fn counter_add(c: &mut Criterion) {
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_fourth_attribute]),
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]),
],
);
});
});
}

fn counter_add_unsorted(c: &mut Criterion) {
let counter = create_counter("Counter_Add_Unsorted");
c.bench_function("Counter_Add_Unsorted", |b| {
let counter = create_counter("Counter_Add_Unsorted");
b.iter(|| {
// 4*4*10*10 = 1600 time series.
let rands = CURRENT_RNG.with(|rng| {
Expand All @@ -100,22 +106,24 @@ fn counter_add(c: &mut Criterion) {
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_fourth_attribute]),
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_fourth_attribute]),
],
);
});
});
}

c.bench_function("Counter_Overflow", |b| {
let counter = create_counter("Counter_Overflow");
// Cause overflow.
for v in 0..2001 {
counter.add(100, &[KeyValue::new("A", v.to_string())]);
}
fn counter_overflow(c: &mut Criterion) {
let counter = create_counter("Counter_Overflow");
// Cause overflow.
for v in 0..2001 {
counter.add(100, &[KeyValue::new("A", v.to_string())]);
}

c.bench_function("Counter_Overflow", |b| {
b.iter(|| {
// 4*4*10*10 = 1600 time series.
let rands = CURRENT_RNG.with(|rng| {
Expand All @@ -134,15 +142,17 @@ fn counter_add(c: &mut Criterion) {
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_fourth_attribute]),
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_fourth_attribute]),
],
);
});
});
}

fn random_generator(c: &mut Criterion) {
c.bench_function("ThreadLocal_Random_Generator_5", |b| {
b.iter(|| {
let __i1 = CURRENT_RNG.with(|rng| {
Expand Down
Loading