Skip to content

Commit 7b3ec69

Browse files
authored
Merge branch 'main' into cijothomas/future-exporter
2 parents 9758e0b + 72ac56f commit 7b3ec69

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

opentelemetry-sdk/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ harness = false
6868
name = "metric_gauge"
6969
harness = false
7070

71+
[[bench]]
72+
name = "metric_histogram"
73+
harness = false
74+
7175
[[bench]]
7276
name = "attribute_set"
7377
harness = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
The benchmark results:
3+
criterion = "0.5.1"
4+
OS: Ubuntu 22.04.4 LTS (5.15.153.1-microsoft-standard-WSL2)
5+
Hardware: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz, 16vCPUs,
6+
RAM: 64.0 GB
7+
| Test | Average time|
8+
|--------------------------------|-------------|
9+
| Histogram_Record | 509.21 ns |
10+
11+
*/
12+
13+
use criterion::{criterion_group, criterion_main, Criterion};
14+
use opentelemetry::{
15+
metrics::{Histogram, MeterProvider as _},
16+
KeyValue,
17+
};
18+
use opentelemetry_sdk::metrics::{ManualReader, SdkMeterProvider};
19+
use pprof::criterion::{Output, PProfProfiler};
20+
use rand::{
21+
rngs::{self},
22+
Rng, SeedableRng,
23+
};
24+
use std::cell::RefCell;
25+
26+
thread_local! {
27+
/// Store random number generator for each thread
28+
static CURRENT_RNG: RefCell<rngs::SmallRng> = RefCell::new(rngs::SmallRng::from_entropy());
29+
}
30+
31+
static ATTRIBUTE_VALUES: [&str; 10] = [
32+
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
33+
"value10",
34+
];
35+
36+
// Run this benchmark with:
37+
// cargo bench --bench metric_histogram
38+
fn create_histogram(name: &'static str) -> Histogram<u64> {
39+
let meter_provider: SdkMeterProvider = SdkMeterProvider::builder()
40+
.with_reader(ManualReader::builder().build())
41+
.build();
42+
let meter = meter_provider.meter("benchmarks");
43+
44+
meter.u64_histogram(name).init()
45+
}
46+
47+
fn criterion_benchmark(c: &mut Criterion) {
48+
histogram_record(c);
49+
}
50+
51+
fn histogram_record(c: &mut Criterion) {
52+
let histogram = create_histogram("Histogram_Record");
53+
c.bench_function("Histogram_Record", |b| {
54+
b.iter(|| {
55+
// 4*4*10*10 = 1600 time series.
56+
let rands = CURRENT_RNG.with(|rng| {
57+
let mut rng = rng.borrow_mut();
58+
[
59+
rng.gen_range(0..4),
60+
rng.gen_range(0..4),
61+
rng.gen_range(0..10),
62+
rng.gen_range(0..10),
63+
]
64+
});
65+
let index_first_attribute = rands[0];
66+
let index_second_attribute = rands[1];
67+
let index_third_attribute = rands[2];
68+
let index_fourth_attribute = rands[3];
69+
histogram.record(
70+
1,
71+
&[
72+
KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]),
73+
KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]),
74+
KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]),
75+
KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]),
76+
],
77+
);
78+
});
79+
});
80+
}
81+
82+
#[cfg(not(target_os = "windows"))]
83+
criterion_group! {
84+
name = benches;
85+
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
86+
targets = criterion_benchmark
87+
}
88+
#[cfg(target_os = "windows")]
89+
criterion_group! {
90+
name = benches;
91+
config = Criterion::default();
92+
targets = criterion_benchmark
93+
}
94+
criterion_main!(benches);

stress/src/metrics_histogram.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ thread_local! {
4040
}
4141

4242
fn main() {
43-
throughput::test_throughput(test_counter);
43+
throughput::test_throughput(test_histogram);
4444
}
4545

46-
fn test_counter() {
46+
fn test_histogram() {
4747
let len = ATTRIBUTE_VALUES.len();
4848
let rands = CURRENT_RNG.with(|rng| {
4949
let mut rng = rng.borrow_mut();

0 commit comments

Comments
 (0)