@@ -5,22 +5,24 @@ extern crate rand;
55extern crate test;
66
77use hdrsample:: * ;
8- use self :: rand:: Rng ;
98use self :: test:: Bencher ;
109
10+ use self :: rand_varint:: * ;
11+
12+ #[ path = "../src/serialization/rand_varint.rs" ]
13+ mod rand_varint;
14+
1115#[ bench]
1216fn record_precalc_random_values_with_1_count_u64 ( b : & mut Bencher ) {
1317 let mut h = Histogram :: < u64 > :: new_with_bounds ( 1 , u64:: max_value ( ) , 3 ) . unwrap ( ) ;
1418 let mut indices = Vec :: < u64 > :: new ( ) ;
15- // TODO improve this and similar benchmarks to use a non-uniform distribution (like that used
16- // in serialization tests) so we're not always recording in the top few buckets
1719 let mut rng = rand:: weak_rng ( ) ;
1820
1921 // same value approach as record_precalc_random_values_with_max_count_u64 so that they are
2022 // comparable
2123
22- for _ in 0 .. 1000_000 {
23- indices. push ( rng . gen ( ) ) ;
24+ for v in RandomVarintEncodedLengthIter :: new ( & mut rng ) . take ( 1_000_000 ) {
25+ indices. push ( v ) ;
2426 }
2527
2628 b. iter ( || {
@@ -39,10 +41,9 @@ fn record_precalc_random_values_with_max_count_u64(b: &mut Bencher) {
3941
4042 // store values in an array and re-use so we can be sure to hit the overflow case
4143
42- for _ in 0 ..1000_000 {
43- let r = rng. gen ( ) ;
44- indices. push ( r) ;
45- h. record_n ( r, u64:: max_value ( ) ) . unwrap ( ) ;
44+ for v in RandomVarintEncodedLengthIter :: new ( & mut rng) . take ( 1_000_000 ) {
45+ indices. push ( v) ;
46+ h. record_n ( v, u64:: max_value ( ) ) . unwrap ( ) ;
4647 }
4748
4849 b. iter ( || {
@@ -59,8 +60,8 @@ fn record_correct_precalc_random_values_with_1_count_u64(b: &mut Bencher) {
5960 let mut indices = Vec :: < u64 > :: new ( ) ;
6061 let mut rng = rand:: weak_rng ( ) ;
6162
62- for _ in 0 .. 10_000 {
63- indices. push ( rng . gen ( ) ) ;
63+ for v in RandomVarintEncodedLengthIter :: new ( & mut rng ) . take ( 10_000 ) {
64+ indices. push ( v ) ;
6465 }
6566
6667 b. iter ( || {
@@ -79,8 +80,10 @@ fn record_random_values_with_1_count_u64(b: &mut Bencher) {
7980 // This should be *slower* than the benchmarks above where we pre-calculate the values
8081 // outside of the hot loop. If it isn't, then those measurements are likely spurious.
8182
82- b. iter ( || for _ in 0 ..1000_000 {
83- h. record ( rng. gen ( ) ) . unwrap ( )
83+ b. iter ( || {
84+ for v in RandomVarintEncodedLengthIter :: new ( & mut rng) . take ( 1_000_000 ) {
85+ h. record ( v) . unwrap ( )
86+ }
8487 } )
8588}
8689
@@ -136,11 +139,10 @@ fn do_subtract_benchmark<F: Fn() -> Histogram<u64>>(
136139 for _ in 0 ..1000 {
137140 let mut h = addend_factory ( ) ;
138141
139- for _ in 0 ..1000 {
140- let r = rng. gen ( ) ;
141- h. record_n ( r, count_at_each_addend_value) . unwrap ( ) ;
142+ for v in RandomVarintEncodedLengthIter :: new ( & mut rng) . take ( 1_000 ) {
143+ h. record_n ( v, count_at_each_addend_value) . unwrap ( ) ;
142144 // ensure there's a count to subtract from
143- accum. record_n ( r , count_at_each_addend_value) . unwrap ( ) ;
145+ accum. record_n ( v , count_at_each_addend_value) . unwrap ( ) ;
144146 }
145147
146148 subtrahends. push ( h) ;
@@ -166,15 +168,16 @@ fn do_add_benchmark<F: Fn() -> Histogram<u64>>(
166168 for _ in 0 ..1000 {
167169 let mut h = addend_factory ( ) ;
168170
169- for _ in 0 ..1000 {
170- let r = rng. gen ( ) ;
171- h. record_n ( r, count_at_each_addend_value) . unwrap ( ) ;
171+ for v in RandomVarintEncodedLengthIter :: new ( & mut rng) . take ( 1_000 ) {
172+ h. record_n ( v, count_at_each_addend_value) . unwrap ( ) ;
172173 }
173174
174175 addends. push ( h) ;
175176 }
176177
177- b. iter ( || for h in addends. iter ( ) {
178- accum. add ( h) . unwrap ( ) ;
178+ b. iter ( || {
179+ for h in addends. iter ( ) {
180+ accum. add ( h) . unwrap ( ) ;
181+ }
179182 } )
180183}
0 commit comments