]>
Commit | Line | Data |
---|---|---|
0731742a XL |
1 | // Copyright 2018 Developers of the Rand project. |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | |
6 | // option. This file may not be copied, modified, or distributed | |
7 | // except according to those terms. | |
8 | ||
b7449926 | 9 | #![feature(test)] |
416331ca | 10 | #![allow(non_snake_case)] |
b7449926 XL |
11 | |
12 | extern crate test; | |
b7449926 XL |
13 | |
14 | const RAND_BENCH_N: u64 = 1000; | |
15 | const BYTES_LEN: usize = 1024; | |
16 | ||
17 | use std::mem::size_of; | |
18 | use test::{black_box, Bencher}; | |
19 | ||
20 | use rand::prelude::*; | |
b7449926 | 21 | use rand::rngs::adapter::ReseedingRng; |
dfeec247 XL |
22 | use rand::rngs::{mock::StepRng, OsRng}; |
23 | use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng}; | |
24 | use rand_hc::Hc128Rng; | |
416331ca | 25 | use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg}; |
b7449926 XL |
26 | |
27 | macro_rules! gen_bytes { | |
28 | ($fnn:ident, $gen:expr) => { | |
29 | #[bench] | |
30 | fn $fnn(b: &mut Bencher) { | |
31 | let mut rng = $gen; | |
32 | let mut buf = [0u8; BYTES_LEN]; | |
33 | b.iter(|| { | |
34 | for _ in 0..RAND_BENCH_N { | |
35 | rng.fill_bytes(&mut buf); | |
36 | black_box(buf); | |
37 | } | |
38 | }); | |
39 | b.bytes = BYTES_LEN as u64 * RAND_BENCH_N; | |
40 | } | |
dfeec247 | 41 | }; |
b7449926 XL |
42 | } |
43 | ||
416331ca | 44 | gen_bytes!(gen_bytes_step, StepRng::new(0, 1)); |
416331ca XL |
45 | gen_bytes!(gen_bytes_pcg32, Pcg32::from_entropy()); |
46 | gen_bytes!(gen_bytes_pcg64, Pcg64::from_entropy()); | |
47 | gen_bytes!(gen_bytes_pcg64mcg, Pcg64Mcg::from_entropy()); | |
48 | gen_bytes!(gen_bytes_chacha8, ChaCha8Rng::from_entropy()); | |
49 | gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_entropy()); | |
50 | gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_entropy()); | |
b7449926 | 51 | gen_bytes!(gen_bytes_hc128, Hc128Rng::from_entropy()); |
b7449926 | 52 | gen_bytes!(gen_bytes_std, StdRng::from_entropy()); |
dfeec247 | 53 | #[cfg(feature = "small_rng")] |
b7449926 | 54 | gen_bytes!(gen_bytes_small, SmallRng::from_entropy()); |
416331ca | 55 | gen_bytes!(gen_bytes_os, OsRng); |
b7449926 XL |
56 | |
57 | macro_rules! gen_uint { | |
58 | ($fnn:ident, $ty:ty, $gen:expr) => { | |
59 | #[bench] | |
60 | fn $fnn(b: &mut Bencher) { | |
61 | let mut rng = $gen; | |
62 | b.iter(|| { | |
63 | let mut accum: $ty = 0; | |
64 | for _ in 0..RAND_BENCH_N { | |
65 | accum = accum.wrapping_add(rng.gen::<$ty>()); | |
66 | } | |
67 | accum | |
68 | }); | |
69 | b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; | |
70 | } | |
dfeec247 | 71 | }; |
b7449926 XL |
72 | } |
73 | ||
416331ca | 74 | gen_uint!(gen_u32_step, u32, StepRng::new(0, 1)); |
416331ca XL |
75 | gen_uint!(gen_u32_pcg32, u32, Pcg32::from_entropy()); |
76 | gen_uint!(gen_u32_pcg64, u32, Pcg64::from_entropy()); | |
77 | gen_uint!(gen_u32_pcg64mcg, u32, Pcg64Mcg::from_entropy()); | |
78 | gen_uint!(gen_u32_chacha8, u32, ChaCha8Rng::from_entropy()); | |
79 | gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_entropy()); | |
80 | gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_entropy()); | |
b7449926 | 81 | gen_uint!(gen_u32_hc128, u32, Hc128Rng::from_entropy()); |
b7449926 | 82 | gen_uint!(gen_u32_std, u32, StdRng::from_entropy()); |
dfeec247 | 83 | #[cfg(feature = "small_rng")] |
b7449926 | 84 | gen_uint!(gen_u32_small, u32, SmallRng::from_entropy()); |
416331ca | 85 | gen_uint!(gen_u32_os, u32, OsRng); |
b7449926 | 86 | |
416331ca | 87 | gen_uint!(gen_u64_step, u64, StepRng::new(0, 1)); |
416331ca XL |
88 | gen_uint!(gen_u64_pcg32, u64, Pcg32::from_entropy()); |
89 | gen_uint!(gen_u64_pcg64, u64, Pcg64::from_entropy()); | |
90 | gen_uint!(gen_u64_pcg64mcg, u64, Pcg64Mcg::from_entropy()); | |
91 | gen_uint!(gen_u64_chacha8, u64, ChaCha8Rng::from_entropy()); | |
92 | gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_entropy()); | |
93 | gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_entropy()); | |
b7449926 | 94 | gen_uint!(gen_u64_hc128, u64, Hc128Rng::from_entropy()); |
b7449926 | 95 | gen_uint!(gen_u64_std, u64, StdRng::from_entropy()); |
dfeec247 | 96 | #[cfg(feature = "small_rng")] |
b7449926 | 97 | gen_uint!(gen_u64_small, u64, SmallRng::from_entropy()); |
416331ca | 98 | gen_uint!(gen_u64_os, u64, OsRng); |
b7449926 XL |
99 | |
100 | macro_rules! init_gen { | |
101 | ($fnn:ident, $gen:ident) => { | |
102 | #[bench] | |
103 | fn $fnn(b: &mut Bencher) { | |
dfeec247 | 104 | let mut rng = Pcg32::from_entropy(); |
b7449926 XL |
105 | b.iter(|| { |
106 | let r2 = $gen::from_rng(&mut rng).unwrap(); | |
107 | r2 | |
108 | }); | |
109 | } | |
dfeec247 | 110 | }; |
b7449926 XL |
111 | } |
112 | ||
416331ca XL |
113 | init_gen!(init_pcg32, Pcg32); |
114 | init_gen!(init_pcg64, Pcg64); | |
115 | init_gen!(init_pcg64mcg, Pcg64Mcg); | |
b7449926 | 116 | init_gen!(init_hc128, Hc128Rng); |
416331ca | 117 | init_gen!(init_chacha, ChaCha20Rng); |
b7449926 | 118 | |
416331ca XL |
119 | const RESEEDING_BYTES_LEN: usize = 1024 * 1024; |
120 | const RESEEDING_BENCH_N: u64 = 16; | |
b7449926 | 121 | |
416331ca XL |
122 | macro_rules! reseeding_bytes { |
123 | ($fnn:ident, $thresh:expr) => { | |
b7449926 XL |
124 | #[bench] |
125 | fn $fnn(b: &mut Bencher) { | |
dfeec247 | 126 | let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(), $thresh * 1024, OsRng); |
416331ca | 127 | let mut buf = [0u8; RESEEDING_BYTES_LEN]; |
b7449926 | 128 | b.iter(|| { |
416331ca XL |
129 | for _ in 0..RESEEDING_BENCH_N { |
130 | rng.fill_bytes(&mut buf); | |
131 | black_box(&buf); | |
b7449926 | 132 | } |
b7449926 | 133 | }); |
416331ca | 134 | b.bytes = RESEEDING_BYTES_LEN as u64 * RESEEDING_BENCH_N; |
b7449926 | 135 | } |
dfeec247 | 136 | }; |
b7449926 XL |
137 | } |
138 | ||
416331ca XL |
139 | reseeding_bytes!(reseeding_chacha20_4k, 4); |
140 | reseeding_bytes!(reseeding_chacha20_16k, 16); | |
141 | reseeding_bytes!(reseeding_chacha20_32k, 32); | |
142 | reseeding_bytes!(reseeding_chacha20_64k, 64); | |
143 | reseeding_bytes!(reseeding_chacha20_256k, 256); | |
144 | reseeding_bytes!(reseeding_chacha20_1M, 1024); | |
b7449926 XL |
145 | |
146 | ||
147 | macro_rules! threadrng_uint { | |
148 | ($fnn:ident, $ty:ty) => { | |
149 | #[bench] | |
150 | fn $fnn(b: &mut Bencher) { | |
151 | let mut rng = thread_rng(); | |
152 | b.iter(|| { | |
153 | let mut accum: $ty = 0; | |
154 | for _ in 0..RAND_BENCH_N { | |
155 | accum = accum.wrapping_add(rng.gen::<$ty>()); | |
156 | } | |
157 | accum | |
158 | }); | |
159 | b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; | |
160 | } | |
dfeec247 | 161 | }; |
b7449926 XL |
162 | } |
163 | ||
164 | threadrng_uint!(thread_rng_u32, u32); | |
165 | threadrng_uint!(thread_rng_u64, u64); |