Skip to content

Commit

Permalink
Benchmarks for AnyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Jun 28, 2024
1 parent 0303a38 commit 1ff14ae
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ required-features = ["metrics"]
[[bench]]
name = "attributes"
harness = false

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

// Run this benchmark with:
// cargo bench --bench logrecord_types
// Results:
// CreateOTelKeyValue 2-3 ns
// CreateOTelKeyAnyValue 30 ns
// CreateTupleKeyValue < 1 ns

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("CreateOTelKeyAnyValue", |b| {
b.iter(|| {
let _k= black_box(Key::new("attribute1"));
let _v1 = black_box(AnyValue::String("value1".to_string().into()));
});
});

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

criterion_group!(benches, criterion_benchmark);

criterion_main!(benches);

0 comments on commit 1ff14ae

Please sign in to comment.