Skip to content

Commit ecf0dda

Browse files
authored
Add microbenching testings for histogram.add() (#1049)
* Update metric.rs * lint * lint * const
1 parent cb65114 commit ecf0dda

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

opentelemetry-sdk/benches/metric.rs

+93-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use rand::Rng;
12
use std::sync::{Arc, Weak};
23

34
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
45
use opentelemetry_api::{
5-
metrics::{Counter, MeterProvider as _, Result},
6+
metrics::{Counter, Histogram, MeterProvider as _, Result},
67
Context, Key, KeyValue,
78
};
89
use opentelemetry_sdk::{
@@ -103,6 +104,46 @@ impl TemporalitySelector for DeltaTemporalitySelector {
103104
// time: [677.00 ns 681.63 ns 686.88 ns]
104105
// Counter/CollectOneAttr time: [659.29 ns 681.20 ns 708.04 ns]
105106
// Counter/CollectTenAttrs time: [3.5048 µs 3.5384 µs 3.5777 µs]
107+
// Histogram/Record0Attrs10bounds
108+
// time: [75.790 ns 77.235 ns 78.825 ns]
109+
// Histogram/Record3Attrs10bounds
110+
// time: [580.88 ns 603.84 ns 628.71 ns]
111+
// Histogram/Record5Attrs10bounds
112+
// time: [3.8539 µs 3.8988 µs 3.9519 µs]
113+
// Histogram/Record7Attrs10bounds
114+
// time: [699.46 ns 720.17 ns 742.24 ns]
115+
// Histogram/Record10Attrs10bounds
116+
// time: [844.95 ns 861.92 ns 880.23 ns]
117+
// Histogram/Record0Attrs49bounds
118+
// time: [75.198 ns 77.081 ns 79.449 ns]
119+
// Histogram/Record3Attrs49bounds
120+
// time: [533.82 ns 540.44 ns 547.30 ns]
121+
// Histogram/Record5Attrs49bounds
122+
// time: [583.01 ns 588.27 ns 593.98 ns]
123+
// Histogram/Record7Attrs49bounds
124+
// time: [645.67 ns 652.03 ns 658.35 ns]
125+
// Histogram/Record10Attrs49bounds
126+
// time: [747.24 ns 755.42 ns 764.37 ns]
127+
// Histogram/Record0Attrs50bounds
128+
// time: [72.023 ns 72.218 ns 72.426 ns]
129+
// Histogram/Record3Attrs50bounds
130+
// time: [530.21 ns 534.23 ns 538.63 ns]
131+
// Histogram/Record5Attrs50bounds
132+
// time: [3.2934 µs 3.3069 µs 3.3228 µs]
133+
// Histogram/Record7Attrs50bounds
134+
// time: [633.88 ns 638.87 ns 644.52 ns]
135+
// Histogram/Record10Attrs50bounds
136+
// time: [759.69 ns 768.42 ns 778.12 ns]
137+
// Histogram/Record0Attrs1000bounds
138+
// time: [75.495 ns 75.942 ns 76.529 ns]
139+
// Histogram/Record3Attrs1000bounds
140+
// time: [542.06 ns 548.37 ns 555.31 ns]
141+
// Histogram/Record5Attrs1000bounds
142+
// time: [3.2935 µs 3.3058 µs 3.3215 µs]
143+
// Histogram/Record7Attrs1000bounds
144+
// time: [643.75 ns 649.05 ns 655.14 ns]
145+
// Histogram/Record10Attrs1000bounds
146+
// time: [726.87 ns 736.52 ns 747.09 ns]
106147
fn bench_counter(
107148
view: Option<Box<dyn View>>,
108149
temporality: &str,
@@ -301,9 +342,60 @@ fn counters(c: &mut Criterion) {
301342
});
302343
}
303344

345+
const MAX_BOUND: usize = 100000;
346+
347+
fn bench_histogram(bound_count: usize) -> (Context, SharedReader, Histogram<i64>) {
348+
let mut bounds = vec![0; bound_count];
349+
#[allow(clippy::needless_range_loop)]
350+
for i in 0..bounds.len() {
351+
bounds[i] = i * MAX_BOUND / bound_count
352+
}
353+
let view = Some(
354+
new_view(
355+
Instrument::new().name("histogram_*"),
356+
Stream::new().aggregation(Aggregation::ExplicitBucketHistogram {
357+
boundaries: bounds.iter().map(|&x| x as f64).collect(),
358+
record_min_max: true,
359+
}),
360+
)
361+
.unwrap(),
362+
);
363+
364+
let cx = Context::new();
365+
let r = SharedReader(Arc::new(ManualReader::default()));
366+
let mut builder = MeterProvider::builder().with_reader(r.clone());
367+
if let Some(view) = view {
368+
builder = builder.with_view(view);
369+
}
370+
let mtr = builder.build().meter("test_meter");
371+
let hist = mtr
372+
.i64_histogram(format!("histogram_{}", bound_count))
373+
.init();
374+
375+
(cx, r, hist)
376+
}
377+
304378
fn histograms(c: &mut Criterion) {
305379
let mut group = c.benchmark_group("Histogram");
380+
let mut rng = rand::thread_rng();
306381

382+
for bound_size in [10, 49, 50, 1000].iter() {
383+
let (cx, _, hist) = bench_histogram(*bound_size);
384+
for attr_size in [0, 3, 5, 7, 10].iter() {
385+
let mut attributes: Vec<KeyValue> = Vec::new();
386+
for i in 0..*attr_size {
387+
attributes.push(KeyValue::new(
388+
format!("K,{},{}", bound_size, attr_size),
389+
format!("V,{},{},{}", bound_size, attr_size, i),
390+
))
391+
}
392+
let value: i64 = rng.gen_range(0..MAX_BOUND).try_into().unwrap();
393+
group.bench_function(
394+
format!("Record{}Attrs{}bounds", attr_size, bound_size),
395+
|b| b.iter(|| hist.record(&cx, value, &attributes)),
396+
);
397+
}
398+
}
307399
group.bench_function("CollectOne", |b| benchmark_collect_histogram(b, 1));
308400
group.bench_function("CollectFive", |b| benchmark_collect_histogram(b, 5));
309401
group.bench_function("CollectTen", |b| benchmark_collect_histogram(b, 10));

0 commit comments

Comments
 (0)