|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | +use opentelemetry::{ |
| 3 | + metrics::{noop::NoopMeterProvider, Counter, MeterProvider as _}, |
| 4 | + KeyValue, |
| 5 | +}; |
| 6 | + |
| 7 | +// Run this benchmark with: |
| 8 | +// cargo bench --bench metrics --features=metrics |
| 9 | + |
| 10 | +fn create_counter() -> Counter<u64> { |
| 11 | + let meter_provider: NoopMeterProvider = NoopMeterProvider::default(); |
| 12 | + let meter = meter_provider.meter("benchmarks"); |
| 13 | + let counter = meter.u64_counter("counter_bench").init(); |
| 14 | + counter |
| 15 | +} |
| 16 | + |
| 17 | +fn criterion_benchmark(c: &mut Criterion) { |
| 18 | + counter_add(c); |
| 19 | +} |
| 20 | + |
| 21 | +fn counter_add(c: &mut Criterion) { |
| 22 | + let counter = create_counter(); |
| 23 | + |
| 24 | + c.bench_function("Counter_NoAttributes", |b| { |
| 25 | + b.iter(|| { |
| 26 | + counter.add(1, &[]); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + c.bench_function("Counter_AddWithInlineStaticAttributes", |b| { |
| 31 | + b.iter(|| { |
| 32 | + counter.add( |
| 33 | + 1, |
| 34 | + &[ |
| 35 | + KeyValue::new("attribute1", "value1"), |
| 36 | + KeyValue::new("attribute2", "value2"), |
| 37 | + KeyValue::new("attribute3", "value3"), |
| 38 | + KeyValue::new("attribute4", "value4"), |
| 39 | + ], |
| 40 | + ); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + c.bench_function("Counter_AddWithStaticArray", |b| { |
| 45 | + b.iter(|| { |
| 46 | + let kv = [ |
| 47 | + KeyValue::new("attribute1", "value1"), |
| 48 | + KeyValue::new("attribute2", "value2"), |
| 49 | + KeyValue::new("attribute3", "value3"), |
| 50 | + KeyValue::new("attribute4", "value4"), |
| 51 | + ]; |
| 52 | + |
| 53 | + counter.add(1, &kv); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + c.bench_function("Counter_AddWithDynamicAttributes", |b| { |
| 58 | + b.iter(|| { |
| 59 | + let kv = vec![ |
| 60 | + KeyValue::new("attribute1", "value1"), |
| 61 | + KeyValue::new("attribute2", "value2"), |
| 62 | + KeyValue::new("attribute3", "value3"), |
| 63 | + KeyValue::new("attribute4", "value4"), |
| 64 | + ]; |
| 65 | + |
| 66 | + counter.add(1, &kv); |
| 67 | + }); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +criterion_group!(benches, criterion_benchmark); |
| 72 | + |
| 73 | +criterion_main!(benches); |
0 commit comments