|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | +use opentelemetry::{ |
| 3 | + metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _}, |
| 4 | + KeyValue, |
| 5 | +}; |
| 6 | + |
| 7 | +fn create_counter() -> Counter<u64> { |
| 8 | + let meter_provider: NoopMeterProvider = NoopMeterProvider::default(); |
| 9 | + let meter = meter_provider.meter("benchmarks"); |
| 10 | + let counter = meter.u64_counter("counter_bench").init(); |
| 11 | + counter |
| 12 | +} |
| 13 | + |
| 14 | +fn criterion_benchmark(c: &mut Criterion) { |
| 15 | + noop_counter_add(c); |
| 16 | +} |
| 17 | + |
| 18 | +fn noop_counter_add(c: &mut Criterion) { |
| 19 | + let noop_counter = create_counter(); |
| 20 | + |
| 21 | + c.bench_function("NoopCounter_NoAttributes", |b| { |
| 22 | + b.iter(|| { |
| 23 | + noop_counter.add(1, &[]); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + c.bench_function("NoopCounter_AddWithInlineStaticAttributes", |b| { |
| 28 | + b.iter(|| { |
| 29 | + noop_counter.add( |
| 30 | + 1, |
| 31 | + &[ |
| 32 | + KeyValue::new("attribute1", "value1"), |
| 33 | + KeyValue::new("attribute2", "value2"), |
| 34 | + KeyValue::new("attribute3", "value3"), |
| 35 | + KeyValue::new("attribute4", "value4"), |
| 36 | + ], |
| 37 | + ); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + c.bench_function("NoopCounter_AddWithStaticArray", |b| { |
| 42 | + b.iter(|| { |
| 43 | + let kv = [ |
| 44 | + KeyValue::new("attribute1", "value1"), |
| 45 | + KeyValue::new("attribute2", "value2"), |
| 46 | + KeyValue::new("attribute3", "value3"), |
| 47 | + KeyValue::new("attribute4", "value4"), |
| 48 | + ]; |
| 49 | + |
| 50 | + noop_counter.add(1, &kv); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + c.bench_function("NoopCounter_AddWithDynamicAttributes", |b| { |
| 55 | + b.iter(|| { |
| 56 | + let kv = vec![ |
| 57 | + KeyValue::new("attribute1", "value1"), |
| 58 | + KeyValue::new("attribute2", "value2"), |
| 59 | + KeyValue::new("attribute3", "value3"), |
| 60 | + KeyValue::new("attribute4", "value4"), |
| 61 | + ]; |
| 62 | + |
| 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 = 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 = vec![ |
| 83 | + ("attribute1", "value1"), |
| 84 | + ("attribute2", "value2"), |
| 85 | + ("attribute3", "value3"), |
| 86 | + ("attribute4", "value4"), |
| 87 | + ]; |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +criterion_group!(benches, criterion_benchmark); |
| 93 | + |
| 94 | +criterion_main!(benches); |
0 commit comments