|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | +use opentelemetry::{ |
| 3 | + metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _}, |
| 4 | + KeyValue, |
| 5 | +}; |
| 6 | + |
| 7 | +use rand::{rngs::SmallRng, Rng, SeedableRng}; |
| 8 | + |
| 9 | +fn create_counter() -> Counter<u64> { |
| 10 | + let meter_provider: NoopMeterProvider = NoopMeterProvider::default(); |
| 11 | + let meter = meter_provider.meter("benchmarks"); |
| 12 | + let counter = meter.u64_counter("counter_bench").init(); |
| 13 | + counter |
| 14 | +} |
| 15 | + |
| 16 | +fn criterion_benchmark(c: &mut Criterion) { |
| 17 | + noop_counter_add(c); |
| 18 | + create_keyvalue_vector(c); |
| 19 | +} |
| 20 | + |
| 21 | +fn noop_counter_add(c: &mut Criterion) { |
| 22 | + let attribute_values = [ |
| 23 | + "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", |
| 24 | + "value10", |
| 25 | + ]; |
| 26 | + |
| 27 | + let mut rng = SmallRng::from_entropy(); |
| 28 | + let num_samples = 1000; // Arbitrary number of samples for the benchmark |
| 29 | + let random_indices: Vec<(usize, usize, usize, usize)> = (0..num_samples) |
| 30 | + .map(|_| { |
| 31 | + ( |
| 32 | + rng.gen_range(0..4), |
| 33 | + rng.gen_range(0..4), |
| 34 | + rng.gen_range(0..10), |
| 35 | + rng.gen_range(0..10), |
| 36 | + ) |
| 37 | + }) |
| 38 | + .collect(); |
| 39 | + |
| 40 | + let noop_counter = create_counter(); |
| 41 | + c.bench_function("Noop_Counter", |b| { |
| 42 | + // Use an iterator to cycle through the pre-generated indices. |
| 43 | + // This ensures that the benchmark does not exhaust the indices and each iteration gets a "random" set. |
| 44 | + let mut indices_iter = random_indices.iter().cycle(); |
| 45 | + b.iter(|| { |
| 46 | + let ( |
| 47 | + index_first_attribute, |
| 48 | + index_second_attribute, |
| 49 | + index_third_attribute, |
| 50 | + index_forth_attribute, |
| 51 | + ) = indices_iter.next().unwrap(); |
| 52 | + noop_counter.add( |
| 53 | + 1, |
| 54 | + &[ |
| 55 | + KeyValue::new("attribute1", attribute_values[*index_first_attribute]), |
| 56 | + KeyValue::new("attribute2", attribute_values[*index_second_attribute]), |
| 57 | + KeyValue::new("attribute3", attribute_values[*index_third_attribute]), |
| 58 | + KeyValue::new("attribute4", attribute_values[*index_forth_attribute]), |
| 59 | + ], |
| 60 | + ); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + c.bench_function("Create_KeyValue", |b| { |
| 65 | + let mut indices_iter = random_indices.iter().cycle(); |
| 66 | + b.iter(|| { |
| 67 | + let ( |
| 68 | + index_first_attribute, |
| 69 | + index_second_attribute, |
| 70 | + index_third_attribute, |
| 71 | + index_forth_attribute, |
| 72 | + ) = indices_iter.next().unwrap(); |
| 73 | + let _ = vec![ |
| 74 | + KeyValue::new("attribute1", attribute_values[*index_first_attribute]), |
| 75 | + KeyValue::new("attribute2", attribute_values[*index_second_attribute]), |
| 76 | + KeyValue::new("attribute3", attribute_values[*index_third_attribute]), |
| 77 | + KeyValue::new("attribute4", attribute_values[*index_forth_attribute]), |
| 78 | + ]; |
| 79 | + }); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +criterion_group!(benches, criterion_benchmark); |
| 84 | + |
| 85 | +criterion_main!(benches); |
0 commit comments