From 590c7ab602c18285183c8cc8342b129d2b15d423 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Wed, 17 Apr 2024 16:02:04 -0700 Subject: [PATCH] Add benchmarks to show cost of random generation (#1667) --- opentelemetry-sdk/benches/metric_counter.rs | 11 +++++++++++ opentelemetry/benches/metrics.rs | 14 +++++++------- stress/Cargo.toml | 9 +++++++-- stress/src/random.rs | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 stress/src/random.rs diff --git a/opentelemetry-sdk/benches/metric_counter.rs b/opentelemetry-sdk/benches/metric_counter.rs index 4bb4c84e6a..aa534e4f99 100644 --- a/opentelemetry-sdk/benches/metric_counter.rs +++ b/opentelemetry-sdk/benches/metric_counter.rs @@ -67,6 +67,17 @@ fn counter_add(c: &mut Criterion) { ); }); }); + + c.bench_function("Random_Generator_5", |b| { + b.iter(|| { + let mut rng = SmallRng::from_entropy(); + let _i1 = rng.gen_range(0..4); + let _i2 = rng.gen_range(0..4); + let _i3 = rng.gen_range(0..10); + let _i4 = rng.gen_range(0..10); + let _i5 = rng.gen_range(0..10); + }); + }); } criterion_group!(benches, criterion_benchmark); diff --git a/opentelemetry/benches/metrics.rs b/opentelemetry/benches/metrics.rs index 44c898d900..86258d95ea 100644 --- a/opentelemetry/benches/metrics.rs +++ b/opentelemetry/benches/metrics.rs @@ -37,15 +37,15 @@ fn counter_add(c: &mut Criterion) { }); }); + let kv = [ + KeyValue::new("attribute1", "value1"), + KeyValue::new("attribute2", "value2"), + KeyValue::new("attribute3", "value3"), + KeyValue::new("attribute4", "value4"), + ]; + c.bench_function("Counter_AddWithStaticArray", |b| { b.iter(|| { - let kv = [ - KeyValue::new("attribute1", "value1"), - KeyValue::new("attribute2", "value2"), - KeyValue::new("attribute3", "value3"), - KeyValue::new("attribute4", "value4"), - ]; - counter.add(1, &kv); }); }); diff --git a/stress/Cargo.toml b/stress/Cargo.toml index 7ee1379b9d..c693eef984 100644 --- a/stress/Cargo.toml +++ b/stress/Cargo.toml @@ -19,12 +19,17 @@ name = "traces" path = "src/traces.rs" doc = false +[[bin]] # Bin to run the stress tests to show the cost of random number generation +name = "random" +path = "src/random.rs" +doc = false + [dependencies] ctrlc = "3.2.5" lazy_static = "1.4.0" num_cpus = "1.15.0" -opentelemetry = { path = "../opentelemetry", features = ["metrics", "logs", "trace"] } -opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["metrics", "logs", "trace"] } +opentelemetry = { path = "../opentelemetry", features = ["metrics", "logs", "trace", "logs_level_enabled"] } +opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["metrics", "logs", "trace", "logs_level_enabled"] } opentelemetry-appender-tracing = { path = "../opentelemetry-appender-tracing"} rand = { version = "0.8.4", features = ["small_rng"] } tracing = { workspace = true, features = ["std"]} diff --git a/stress/src/random.rs b/stress/src/random.rs new file mode 100644 index 0000000000..5cefe5af80 --- /dev/null +++ b/stress/src/random.rs @@ -0,0 +1,14 @@ +use rand::{rngs::SmallRng, Rng, SeedableRng}; + +mod throughput; + +fn main() { + throughput::test_throughput(test_random_generation); +} + +fn test_random_generation() { + let mut rng = SmallRng::from_entropy(); + let _index_first_attribute = rng.gen_range(0..10); + let _index_second_attribute = rng.gen_range(0..10); + let _index_third_attribute = rng.gen_range(0..10); +}