Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactory opentelemetry benchmarks #1614

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ opentelemetry_sdk = { path = "../opentelemetry-sdk" } # for documentation tests
criterion = { version = "0.3" }

[[bench]]
name = "noop_metrics"
name = "metrics"
harness = false
required-features = ["metrics"]

[[bench]]
name = "attributes"
harness = false
56 changes: 56 additions & 0 deletions opentelemetry/benches/attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use opentelemetry::KeyValue;

// Run this benchmark with:
// cargo bench --bench attributes

fn criterion_benchmark(c: &mut Criterion) {
attributes_creation(c);
}

fn attributes_creation(c: &mut Criterion) {

c.bench_function("CreateOTelKeyValue", |b| {
b.iter(|| {
let _v1 = black_box(
KeyValue::new("attribute1", "value1")
);
});
});

c.bench_function("CreateKeyValueTuple", |b| {
b.iter(|| {
let _v1 = black_box(
("attribute1", "value1")
);
});
});

#[allow(clippy::useless_vec)]
c.bench_function("CreateVector_KeyValue", |b| {
b.iter(|| {
let _v1 = black_box(vec![
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute2", "value2"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
]);
});
});

#[allow(clippy::useless_vec)]
c.bench_function("CreateVector_StringPairs", |b| {
b.iter(|| {
let _v1 = black_box(vec![
("attribute1", "value1"),
("attribute2", "value2"),
("attribute3", "value3"),
("attribute4", "value4"),
]);
});
});
}

criterion_group!(benches, criterion_benchmark);

criterion_main!(benches);
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, Criterion};
use opentelemetry::{
metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _},
KeyValue,
};

// Run this benchmark with:
// cargo bench --bench metrics --features=metrics

fn create_counter() -> Counter<u64> {
let meter_provider: NoopMeterProvider = NoopMeterProvider::default();
let meter = meter_provider.meter("benchmarks");
Expand All @@ -12,21 +15,21 @@ fn create_counter() -> Counter<u64> {
}

fn criterion_benchmark(c: &mut Criterion) {
noop_counter_add(c);
counter_add(c);
}

fn noop_counter_add(c: &mut Criterion) {
let noop_counter = create_counter();
fn counter_add(c: &mut Criterion) {
let counter = create_counter();

c.bench_function("NoopCounter_NoAttributes", |b| {
c.bench_function("Counter_NoAttributes", |b| {
b.iter(|| {
noop_counter.add(1, &[]);
counter.add(1, &[]);
});
});

c.bench_function("NoopCounter_AddWithInlineStaticAttributes", |b| {
c.bench_function("Counter_AddWithInlineStaticAttributes", |b| {
b.iter(|| {
noop_counter.add(
counter.add(
1,
&[
KeyValue::new("attribute1", "value1"),
Expand All @@ -38,7 +41,7 @@ fn noop_counter_add(c: &mut Criterion) {
});
});

c.bench_function("NoopCounter_AddWithStaticArray", |b| {
c.bench_function("Counter_AddWithStaticArray", |b| {
b.iter(|| {
let kv = [
KeyValue::new("attribute1", "value1"),
Expand All @@ -47,11 +50,11 @@ fn noop_counter_add(c: &mut Criterion) {
KeyValue::new("attribute4", "value4"),
];

noop_counter.add(1, &kv);
counter.add(1, &kv);
});
});

c.bench_function("NoopCounter_AddWithDynamicAttributes", |b| {
c.bench_function("Counter_AddWithDynamicAttributes", |b| {
b.iter(|| {
let kv = vec![
KeyValue::new("attribute1", "value1"),
Expand All @@ -60,31 +63,7 @@ fn noop_counter_add(c: &mut Criterion) {
KeyValue::new("attribute4", "value4"),
];

noop_counter.add(1, &kv);
});
});

#[allow(clippy::useless_vec)]
c.bench_function("CreateVector_KeyValue", |b| {
b.iter(|| {
let _v1 = black_box(vec![
KeyValue::new("attribute1", "value1"),
KeyValue::new("attribute2", "value2"),
KeyValue::new("attribute3", "value3"),
KeyValue::new("attribute4", "value4"),
]);
});
});

#[allow(clippy::useless_vec)]
c.bench_function("CreateDynamicVector_StringPair", |b| {
b.iter(|| {
let _v1 = black_box(vec![
("attribute1", "value1"),
("attribute2", "value2"),
("attribute3", "value3"),
("attribute4", "value4"),
]);
counter.add(1, &kv);
});
});
}
Expand Down
Loading