|
12 | 12 | | ThreadLocal_Random_Generator_5 | 37 ns | |
13 | 13 | */ |
14 | 14 |
|
15 | | -use criterion::{criterion_group, criterion_main, Criterion}; |
| 15 | +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; |
16 | 16 | use opentelemetry::{ |
17 | 17 | metrics::{Counter, MeterProvider as _}, |
18 | 18 | KeyValue, |
@@ -57,62 +57,72 @@ fn criterion_benchmark(c: &mut Criterion) { |
57 | 57 | fn counter_add_sorted(c: &mut Criterion) { |
58 | 58 | let counter = create_counter("Counter_Add_Sorted"); |
59 | 59 | c.bench_function("Counter_Add_Sorted", |b| { |
60 | | - b.iter(|| { |
61 | | - // 4*4*10*10 = 1600 time series. |
62 | | - let rands = CURRENT_RNG.with(|rng| { |
63 | | - let mut rng = rng.borrow_mut(); |
64 | | - [ |
65 | | - rng.gen_range(0..4), |
66 | | - rng.gen_range(0..4), |
67 | | - rng.gen_range(0..10), |
68 | | - rng.gen_range(0..10), |
69 | | - ] |
70 | | - }); |
71 | | - let index_first_attribute = rands[0]; |
72 | | - let index_second_attribute = rands[1]; |
73 | | - let index_third_attribute = rands[2]; |
74 | | - let index_fourth_attribute = rands[3]; |
75 | | - counter.add( |
76 | | - 1, |
77 | | - &[ |
78 | | - KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]), |
79 | | - KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]), |
80 | | - KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]), |
81 | | - KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]), |
82 | | - ], |
83 | | - ); |
84 | | - }); |
| 60 | + b.iter_batched( |
| 61 | + || { |
| 62 | + // 4*4*10*10 = 1600 time series. |
| 63 | + CURRENT_RNG.with(|rng| { |
| 64 | + let mut rng = rng.borrow_mut(); |
| 65 | + [ |
| 66 | + rng.gen_range(0..4), |
| 67 | + rng.gen_range(0..4), |
| 68 | + rng.gen_range(0..10), |
| 69 | + rng.gen_range(0..10), |
| 70 | + ] |
| 71 | + }) |
| 72 | + }, |
| 73 | + |rands| { |
| 74 | + let index_first_attribute = rands[0]; |
| 75 | + let index_second_attribute = rands[1]; |
| 76 | + let index_third_attribute = rands[2]; |
| 77 | + let index_fourth_attribute = rands[3]; |
| 78 | + counter.add( |
| 79 | + 1, |
| 80 | + &[ |
| 81 | + KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]), |
| 82 | + KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]), |
| 83 | + KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]), |
| 84 | + KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]), |
| 85 | + ], |
| 86 | + ); |
| 87 | + }, |
| 88 | + BatchSize::LargeInput, |
| 89 | + ); |
85 | 90 | }); |
86 | 91 | } |
87 | 92 |
|
88 | 93 | fn counter_add_unsorted(c: &mut Criterion) { |
89 | 94 | let counter = create_counter("Counter_Add_Unsorted"); |
90 | 95 | c.bench_function("Counter_Add_Unsorted", |b| { |
91 | | - b.iter(|| { |
92 | | - // 4*4*10*10 = 1600 time series. |
93 | | - let rands = CURRENT_RNG.with(|rng| { |
94 | | - let mut rng = rng.borrow_mut(); |
95 | | - [ |
96 | | - rng.gen_range(0..4), |
97 | | - rng.gen_range(0..4), |
98 | | - rng.gen_range(0..10), |
99 | | - rng.gen_range(0..10), |
100 | | - ] |
101 | | - }); |
102 | | - let index_first_attribute = rands[0]; |
103 | | - let index_second_attribute = rands[1]; |
104 | | - let index_third_attribute = rands[2]; |
105 | | - let index_fourth_attribute = rands[3]; |
106 | | - counter.add( |
107 | | - 1, |
108 | | - &[ |
109 | | - KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]), |
110 | | - KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]), |
111 | | - KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]), |
112 | | - KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]), |
113 | | - ], |
114 | | - ); |
115 | | - }); |
| 96 | + b.iter_batched( |
| 97 | + || { |
| 98 | + // 4*4*10*10 = 1600 time series. |
| 99 | + CURRENT_RNG.with(|rng| { |
| 100 | + let mut rng = rng.borrow_mut(); |
| 101 | + [ |
| 102 | + rng.gen_range(0..4), |
| 103 | + rng.gen_range(0..4), |
| 104 | + rng.gen_range(0..10), |
| 105 | + rng.gen_range(0..10), |
| 106 | + ] |
| 107 | + }) |
| 108 | + }, |
| 109 | + |rands| { |
| 110 | + let index_first_attribute = rands[0]; |
| 111 | + let index_second_attribute = rands[1]; |
| 112 | + let index_third_attribute = rands[2]; |
| 113 | + let index_fourth_attribute = rands[3]; |
| 114 | + counter.add( |
| 115 | + 1, |
| 116 | + &[ |
| 117 | + KeyValue::new("attribute2", ATTRIBUTE_VALUES[index_second_attribute]), |
| 118 | + KeyValue::new("attribute3", ATTRIBUTE_VALUES[index_third_attribute]), |
| 119 | + KeyValue::new("attribute1", ATTRIBUTE_VALUES[index_first_attribute]), |
| 120 | + KeyValue::new("attribute4", ATTRIBUTE_VALUES[index_fourth_attribute]), |
| 121 | + ], |
| 122 | + ); |
| 123 | + }, |
| 124 | + BatchSize::LargeInput, |
| 125 | + ); |
116 | 126 | }); |
117 | 127 | } |
118 | 128 |
|
|
0 commit comments