1111// See the License for the specific language governing permissions and
1212// limitations under the License.
1313
14- #![ feature( test) ]
15-
16- extern crate test;
17-
14+ use criterion:: { criterion_group, criterion_main, Criterion } ;
15+ use prometheus:: { Counter , CounterVec , IntCounter , Opts } ;
1816use std:: collections:: HashMap ;
1917use std:: sync:: { atomic, Arc } ;
2018use std:: thread;
2119
22- use prometheus:: { Counter , CounterVec , IntCounter , Opts } ;
23- use test:: Bencher ;
24-
25- #[ bench]
26- fn bench_counter_with_label_values ( b : & mut Bencher ) {
20+ fn bench_counter_with_label_values ( c : & mut Criterion ) {
2721 let counter = CounterVec :: new (
2822 Opts :: new ( "benchmark_counter" , "A counter to benchmark it." ) ,
2923 & [ "one" , "two" , "three" ] ,
3024 )
3125 . unwrap ( ) ;
32- b. iter ( || counter. with_label_values ( & [ "eins" , "zwei" , "drei" ] ) . inc ( ) )
26+ c. bench_function ( "counter_with_label_values" , |b| {
27+ b. iter ( || counter. with_label_values ( & [ "eins" , "zwei" , "drei" ] ) . inc ( ) )
28+ } ) ;
3329}
3430
35- #[ bench]
36- fn bench_counter_with_mapped_labels ( b : & mut Bencher ) {
31+ fn bench_counter_with_mapped_labels ( c : & mut Criterion ) {
3732 let counter = CounterVec :: new (
3833 Opts :: new ( "benchmark_counter" , "A counter to benchmark it." ) ,
3934 & [ "one" , "two" , "three" ] ,
4035 )
4136 . unwrap ( ) ;
4237
43- b. iter ( || {
44- let mut labels = HashMap :: with_capacity ( 3 ) ;
45- labels. insert ( "two" , "zwei" ) ;
46- labels. insert ( "one" , "eins" ) ;
47- labels. insert ( "three" , "drei" ) ;
48- counter. with ( & labels) . inc ( ) ;
49- } )
38+ c. bench_function ( "counter_with_mapped_labels" , |b| {
39+ b. iter ( || {
40+ let mut labels = HashMap :: with_capacity ( 3 ) ;
41+ labels. insert ( "two" , "zwei" ) ;
42+ labels. insert ( "one" , "eins" ) ;
43+ labels. insert ( "three" , "drei" ) ;
44+ counter. with ( & labels) . inc ( ) ;
45+ } )
46+ } ) ;
5047}
5148
52- #[ bench]
53- fn bench_counter_with_prepared_mapped_labels ( b : & mut Bencher ) {
49+ fn bench_counter_with_prepared_mapped_labels ( c : & mut Criterion ) {
5450 let counter = CounterVec :: new (
5551 Opts :: new ( "benchmark_counter" , "A counter to benchmark it." ) ,
5652 & [ "one" , "two" , "three" ] ,
@@ -62,25 +58,24 @@ fn bench_counter_with_prepared_mapped_labels(b: &mut Bencher) {
6258 labels. insert ( "one" , "eins" ) ;
6359 labels. insert ( "three" , "drei" ) ;
6460
65- b. iter ( || {
66- counter. with ( & labels) . inc ( ) ;
67- } )
61+ c. bench_function ( "counter_with_prepared_mapped_labels" , |b| {
62+ b. iter ( || {
63+ counter. with ( & labels) . inc ( ) ;
64+ } )
65+ } ) ;
6866}
6967
70- #[ bench]
71- fn bench_counter_no_labels ( b : & mut Bencher ) {
68+ fn bench_counter_no_labels ( c : & mut Criterion ) {
7269 let counter = Counter :: new ( "benchmark_counter" , "A counter to benchmark." ) . unwrap ( ) ;
73- b . iter ( || counter. inc ( ) )
70+ c . bench_function ( "counter_no_labels" , |b| b . iter ( || counter. inc ( ) ) ) ;
7471}
7572
76- #[ bench]
77- fn bench_int_counter_no_labels ( b : & mut Bencher ) {
73+ fn bench_int_counter_no_labels ( c : & mut Criterion ) {
7874 let counter = IntCounter :: new ( "benchmark_int_counter" , "A int_counter to benchmark." ) . unwrap ( ) ;
79- b . iter ( || counter. inc ( ) )
75+ c . bench_function ( "int_counter_no_labels" , |b| b . iter ( || counter. inc ( ) ) ) ;
8076}
8177
82- #[ bench]
83- fn bench_counter_no_labels_concurrent_nop ( b : & mut Bencher ) {
78+ fn bench_counter_no_labels_concurrent_nop ( c : & mut Criterion ) {
8479 let signal_exit = Arc :: new ( atomic:: AtomicBool :: new ( false ) ) ;
8580 let counter = Counter :: new ( "foo" , "bar" ) . unwrap ( ) ;
8681
@@ -95,7 +90,9 @@ fn bench_counter_no_labels_concurrent_nop(b: &mut Bencher) {
9590 } )
9691 . collect ( ) ;
9792
98- b. iter ( || counter. inc ( ) ) ;
93+ c. bench_function ( "counter_no_labels_concurrent_nop" , |b| {
94+ b. iter ( || counter. inc ( ) ) ;
95+ } ) ;
9996
10097 // Wait for accompanying thread to exit.
10198 signal_exit. store ( true , atomic:: Ordering :: Relaxed ) ;
@@ -104,8 +101,7 @@ fn bench_counter_no_labels_concurrent_nop(b: &mut Bencher) {
104101 }
105102}
106103
107- #[ bench]
108- fn bench_counter_no_labels_concurrent_write ( b : & mut Bencher ) {
104+ fn bench_counter_no_labels_concurrent_write ( c : & mut Criterion ) {
109105 let signal_exit = Arc :: new ( atomic:: AtomicBool :: new ( false ) ) ;
110106 let counter = Counter :: new ( "foo" , "bar" ) . unwrap ( ) ;
111107
@@ -122,7 +118,9 @@ fn bench_counter_no_labels_concurrent_write(b: &mut Bencher) {
122118 } )
123119 . collect ( ) ;
124120
125- b. iter ( || counter. inc ( ) ) ;
121+ c. bench_function ( "counter_no_labels_concurrent_write" , |b| {
122+ b. iter ( || counter. inc ( ) ) ;
123+ } ) ;
126124
127125 // Wait for accompanying thread to exit.
128126 signal_exit. store ( true , atomic:: Ordering :: Relaxed ) ;
@@ -131,8 +129,7 @@ fn bench_counter_no_labels_concurrent_write(b: &mut Bencher) {
131129 }
132130}
133131
134- #[ bench]
135- fn bench_int_counter_no_labels_concurrent_write ( b : & mut Bencher ) {
132+ fn bench_int_counter_no_labels_concurrent_write ( c : & mut Criterion ) {
136133 let signal_exit = Arc :: new ( atomic:: AtomicBool :: new ( false ) ) ;
137134 let counter = IntCounter :: new ( "foo" , "bar" ) . unwrap ( ) ;
138135
@@ -149,7 +146,9 @@ fn bench_int_counter_no_labels_concurrent_write(b: &mut Bencher) {
149146 } )
150147 . collect ( ) ;
151148
152- b. iter ( || counter. inc ( ) ) ;
149+ c. bench_function ( "int_counter_no_labels_concurrent_write" , |b| {
150+ b. iter ( || counter. inc ( ) ) ;
151+ } ) ;
153152
154153 // Wait for accompanying thread to exit.
155154 signal_exit. store ( true , atomic:: Ordering :: Relaxed ) ;
@@ -158,8 +157,7 @@ fn bench_int_counter_no_labels_concurrent_write(b: &mut Bencher) {
158157 }
159158}
160159
161- #[ bench]
162- fn bench_counter_with_label_values_concurrent_write ( b : & mut Bencher ) {
160+ fn bench_counter_with_label_values_concurrent_write ( c : & mut Criterion ) {
163161 let signal_exit = Arc :: new ( atomic:: AtomicBool :: new ( false ) ) ;
164162 let counter = CounterVec :: new ( Opts :: new ( "foo" , "bar" ) , & [ "one" , "two" , "three" ] ) . unwrap ( ) ;
165163
@@ -175,11 +173,27 @@ fn bench_counter_with_label_values_concurrent_write(b: &mut Bencher) {
175173 } )
176174 . collect ( ) ;
177175
178- b. iter ( || counter. with_label_values ( & [ "eins" , "zwei" , "drei" ] ) . inc ( ) ) ;
176+ c. bench_function ( "counter_with_label_values_concurrent_write" , |b| {
177+ b. iter ( || counter. with_label_values ( & [ "eins" , "zwei" , "drei" ] ) . inc ( ) ) ;
178+ } ) ;
179179
180180 // Wait for accompanying thread to exit.
181181 signal_exit. store ( true , atomic:: Ordering :: Relaxed ) ;
182182 for h in thread_handles {
183183 h. join ( ) . unwrap ( ) ;
184184 }
185185}
186+
187+ criterion_group ! (
188+ benches,
189+ bench_counter_no_labels,
190+ bench_counter_no_labels_concurrent_nop,
191+ bench_counter_no_labels_concurrent_write,
192+ bench_counter_with_label_values,
193+ bench_counter_with_label_values_concurrent_write,
194+ bench_counter_with_mapped_labels,
195+ bench_counter_with_prepared_mapped_labels,
196+ bench_int_counter_no_labels,
197+ bench_int_counter_no_labels_concurrent_write,
198+ ) ;
199+ criterion_main ! ( benches) ;
0 commit comments