Skip to content

Commit b121fea

Browse files
committed
Refactory opentelemetry benchmarks
1 parent df12c2c commit b121fea

File tree

3 files changed

+76
-37
lines changed

3 files changed

+76
-37
lines changed

opentelemetry/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ opentelemetry_sdk = { path = "../opentelemetry-sdk" } # for documentation tests
4545
criterion = { version = "0.3" }
4646

4747
[[bench]]
48-
name = "noop_metrics"
48+
name = "metrics"
4949
harness = false
5050
required-features = ["metrics"]
51+
52+
[[bench]]
53+
name = "attributes"
54+
harness = false

opentelemetry/benches/attributes.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
use opentelemetry::KeyValue;
3+
4+
// Run this benchmark with:
5+
// cargo bench --bench attributes
6+
7+
fn criterion_benchmark(c: &mut Criterion) {
8+
attributes_creation(c);
9+
}
10+
11+
fn attributes_creation(c: &mut Criterion) {
12+
13+
c.bench_function("CreateOTelKeyValue", |b| {
14+
b.iter(|| {
15+
let _v1 = black_box(
16+
KeyValue::new("attribute1", "value1")
17+
);
18+
});
19+
});
20+
21+
c.bench_function("CreateKeyValueTuple", |b| {
22+
b.iter(|| {
23+
let _v1 = black_box(
24+
("attribute1", "value1")
25+
);
26+
});
27+
});
28+
29+
#[allow(clippy::useless_vec)]
30+
c.bench_function("CreateVector_KeyValue", |b| {
31+
b.iter(|| {
32+
let _v1 = black_box(vec![
33+
KeyValue::new("attribute1", "value1"),
34+
KeyValue::new("attribute2", "value2"),
35+
KeyValue::new("attribute3", "value3"),
36+
KeyValue::new("attribute4", "value4"),
37+
]);
38+
});
39+
});
40+
41+
#[allow(clippy::useless_vec)]
42+
c.bench_function("CreateVector_StringPairs", |b| {
43+
b.iter(|| {
44+
let _v1 = black_box(vec![
45+
("attribute1", "value1"),
46+
("attribute2", "value2"),
47+
("attribute3", "value3"),
48+
("attribute4", "value4"),
49+
]);
50+
});
51+
});
52+
}
53+
54+
criterion_group!(benches, criterion_benchmark);
55+
56+
criterion_main!(benches);

opentelemetry/benches/noop_metrics.rs opentelemetry/benches/metrics.rs

+15-36
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1+
use criterion::{criterion_group, criterion_main, Criterion};
22
use opentelemetry::{
33
metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _},
44
KeyValue,
55
};
66

7+
// Run this benchmark with:
8+
// cargo bench --bench metrics --features=metrics
9+
710
fn create_counter() -> Counter<u64> {
811
let meter_provider: NoopMeterProvider = NoopMeterProvider::default();
912
let meter = meter_provider.meter("benchmarks");
@@ -12,21 +15,21 @@ fn create_counter() -> Counter<u64> {
1215
}
1316

1417
fn criterion_benchmark(c: &mut Criterion) {
15-
noop_counter_add(c);
18+
counter_add(c);
1619
}
1720

18-
fn noop_counter_add(c: &mut Criterion) {
19-
let noop_counter = create_counter();
21+
fn counter_add(c: &mut Criterion) {
22+
let counter = create_counter();
2023

21-
c.bench_function("NoopCounter_NoAttributes", |b| {
24+
c.bench_function("Counter_NoAttributes", |b| {
2225
b.iter(|| {
23-
noop_counter.add(1, &[]);
26+
counter.add(1, &[]);
2427
});
2528
});
2629

27-
c.bench_function("NoopCounter_AddWithInlineStaticAttributes", |b| {
30+
c.bench_function("Counter_AddWithInlineStaticAttributes", |b| {
2831
b.iter(|| {
29-
noop_counter.add(
32+
counter.add(
3033
1,
3134
&[
3235
KeyValue::new("attribute1", "value1"),
@@ -38,7 +41,7 @@ fn noop_counter_add(c: &mut Criterion) {
3841
});
3942
});
4043

41-
c.bench_function("NoopCounter_AddWithStaticArray", |b| {
44+
c.bench_function("Counter_AddWithStaticArray", |b| {
4245
b.iter(|| {
4346
let kv = [
4447
KeyValue::new("attribute1", "value1"),
@@ -47,11 +50,11 @@ fn noop_counter_add(c: &mut Criterion) {
4750
KeyValue::new("attribute4", "value4"),
4851
];
4952

50-
noop_counter.add(1, &kv);
53+
counter.add(1, &kv);
5154
});
5255
});
5356

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

63-
noop_counter.add(1, &kv);
64-
});
65-
});
66-
67-
#[allow(clippy::useless_vec)]
68-
c.bench_function("CreateVector_KeyValue", |b| {
69-
b.iter(|| {
70-
let _v1 = black_box(vec![
71-
KeyValue::new("attribute1", "value1"),
72-
KeyValue::new("attribute2", "value2"),
73-
KeyValue::new("attribute3", "value3"),
74-
KeyValue::new("attribute4", "value4"),
75-
]);
76-
});
77-
});
78-
79-
#[allow(clippy::useless_vec)]
80-
c.bench_function("CreateDynamicVector_StringPair", |b| {
81-
b.iter(|| {
82-
let _v1 = black_box(vec![
83-
("attribute1", "value1"),
84-
("attribute2", "value2"),
85-
("attribute3", "value3"),
86-
("attribute4", "value4"),
87-
]);
66+
counter.add(1, &kv);
8867
});
8968
});
9069
}

0 commit comments

Comments
 (0)