Skip to content

Commit 6f3892d

Browse files
committed
Fix benchmark
1 parent 1b86d3b commit 6f3892d

File tree

2 files changed

+63
-80
lines changed

2 files changed

+63
-80
lines changed

opentelemetry-sdk/benches/metrics_counter.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
|-------------------------------------------------------|-------------|
1010
| Counter_Add_Sorted | 160 ns |
1111
| Counter_Add_Unsorted | 164 ns |
12-
| Counter_Add_Sorted_With_Non_Static_Values | 439 ns |
12+
| Counter_Add_Sorted_With_Non_Static_Values | 238 ns |
1313
| Counter_Overflow | 562 ns |
1414
| ThreadLocal_Random_Generator_5 | 37 ns |
1515
*/
@@ -52,7 +52,11 @@ fn create_counter(name: &'static str) -> Counter<u64> {
5252
fn criterion_benchmark(c: &mut Criterion) {
5353
counter_add_sorted(c);
5454
counter_add_unsorted(c);
55-
counter_add_sorted_with_non_static_values(c);
55+
56+
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()];
57+
58+
counter_add_sorted_with_non_static_values(c, attribute_values);
59+
5660
counter_overflow(c);
5761
random_generator(c);
5862
}
@@ -129,37 +133,23 @@ fn counter_add_unsorted(c: &mut Criterion) {
129133
});
130134
}
131135

132-
fn counter_add_sorted_with_non_static_values(c: &mut Criterion) {
136+
fn counter_add_sorted_with_non_static_values(c: &mut Criterion, attribute_values: [String; 10]) {
133137
let counter = create_counter("Counter_Add_Sorted_With_Non_Static_Values");
134138
c.bench_function("Counter_Add_Sorted_With_Non_Static_Values", |b| {
135139
b.iter_batched(
136140
|| {
137-
(
141+
// 4*4*10*10 = 1600 time series.
142+
CURRENT_RNG.with(|rng| {
143+
let mut rng = rng.borrow_mut();
138144
[
139-
"value1".to_owned(),
140-
"value2".to_owned(),
141-
"value3".to_owned(),
142-
"value4".to_owned(),
143-
"value5".to_owned(),
144-
"value6".to_owned(),
145-
"value7".to_owned(),
146-
"value8".to_owned(),
147-
"value9".to_owned(),
148-
"value10".to_owned(),
149-
],
150-
// 4*4*10*10 = 1600 time series.
151-
CURRENT_RNG.with(|rng| {
152-
let mut rng = rng.borrow_mut();
153-
[
154-
rng.gen_range(0..4),
155-
rng.gen_range(0..4),
156-
rng.gen_range(0..10),
157-
rng.gen_range(0..10),
158-
]
159-
}),
160-
)
145+
rng.gen_range(0..4),
146+
rng.gen_range(0..4),
147+
rng.gen_range(0..10),
148+
rng.gen_range(0..10),
149+
]
150+
})
161151
},
162-
|(attribute_values, rands)| {
152+
|rands| {
163153
let index_first_attribute = rands[0];
164154
let index_second_attribute = rands[1];
165155
let index_third_attribute = rands[2];

opentelemetry-sdk/benches/metrics_histogram.rs

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
RAM: 64.0 GB
88
| Test | Average time|
99
|-------------------------------------------------------|-------------|
10-
| Histogram_Record | 206.35 ns |
11-
| Histogram_Record_With_Non_Static_Values | 483.58 ns |
10+
| Histogram_Record | 186.24 ns |
11+
| Histogram_Record_With_Non_Static_Values | 264.70 ns |
1212
1313
*/
1414

@@ -49,71 +49,64 @@ fn create_histogram(name: &'static str) -> Histogram<u64> {
4949

5050
fn criterion_benchmark(c: &mut Criterion) {
5151
histogram_record(c);
52-
histogram_record_with_non_static_values(c);
52+
53+
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()];
54+
histogram_record_with_non_static_values(c, attribute_values);
5355
}
5456

5557
fn histogram_record(c: &mut Criterion) {
5658
let histogram = create_histogram("Histogram_Record");
5759
c.bench_function("Histogram_Record", |b| {
58-
b.iter(|| {
59-
// 4*4*10*10 = 1600 time series.
60-
let rands = CURRENT_RNG.with(|rng| {
61-
let mut rng = rng.borrow_mut();
62-
[
63-
rng.gen_range(0..4),
64-
rng.gen_range(0..4),
65-
rng.gen_range(0..10),
66-
rng.gen_range(0..10),
67-
]
68-
});
69-
let index_first_attribute = rands[0];
70-
let index_second_attribute = rands[1];
71-
let index_third_attribute = rands[2];
72-
let index_fourth_attribute = rands[3];
73-
histogram.record(
74-
1,
75-
&[
76-
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
77-
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
78-
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
79-
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
80-
],
81-
);
82-
});
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+
histogram.record(
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+
);
8390
});
8491
}
8592

86-
fn histogram_record_with_non_static_values(c: &mut Criterion) {
93+
fn histogram_record_with_non_static_values(c: &mut Criterion, attribute_values: [String; 10]) {
8794
let histogram = create_histogram("Histogram_Record_With_Non_Static_Values");
8895
c.bench_function("Histogram_Record_With_Non_Static_Values", |b| {
8996
b.iter_batched(
9097
|| {
91-
(
98+
// 4*4*10*10 = 1600 time series.
99+
CURRENT_RNG.with(|rng| {
100+
let mut rng = rng.borrow_mut();
92101
[
93-
"value1".to_owned(),
94-
"value2".to_owned(),
95-
"value3".to_owned(),
96-
"value4".to_owned(),
97-
"value5".to_owned(),
98-
"value6".to_owned(),
99-
"value7".to_owned(),
100-
"value8".to_owned(),
101-
"value9".to_owned(),
102-
"value10".to_owned(),
103-
],
104-
// 4*4*10*10 = 1600 time series.
105-
CURRENT_RNG.with(|rng| {
106-
let mut rng = rng.borrow_mut();
107-
[
108-
rng.gen_range(0..4),
109-
rng.gen_range(0..4),
110-
rng.gen_range(0..10),
111-
rng.gen_range(0..10),
112-
]
113-
}),
114-
)
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+
})
115108
},
116-
|(attribute_values, rands)| {
109+
|rands| {
117110
let index_first_attribute = rands[0];
118111
let index_second_attribute = rands[1];
119112
let index_third_attribute = rands[2];

0 commit comments

Comments
 (0)