|
1 | 1 | /*
|
2 | 2 | The benchmark results:
|
3 | 3 | criterion = "0.5.1"
|
4 |
| - rustc 1.82.0 (f6e511eec 2024-10-15) |
| 4 | + rustc 1.83.0 (90b35a623 2024-11-26) |
5 | 5 | OS: Ubuntu 22.04.4 LTS (5.15.167.4-microsoft-standard-WSL2)
|
6 |
| - Hardware: AMD EPYC 7763 64-Core Processor - 2.44 GHz, 16vCPUs, |
| 6 | + Hardware: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz 2.79 GHz |
7 | 7 | RAM: 64.0 GB
|
8 |
| - | Test | Average time| |
9 |
| - |--------------------------------|-------------| |
10 |
| - | Histogram_Record | 225.04 ns | |
| 8 | + | Test | Average time| |
| 9 | + |-------------------------------------------------------|-------------| |
| 10 | + | Histogram_Record | 186.24 ns | |
| 11 | + | Histogram_Record_With_Non_Static_Values | 264.70 ns | |
11 | 12 |
|
12 | 13 | */
|
13 | 14 |
|
14 |
| -use criterion::{criterion_group, criterion_main, Criterion}; |
| 15 | +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; |
15 | 16 | use opentelemetry::{
|
16 | 17 | metrics::{Histogram, MeterProvider as _},
|
17 | 18 | KeyValue,
|
@@ -48,36 +49,97 @@ fn create_histogram(name: &'static str) -> Histogram<u64> {
|
48 | 49 |
|
49 | 50 | fn criterion_benchmark(c: &mut Criterion) {
|
50 | 51 | histogram_record(c);
|
| 52 | + |
| 53 | + let attribute_values: [String; 10] = (1..=10) |
| 54 | + .map(|i| format!("value{}", i)) |
| 55 | + .collect::<Vec<String>>() |
| 56 | + .try_into() |
| 57 | + .expect("Expected a Vec of length 10"); |
| 58 | + |
| 59 | + histogram_record_with_non_static_values(c, attribute_values); |
51 | 60 | }
|
52 | 61 |
|
53 | 62 | fn histogram_record(c: &mut Criterion) {
|
54 | 63 | let histogram = create_histogram("Histogram_Record");
|
55 | 64 | c.bench_function("Histogram_Record", |b| {
|
56 |
| - b.iter(|| { |
57 |
| - // 4*4*10*10 = 1600 time series. |
58 |
| - let rands = CURRENT_RNG.with(|rng| { |
59 |
| - let mut rng = rng.borrow_mut(); |
60 |
| - [ |
61 |
| - rng.gen_range(0..4), |
62 |
| - rng.gen_range(0..4), |
63 |
| - rng.gen_range(0..10), |
64 |
| - rng.gen_range(0..10), |
65 |
| - ] |
66 |
| - }); |
67 |
| - let index_first_attribute = rands[0]; |
68 |
| - let index_second_attribute = rands[1]; |
69 |
| - let index_third_attribute = rands[2]; |
70 |
| - let index_fourth_attribute = rands[3]; |
71 |
| - histogram.record( |
72 |
| - 1, |
73 |
| - &[ |
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_fourth_attribute]), |
78 |
| - ], |
79 |
| - ); |
80 |
| - }); |
| 65 | + b.iter_batched( |
| 66 | + || { |
| 67 | + // 4*4*10*10 = 1600 time series. |
| 68 | + CURRENT_RNG.with(|rng| { |
| 69 | + let mut rng = rng.borrow_mut(); |
| 70 | + [ |
| 71 | + rng.gen_range(0..4), |
| 72 | + rng.gen_range(0..4), |
| 73 | + rng.gen_range(0..10), |
| 74 | + rng.gen_range(0..10), |
| 75 | + ] |
| 76 | + }) |
| 77 | + }, |
| 78 | + |rands| { |
| 79 | + let index_first_attribute = rands[0]; |
| 80 | + let index_second_attribute = rands[1]; |
| 81 | + let index_third_attribute = rands[2]; |
| 82 | + let index_fourth_attribute = rands[3]; |
| 83 | + histogram.record( |
| 84 | + 1, |
| 85 | + &[ |
| 86 | + KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]), |
| 87 | + KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]), |
| 88 | + KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]), |
| 89 | + KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]), |
| 90 | + ], |
| 91 | + ); |
| 92 | + }, |
| 93 | + BatchSize::SmallInput, |
| 94 | + ); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +fn histogram_record_with_non_static_values(c: &mut Criterion, attribute_values: [String; 10]) { |
| 99 | + let histogram = create_histogram("Histogram_Record_With_Non_Static_Values"); |
| 100 | + c.bench_function("Histogram_Record_With_Non_Static_Values", |b| { |
| 101 | + b.iter_batched( |
| 102 | + || { |
| 103 | + // 4*4*10*10 = 1600 time series. |
| 104 | + CURRENT_RNG.with(|rng| { |
| 105 | + let mut rng = rng.borrow_mut(); |
| 106 | + [ |
| 107 | + rng.gen_range(0..4), |
| 108 | + rng.gen_range(0..4), |
| 109 | + rng.gen_range(0..10), |
| 110 | + rng.gen_range(0..10), |
| 111 | + ] |
| 112 | + }) |
| 113 | + }, |
| 114 | + |rands| { |
| 115 | + let index_first_attribute = rands[0]; |
| 116 | + let index_second_attribute = rands[1]; |
| 117 | + let index_third_attribute = rands[2]; |
| 118 | + let index_fourth_attribute = rands[3]; |
| 119 | + histogram.record( |
| 120 | + 1, |
| 121 | + &[ |
| 122 | + KeyValue::new( |
| 123 | + "attribute1", |
| 124 | + attribute_values[index_first_attribute].as_str().to_owned(), |
| 125 | + ), |
| 126 | + KeyValue::new( |
| 127 | + "attribute2", |
| 128 | + attribute_values[index_second_attribute].as_str().to_owned(), |
| 129 | + ), |
| 130 | + KeyValue::new( |
| 131 | + "attribute3", |
| 132 | + attribute_values[index_third_attribute].as_str().to_owned(), |
| 133 | + ), |
| 134 | + KeyValue::new( |
| 135 | + "attribute4", |
| 136 | + attribute_values[index_fourth_attribute].as_str().to_owned(), |
| 137 | + ), |
| 138 | + ], |
| 139 | + ); |
| 140 | + }, |
| 141 | + BatchSize::SmallInput, |
| 142 | + ); |
81 | 143 | });
|
82 | 144 | }
|
83 | 145 |
|
|
0 commit comments