Skip to content

Commit

Permalink
Add benchmarks to show cost of random generation (#1667)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Apr 17, 2024
1 parent 758cdd7 commit 590c7ab
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
11 changes: 11 additions & 0 deletions opentelemetry-sdk/benches/metric_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions opentelemetry/benches/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
9 changes: 7 additions & 2 deletions stress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down
14 changes: 14 additions & 0 deletions stress/src/random.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 590c7ab

Please sign in to comment.