Skip to content

Commit

Permalink
Add temporality benchmark tests for counter in metrics (open-telemetr…
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Apr 23, 2023
1 parent e7163f1 commit f42c11d
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 12 deletions.
9 changes: 8 additions & 1 deletion opentelemetry-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ The Rust [OpenTelemetry](https://opentelemetry.io/) implementation.
[![codecov](https://codecov.io/gh/open-telemetry/opentelemetry-rust/branch/main/graph/badge.svg)](https://codecov.io/gh/open-telemetry/opentelemetry-rust)
[![Slack](https://img.shields.io/badge/slack-@cncf/otel/rust-brightgreen.svg?logo=slack)](https://cloud-native.slack.com/archives/C03GDP0H023)


## Overview

OpenTelemetry is a collection of tools, APIs, and SDKs used to instrument,
Expand All @@ -27,3 +26,11 @@ observability tools.
[Prometheus]: https://prometheus.io
[Jaeger]: https://www.jaegertracing.io
[msrv]: #supported-rust-versions

## OpenTelemetry Benchmarks

From the root folder, run the following command:

```sh
cargo bench
```
188 changes: 177 additions & 11 deletions opentelemetry-sdk/benches/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,70 @@ impl MetricReader for SharedReader {
}
}

fn bench_counter(view: Option<Box<dyn View>>) -> (Context, SharedReader, Counter<u64>) {
/// Configure delta temporality for all [InstrumentKind]
///
/// [Temporality::Delta] will be used for all instrument kinds if this
/// [TemporalitySelector] is used.
#[derive(Clone, Default, Debug)]
pub struct DeltaTemporalitySelector {
pub(crate) _private: (),
}

impl DeltaTemporalitySelector {
/// Create a new default temporality selector.
pub fn new() -> Self {
Self::default()
}
}

impl TemporalitySelector for DeltaTemporalitySelector {
fn temporality(&self, _kind: InstrumentKind) -> Temporality {
Temporality::Delta
}
}

// * Summary *

// rustc 1.68.0 (2c8cc3432 2023-03-06)
// cargo 1.68.0 (115f34552 2023-02-26), OS=Windows 11 Enterprise
// Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz 2.59 GHz
// 12 logical and 6 physical cores

// Counter/AddNoAttrs time: [65.406 ns 65.535 ns 65.675 ns]
// Counter/AddNoAttrsDelta time: [65.553 ns 65.761 ns 65.981 ns]
// Counter/AddOneAttr time: [341.55 ns 344.40 ns 347.58 ns]
// Counter/AddOneAttrDelta time: [340.11 ns 342.42 ns 344.89 ns]
// Counter/AddThreeAttr time: [619.01 ns 624.16 ns 630.16 ns]
// Counter/AddThreeAttrDelta
// time: [606.71 ns 611.45 ns 616.66 ns]
// Counter/AddFiveAttr time: [3.7551 µs 3.7813 µs 3.8094 µs]
// Counter/AddFiveAttrDelta
// time: [3.7550 µs 3.7870 µs 3.8266 µs]
// Counter/AddTenAttr time: [4.7684 µs 4.7909 µs 4.8146 µs]
// Counter/AddTenAttrDelta time: [4.7682 µs 4.8152 µs 4.8722 µs]
// Counter/AddInvalidAttr time: [469.31 ns 472.97 ns 476.92 ns]
// Counter/AddSingleUseAttrs
// time: [749.15 ns 805.09 ns 868.03 ns]
// Counter/AddSingleUseInvalid
// time: [693.75 ns 702.65 ns 713.20 ns]
// Counter/AddSingleUseFiltered
// time: [677.00 ns 681.63 ns 686.88 ns]
// Counter/CollectOneAttr time: [659.29 ns 681.20 ns 708.04 ns]
// Counter/CollectTenAttrs time: [3.5048 µs 3.5384 µs 3.5777 µs]
fn bench_counter(
view: Option<Box<dyn View>>,
temporality: &str,
) -> (Context, SharedReader, Counter<u64>) {
let cx = Context::new();
let rdr = SharedReader(Arc::new(ManualReader::builder().build()));
let rdr = if temporality == "cumulative" {
SharedReader(Arc::new(ManualReader::builder().build()))
} else {
SharedReader(Arc::new(
ManualReader::builder()
.with_temporality_selector(DeltaTemporalitySelector::new())
.build(),
))
};
let mut builder = MeterProvider::builder().with_reader(rdr.clone());
if let Some(view) = view {
builder = builder.with_view(view);
Expand All @@ -67,13 +128,115 @@ fn bench_counter(view: Option<Box<dyn View>>) -> (Context, SharedReader, Counter
}

fn counters(c: &mut Criterion) {
let (cx, _, cntr) = bench_counter(None);
let (cx, _, cntr) = bench_counter(None, "cumulative");
let (cx2, _, cntr2) = bench_counter(None, "delta");

let mut group = c.benchmark_group("Counter");
group.bench_function("AddNoAttrs", |b| b.iter(|| cntr.add(&cx, 1, &[])));
group.bench_function("AddNoAttrsDelta", |b| b.iter(|| cntr2.add(&cx, 1, &[])));

group.bench_function("AddOneAttr", |b| {
b.iter(|| cntr.add(&cx, 1, &[KeyValue::new("K", "V")]))
});
group.bench_function("AddOneAttrDelta", |b| {
b.iter(|| cntr2.add(&cx2, 1, &[KeyValue::new("K1", "V1")]))
});
group.bench_function("AddThreeAttr", |b| {
b.iter(|| {
cntr.add(
&cx,
1,
&[
KeyValue::new("K2", "V2"),
KeyValue::new("K3", "V3"),
KeyValue::new("K4", "V4"),
],
)
})
});
group.bench_function("AddThreeAttrDelta", |b| {
b.iter(|| {
cntr2.add(
&cx2,
1,
&[
KeyValue::new("K2", "V2"),
KeyValue::new("K3", "V3"),
KeyValue::new("K4", "V4"),
],
)
})
});
group.bench_function("AddFiveAttr", |b| {
b.iter(|| {
cntr.add(
&cx,
1,
&[
KeyValue::new("K5", "V5"),
KeyValue::new("K6", "V6"),
KeyValue::new("K7", "V7"),
KeyValue::new("K8", "V8"),
KeyValue::new("K9", "V9"),
],
)
})
});
group.bench_function("AddFiveAttrDelta", |b| {
b.iter(|| {
cntr2.add(
&cx2,
1,
&[
KeyValue::new("K5", "V5"),
KeyValue::new("K6", "V6"),
KeyValue::new("K7", "V7"),
KeyValue::new("K8", "V8"),
KeyValue::new("K9", "V9"),
],
)
})
});
group.bench_function("AddTenAttr", |b| {
b.iter(|| {
cntr.add(
&cx,
1,
&[
KeyValue::new("K10", "V10"),
KeyValue::new("K11", "V11"),
KeyValue::new("K12", "V12"),
KeyValue::new("K13", "V13"),
KeyValue::new("K14", "V14"),
KeyValue::new("K15", "V15"),
KeyValue::new("K16", "V16"),
KeyValue::new("K17", "V17"),
KeyValue::new("K18", "V18"),
KeyValue::new("K19", "V19"),
],
)
})
});
group.bench_function("AddTenAttrDelta", |b| {
b.iter(|| {
cntr2.add(
&cx2,
1,
&[
KeyValue::new("K10", "V10"),
KeyValue::new("K11", "V11"),
KeyValue::new("K12", "V12"),
KeyValue::new("K13", "V13"),
KeyValue::new("K14", "V14"),
KeyValue::new("K15", "V15"),
KeyValue::new("K16", "V16"),
KeyValue::new("K17", "V17"),
KeyValue::new("K18", "V18"),
KeyValue::new("K19", "V19"),
],
)
})
});
group.bench_function("AddInvalidAttr", |b| {
b.iter(|| cntr.add(&cx, 1, &[KeyValue::new("", "V"), KeyValue::new("K", "V")]))
});
Expand All @@ -92,13 +255,16 @@ fn counters(c: &mut Criterion) {
})
});

let (cx, _, cntr) = bench_counter(Some(
new_view(
Instrument::new().name("*"),
Stream::new().attribute_filter(|kv| kv.key == Key::new("K")),
)
.unwrap(),
));
let (cx, _, cntr) = bench_counter(
Some(
new_view(
Instrument::new().name("*"),
Stream::new().attribute_filter(|kv| kv.key == Key::new("K")),
)
.unwrap(),
),
"cumulative",
);

group.bench_function("AddSingleUseFiltered", |b| {
let mut v = 0;
Expand All @@ -108,7 +274,7 @@ fn counters(c: &mut Criterion) {
})
});

let (cx, rdr, cntr) = bench_counter(None);
let (cx, rdr, cntr) = bench_counter(None, "cumulative");
let mut rm = ResourceMetrics {
resource: Resource::empty(),
scope_metrics: Vec::new(),
Expand Down

0 comments on commit f42c11d

Please sign in to comment.