Skip to content

Commit

Permalink
Exclude benchmark setup duration using iter_batched (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
zarvd authored Oct 25, 2024
1 parent a18853e commit 5628f66
Showing 1 changed file with 61 additions and 51 deletions.
112 changes: 61 additions & 51 deletions opentelemetry-sdk/benches/metrics_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
| ThreadLocal_Random_Generator_5 | 37 ns |
*/

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use opentelemetry::{
metrics::{Counter, MeterProvider as _},
KeyValue,
Expand Down Expand Up @@ -57,62 +57,72 @@ fn criterion_benchmark(c: &mut Criterion) {
fn counter_add_sorted(c: &mut Criterion) {
let counter = create_counter("Counter_Add_Sorted");
c.bench_function("Counter_Add_Sorted", |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];
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]),
],
);
});
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]),
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 counter_add_unsorted(c: &mut Criterion) {
let counter = create_counter("Counter_Add_Unsorted");
c.bench_function("Counter_Add_Unsorted", |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];
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]),
],
);
});
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("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]),
],
);
},
BatchSize::SmallInput,
);
});
}

Expand Down

0 comments on commit 5628f66

Please sign in to comment.