From e4eaa40728483abdc5c2984a0073d8baf8796d61 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 8 Mar 2024 00:46:40 +0000 Subject: [PATCH 1/5] noop-metrics-benchmark --- opentelemetry-sdk/Cargo.toml | 5 ++ opentelemetry-sdk/benches/noop_metrics.rs | 85 +++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 opentelemetry-sdk/benches/noop_metrics.rs diff --git a/opentelemetry-sdk/Cargo.toml b/opentelemetry-sdk/Cargo.toml index cc6f010683..d0afc774a9 100644 --- a/opentelemetry-sdk/Cargo.toml +++ b/opentelemetry-sdk/Cargo.toml @@ -68,6 +68,11 @@ name = "metric_counter" harness = false required-features = ["metrics"] +[[bench]] +name = "noop_metrics" +harness = false +required-features = ["metrics"] + [[bench]] name = "attribute_set" harness = false diff --git a/opentelemetry-sdk/benches/noop_metrics.rs b/opentelemetry-sdk/benches/noop_metrics.rs new file mode 100644 index 0000000000..6f784ce5c6 --- /dev/null +++ b/opentelemetry-sdk/benches/noop_metrics.rs @@ -0,0 +1,85 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use opentelemetry::{ + metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _}, + KeyValue, +}; + +use rand::{rngs::SmallRng, Rng, SeedableRng}; + +fn create_counter() -> Counter { + let meter_provider: NoopMeterProvider = NoopMeterProvider::default(); + let meter = meter_provider.meter("benchmarks"); + let counter = meter.u64_counter("counter_bench").init(); + counter +} + +fn criterion_benchmark(c: &mut Criterion) { + noop_counter_add(c); + create_keyvalue_vector(c); +} + +fn noop_counter_add(c: &mut Criterion) { + let attribute_values = [ + "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", + "value10", + ]; + + let mut rng = SmallRng::from_entropy(); + let num_samples = 1000; // Arbitrary number of samples for the benchmark + let random_indices: Vec<(usize, usize, usize, usize)> = (0..num_samples) + .map(|_| { + ( + rng.gen_range(0..4), + rng.gen_range(0..4), + rng.gen_range(0..10), + rng.gen_range(0..10), + ) + }) + .collect(); + + let noop_counter = create_counter(); + c.bench_function("Noop_Counter", |b| { + // Use an iterator to cycle through the pre-generated indices. + // This ensures that the benchmark does not exhaust the indices and each iteration gets a "random" set. + let mut indices_iter = random_indices.iter().cycle(); + b.iter(|| { + let ( + index_first_attribute, + index_second_attribute, + index_third_attribute, + index_forth_attribute, + ) = indices_iter.next().unwrap(); + noop_counter.add( + 1, + &[ + KeyValue::new("attribute1", attribute_values[*index_first_attribute]), + KeyValue::new("attribute2", attribute_values[*index_second_attribute]), + KeyValue::new("attribute3", attribute_values[*index_third_attribute]), + KeyValue::new("attribute4", attribute_values[*index_forth_attribute]), + ], + ); + }); + }); + + c.bench_function("Create_KeyValue", |b| { + let mut indices_iter = random_indices.iter().cycle(); + b.iter(|| { + let ( + index_first_attribute, + index_second_attribute, + index_third_attribute, + index_forth_attribute, + ) = indices_iter.next().unwrap(); + let _ = vec![ + KeyValue::new("attribute1", attribute_values[*index_first_attribute]), + KeyValue::new("attribute2", attribute_values[*index_second_attribute]), + KeyValue::new("attribute3", attribute_values[*index_third_attribute]), + KeyValue::new("attribute4", attribute_values[*index_forth_attribute]), + ]; + }); + }); +} + +criterion_group!(benches, criterion_benchmark); + +criterion_main!(benches); From 2961816c3ca3698d23305637ae923732e4a2ef13 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 8 Mar 2024 02:20:59 +0000 Subject: [PATCH 2/5] fix --- opentelemetry-sdk/Cargo.toml | 5 - opentelemetry/Cargo.toml | 6 ++ opentelemetry/benches/noop_metrics.rs | 92 +++++++++++++++++++ .../benches => opentelemetry}/noop_metrics.rs | 0 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 opentelemetry/benches/noop_metrics.rs rename {opentelemetry-sdk/benches => opentelemetry}/noop_metrics.rs (100%) diff --git a/opentelemetry-sdk/Cargo.toml b/opentelemetry-sdk/Cargo.toml index d0afc774a9..cc6f010683 100644 --- a/opentelemetry-sdk/Cargo.toml +++ b/opentelemetry-sdk/Cargo.toml @@ -68,11 +68,6 @@ name = "metric_counter" harness = false required-features = ["metrics"] -[[bench]] -name = "noop_metrics" -harness = false -required-features = ["metrics"] - [[bench]] name = "attribute_set" harness = false diff --git a/opentelemetry/Cargo.toml b/opentelemetry/Cargo.toml index 7e0b73600d..75bf9e524d 100644 --- a/opentelemetry/Cargo.toml +++ b/opentelemetry/Cargo.toml @@ -42,3 +42,9 @@ otel_unstable = [] [dev-dependencies] opentelemetry_sdk = { path = "../opentelemetry-sdk" } # for documentation tests +criterion = { version = "0.5", features = ["html_reports"] } + +[[bench]] +name = "noop_metrics" +harness = false +required-features = ["metrics"] diff --git a/opentelemetry/benches/noop_metrics.rs b/opentelemetry/benches/noop_metrics.rs new file mode 100644 index 0000000000..648e6b2fb1 --- /dev/null +++ b/opentelemetry/benches/noop_metrics.rs @@ -0,0 +1,92 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use opentelemetry::{ + metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _}, + KeyValue, +}; + +fn create_counter() -> Counter { + let meter_provider: NoopMeterProvider = NoopMeterProvider::default(); + let meter = meter_provider.meter("benchmarks"); + let counter = meter.u64_counter("counter_bench").init(); + counter +} + +fn criterion_benchmark(c: &mut Criterion) { + noop_counter_add(c); +} + +fn noop_counter_add(c: &mut Criterion) { + let noop_counter = create_counter(); + + c.bench_function("NoopCounter_NoAttributes", |b| { + b.iter(|| { + noop_counter.add(1, &[]); + }); + }); + + c.bench_function("NoopCounter_AddWithInlineStaticAttributes", |b| { + b.iter(|| { + noop_counter.add( + 1, + &[ + KeyValue::new("attribute1", "value1"), + KeyValue::new("attribute2", "value2"), + KeyValue::new("attribute3", "value3"), + KeyValue::new("attribute4", "value4"), + ], + ); + }); + }); + + c.bench_function("NoopCounter_AddWithStaticArray", |b| { + b.iter(|| { + let kv = [ + KeyValue::new("attribute1", "value1"), + KeyValue::new("attribute2", "value2"), + KeyValue::new("attribute3", "value3"), + KeyValue::new("attribute4", "value4"), + ]; + + noop_counter.add(1, &kv); + }); + }); + + c.bench_function("NoopCounter_AddWithDynamicAttributes", |b| { + b.iter(|| { + let kv = vec![ + KeyValue::new("attribute1", "value1"), + KeyValue::new("attribute2", "value2"), + KeyValue::new("attribute3", "value3"), + KeyValue::new("attribute4", "value4"), + ]; + + noop_counter.add(1, &kv); + }); + }); + + c.bench_function("CreateVector_KeyValue", |b| { + b.iter(|| { + let _v1 = vec![ + KeyValue::new("attribute1", "value1"), + KeyValue::new("attribute2", "value2"), + KeyValue::new("attribute3", "value3"), + KeyValue::new("attribute4", "value4"), + ]; + }); + }); + + c.bench_function("CreateDynamicVector_StringPair", |b| { + b.iter(|| { + let _v1 = vec![ + ("attribute1", "value1"), + ("attribute2", "value2"), + ("attribute3", "value3"), + ("attribute4", "value4"), + ]; + }); + }); +} + +criterion_group!(benches, criterion_benchmark); + +criterion_main!(benches); diff --git a/opentelemetry-sdk/benches/noop_metrics.rs b/opentelemetry/noop_metrics.rs similarity index 100% rename from opentelemetry-sdk/benches/noop_metrics.rs rename to opentelemetry/noop_metrics.rs From 999e99e2b655cc53e7423ff173a188707ef1f06c Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 8 Mar 2024 02:22:51 +0000 Subject: [PATCH 3/5] remove unwanted --- opentelemetry/noop_metrics.rs | 85 ----------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 opentelemetry/noop_metrics.rs diff --git a/opentelemetry/noop_metrics.rs b/opentelemetry/noop_metrics.rs deleted file mode 100644 index 6f784ce5c6..0000000000 --- a/opentelemetry/noop_metrics.rs +++ /dev/null @@ -1,85 +0,0 @@ -use criterion::{criterion_group, criterion_main, Criterion}; -use opentelemetry::{ - metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _}, - KeyValue, -}; - -use rand::{rngs::SmallRng, Rng, SeedableRng}; - -fn create_counter() -> Counter { - let meter_provider: NoopMeterProvider = NoopMeterProvider::default(); - let meter = meter_provider.meter("benchmarks"); - let counter = meter.u64_counter("counter_bench").init(); - counter -} - -fn criterion_benchmark(c: &mut Criterion) { - noop_counter_add(c); - create_keyvalue_vector(c); -} - -fn noop_counter_add(c: &mut Criterion) { - let attribute_values = [ - "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", - "value10", - ]; - - let mut rng = SmallRng::from_entropy(); - let num_samples = 1000; // Arbitrary number of samples for the benchmark - let random_indices: Vec<(usize, usize, usize, usize)> = (0..num_samples) - .map(|_| { - ( - rng.gen_range(0..4), - rng.gen_range(0..4), - rng.gen_range(0..10), - rng.gen_range(0..10), - ) - }) - .collect(); - - let noop_counter = create_counter(); - c.bench_function("Noop_Counter", |b| { - // Use an iterator to cycle through the pre-generated indices. - // This ensures that the benchmark does not exhaust the indices and each iteration gets a "random" set. - let mut indices_iter = random_indices.iter().cycle(); - b.iter(|| { - let ( - index_first_attribute, - index_second_attribute, - index_third_attribute, - index_forth_attribute, - ) = indices_iter.next().unwrap(); - noop_counter.add( - 1, - &[ - KeyValue::new("attribute1", attribute_values[*index_first_attribute]), - KeyValue::new("attribute2", attribute_values[*index_second_attribute]), - KeyValue::new("attribute3", attribute_values[*index_third_attribute]), - KeyValue::new("attribute4", attribute_values[*index_forth_attribute]), - ], - ); - }); - }); - - c.bench_function("Create_KeyValue", |b| { - let mut indices_iter = random_indices.iter().cycle(); - b.iter(|| { - let ( - index_first_attribute, - index_second_attribute, - index_third_attribute, - index_forth_attribute, - ) = indices_iter.next().unwrap(); - let _ = vec![ - KeyValue::new("attribute1", attribute_values[*index_first_attribute]), - KeyValue::new("attribute2", attribute_values[*index_second_attribute]), - KeyValue::new("attribute3", attribute_values[*index_third_attribute]), - KeyValue::new("attribute4", attribute_values[*index_forth_attribute]), - ]; - }); - }); -} - -criterion_group!(benches, criterion_benchmark); - -criterion_main!(benches); From 5c3e8a3e966fc71b483f5dd4ed1e2a2be82e2583 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 8 Mar 2024 02:30:28 +0000 Subject: [PATCH 4/5] lint error fix --- opentelemetry/benches/noop_metrics.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/opentelemetry/benches/noop_metrics.rs b/opentelemetry/benches/noop_metrics.rs index 648e6b2fb1..ba258293c0 100644 --- a/opentelemetry/benches/noop_metrics.rs +++ b/opentelemetry/benches/noop_metrics.rs @@ -64,6 +64,7 @@ fn noop_counter_add(c: &mut Criterion) { }); }); + #[allow(clippy::useless_vec)] c.bench_function("CreateVector_KeyValue", |b| { b.iter(|| { let _v1 = vec![ @@ -75,6 +76,7 @@ fn noop_counter_add(c: &mut Criterion) { }); }); + #[allow(clippy::useless_vec)] c.bench_function("CreateDynamicVector_StringPair", |b| { b.iter(|| { let _v1 = vec![ From 42731f52965d74ad8c707afa1a5dfc080fecf202 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 8 Mar 2024 02:47:05 +0000 Subject: [PATCH 5/5] downgrade criterion version --- opentelemetry/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry/Cargo.toml b/opentelemetry/Cargo.toml index 75bf9e524d..ae198db3d9 100644 --- a/opentelemetry/Cargo.toml +++ b/opentelemetry/Cargo.toml @@ -42,7 +42,7 @@ otel_unstable = [] [dev-dependencies] opentelemetry_sdk = { path = "../opentelemetry-sdk" } # for documentation tests -criterion = { version = "0.5", features = ["html_reports"] } +criterion = { version = "0.4", features = ["html_reports"] } [[bench]] name = "noop_metrics"