Skip to content

Commit 5628f66

Browse files
authored
Exclude benchmark setup duration using iter_batched (#2233)
1 parent a18853e commit 5628f66

File tree

1 file changed

+61
-51
lines changed

1 file changed

+61
-51
lines changed

opentelemetry-sdk/benches/metrics_counter.rs

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| ThreadLocal_Random_Generator_5 | 37 ns |
1313
*/
1414

15-
use criterion::{criterion_group, criterion_main, Criterion};
15+
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
1616
use opentelemetry::{
1717
metrics::{Counter, MeterProvider as _},
1818
KeyValue,
@@ -57,62 +57,72 @@ fn criterion_benchmark(c: &mut Criterion) {
5757
fn counter_add_sorted(c: &mut Criterion) {
5858
let counter = create_counter("Counter_Add_Sorted");
5959
c.bench_function("Counter_Add_Sorted", |b| {
60-
b.iter(|| {
61-
// 4*4*10*10 = 1600 time series.
62-
let rands = CURRENT_RNG.with(|rng| {
63-
let mut rng = rng.borrow_mut();
64-
[
65-
rng.gen_range(0..4),
66-
rng.gen_range(0..4),
67-
rng.gen_range(0..10),
68-
rng.gen_range(0..10),
69-
]
70-
});
71-
let index_first_attribute = rands[0];
72-
let index_second_attribute = rands[1];
73-
let index_third_attribute = rands[2];
74-
let index_fourth_attribute = rands[3];
75-
counter.add(
76-
1,
77-
&[
78-
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
79-
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
80-
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
81-
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
82-
],
83-
);
84-
});
60+
b.iter_batched(
61+
|| {
62+
// 4*4*10*10 = 1600 time series.
63+
CURRENT_RNG.with(|rng| {
64+
let mut rng = rng.borrow_mut();
65+
[
66+
rng.gen_range(0..4),
67+
rng.gen_range(0..4),
68+
rng.gen_range(0..10),
69+
rng.gen_range(0..10),
70+
]
71+
})
72+
},
73+
|rands| {
74+
let index_first_attribute = rands[0];
75+
let index_second_attribute = rands[1];
76+
let index_third_attribute = rands[2];
77+
let index_fourth_attribute = rands[3];
78+
counter.add(
79+
1,
80+
&[
81+
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
82+
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
83+
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
84+
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
85+
],
86+
);
87+
},
88+
BatchSize::SmallInput,
89+
);
8590
});
8691
}
8792

8893
fn counter_add_unsorted(c: &mut Criterion) {
8994
let counter = create_counter("Counter_Add_Unsorted");
9095
c.bench_function("Counter_Add_Unsorted", |b| {
91-
b.iter(|| {
92-
// 4*4*10*10 = 1600 time series.
93-
let rands = CURRENT_RNG.with(|rng| {
94-
let mut rng = rng.borrow_mut();
95-
[
96-
rng.gen_range(0..4),
97-
rng.gen_range(0..4),
98-
rng.gen_range(0..10),
99-
rng.gen_range(0..10),
100-
]
101-
});
102-
let index_first_attribute = rands[0];
103-
let index_second_attribute = rands[1];
104-
let index_third_attribute = rands[2];
105-
let index_fourth_attribute = rands[3];
106-
counter.add(
107-
1,
108-
&[
109-
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
110-
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
111-
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
112-
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
113-
],
114-
);
115-
});
96+
b.iter_batched(
97+
|| {
98+
// 4*4*10*10 = 1600 time series.
99+
CURRENT_RNG.with(|rng| {
100+
let mut rng = rng.borrow_mut();
101+
[
102+
rng.gen_range(0..4),
103+
rng.gen_range(0..4),
104+
rng.gen_range(0..10),
105+
rng.gen_range(0..10),
106+
]
107+
})
108+
},
109+
|rands| {
110+
let index_first_attribute = rands[0];
111+
let index_second_attribute = rands[1];
112+
let index_third_attribute = rands[2];
113+
let index_fourth_attribute = rands[3];
114+
counter.add(
115+
1,
116+
&[
117+
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
118+
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
119+
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
120+
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
121+
],
122+
);
123+
},
124+
BatchSize::SmallInput,
125+
);
116126
});
117127
}
118128

0 commit comments

Comments
 (0)