Skip to content

Commit 76b40ba

Browse files
authored
Metric benchmark - more isolation (#1991)
1 parent 75eb96d commit 76b40ba

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

opentelemetry-sdk/benches/metric_counter.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
RAM: 64.0 GB
77
| Test | Average time|
88
|--------------------------------|-------------|
9-
| Counter_Add_Sorted | 586 ns |
10-
| Counter_Add_Unsorted | 592 ns |
11-
| Counter_Overflow | 600 ns |
9+
| Counter_Add_Sorted | 560 ns |
10+
| Counter_Add_Unsorted | 565 ns |
11+
| Counter_Overflow | 568 ns |
1212
| ThreadLocal_Random_Generator_5 | 37 ns |
1313
*/
1414

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

33+
static ATTRIBUTE_VALUES: [&str; 10] = [
34+
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
35+
"value10",
36+
];
37+
3338
// Run this benchmark with:
3439
// cargo bench --bench metric_counter
3540
fn create_counter(name: &'static str) -> Counter<u64> {
@@ -38,21 +43,20 @@ fn create_counter(name: &'static str) -> Counter<u64> {
3843
.build();
3944
let meter = meter_provider.meter("benchmarks");
4045

46+
println!("Counter_Created");
4147
meter.u64_counter(name).init()
4248
}
4349

4450
fn criterion_benchmark(c: &mut Criterion) {
45-
counter_add(c);
51+
counter_add_sorted(c);
52+
counter_add_unsorted(c);
53+
counter_overflow(c);
54+
random_generator(c);
4655
}
4756

48-
fn counter_add(c: &mut Criterion) {
49-
let attribute_values = [
50-
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
51-
"value10",
52-
];
53-
57+
fn counter_add_sorted(c: &mut Criterion) {
58+
let counter = create_counter("Counter_Add_Sorted");
5459
c.bench_function("Counter_Add_Sorted", |b| {
55-
let counter = create_counter("Counter_Add_Sorted");
5660
b.iter(|| {
5761
// 4*4*10*10 = 1600 time series.
5862
let rands = CURRENT_RNG.with(|rng| {
@@ -71,17 +75,19 @@ fn counter_add(c: &mut Criterion) {
7175
counter.add(
7276
1,
7377
&[
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+
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]),
7882
],
7983
);
8084
});
8185
});
86+
}
8287

88+
fn counter_add_unsorted(c: &mut Criterion) {
89+
let counter = create_counter("Counter_Add_Unsorted");
8390
c.bench_function("Counter_Add_Unsorted", |b| {
84-
let counter = create_counter("Counter_Add_Unsorted");
8591
b.iter(|| {
8692
// 4*4*10*10 = 1600 time series.
8793
let rands = CURRENT_RNG.with(|rng| {
@@ -100,22 +106,24 @@ fn counter_add(c: &mut Criterion) {
100106
counter.add(
101107
1,
102108
&[
103-
KeyValue::new("attribute2", attribute_values[index_second_attribute]),
104-
KeyValue::new("attribute3", attribute_values[index_third_attribute]),
105-
KeyValue::new("attribute1", attribute_values[index_first_attribute]),
106-
KeyValue::new("attribute4", attribute_values[index_fourth_attribute]),
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]),
107113
],
108114
);
109115
});
110116
});
117+
}
111118

112-
c.bench_function("Counter_Overflow", |b| {
113-
let counter = create_counter("Counter_Overflow");
114-
// Cause overflow.
115-
for v in 0..2001 {
116-
counter.add(100, &[KeyValue::new("A", v.to_string())]);
117-
}
119+
fn counter_overflow(c: &mut Criterion) {
120+
let counter = create_counter("Counter_Overflow");
121+
// Cause overflow.
122+
for v in 0..2001 {
123+
counter.add(100, &[KeyValue::new("A", v.to_string())]);
124+
}
118125

126+
c.bench_function("Counter_Overflow", |b| {
119127
b.iter(|| {
120128
// 4*4*10*10 = 1600 time series.
121129
let rands = CURRENT_RNG.with(|rng| {
@@ -134,15 +142,17 @@ fn counter_add(c: &mut Criterion) {
134142
counter.add(
135143
1,
136144
&[
137-
KeyValue::new("attribute1", attribute_values[index_first_attribute]),
138-
KeyValue::new("attribute2", attribute_values[index_second_attribute]),
139-
KeyValue::new("attribute3", attribute_values[index_third_attribute]),
140-
KeyValue::new("attribute4", attribute_values[index_fourth_attribute]),
145+
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
146+
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
147+
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
148+
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
141149
],
142150
);
143151
});
144152
});
153+
}
145154

155+
fn random_generator(c: &mut Criterion) {
146156
c.bench_function("ThreadLocal_Random_Generator_5", |b| {
147157
b.iter(|| {
148158
let __i1 = CURRENT_RNG.with(|rng| {

0 commit comments

Comments
 (0)