Skip to content

Commit 32d73fd

Browse files
committed
Update metric benchmarks
1 parent ad990d6 commit 32d73fd

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

opentelemetry/benches/metrics.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use criterion::{criterion_group, criterion_main, Criterion};
1+
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
22
use opentelemetry::{global, metrics::Counter, KeyValue};
33

44
// Run this benchmark with:
@@ -28,10 +28,10 @@ fn counter_add(c: &mut Criterion) {
2828
counter.add(
2929
1,
3030
&[
31-
KeyValue::new("attribute1", "value1"),
32-
KeyValue::new("attribute2", "value2"),
33-
KeyValue::new("attribute3", "value3"),
34-
KeyValue::new("attribute4", "value4"),
31+
black_box(KeyValue::new("attribute1", "value1")),
32+
black_box(KeyValue::new("attribute2", "value2")),
33+
black_box(KeyValue::new("attribute3", "value3")),
34+
black_box(KeyValue::new("attribute4", "value4")),
3535
],
3636
);
3737
});
@@ -51,16 +51,26 @@ fn counter_add(c: &mut Criterion) {
5151
});
5252

5353
c.bench_function("Counter_AddWithDynamicAttributes", |b| {
54-
b.iter(|| {
55-
let kv = vec![
56-
KeyValue::new("attribute1", "value1"),
57-
KeyValue::new("attribute2", "value2"),
58-
KeyValue::new("attribute3", "value3"),
59-
KeyValue::new("attribute4", "value4"),
60-
];
54+
b.iter_batched(
55+
|| {
56+
let value1 = black_box("a".repeat(6)); // Repeat character six times to match the length of value strings used in other benchmarks
57+
let value2 = black_box("b".repeat(6));
58+
let value3 = black_box("c".repeat(6));
59+
let value4 = black_box("d".repeat(6));
6160

62-
counter.add(1, &kv);
63-
});
61+
(value1, value2, value3, value4)
62+
},
63+
|values| {
64+
let kv = &[
65+
KeyValue::new("attribute1", values.0),
66+
KeyValue::new("attribute2", values.1),
67+
KeyValue::new("attribute3", values.2),
68+
KeyValue::new("attribute4", values.3),
69+
];
70+
71+
counter.add(1, kv);
72+
},
73+
BatchSize::SmallInput);
6474
});
6575
}
6676

0 commit comments

Comments
 (0)