]> git.proxmox.com Git - rustc.git/blame - vendor/rand/benches/generators.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / vendor / rand / benches / generators.rs
CommitLineData
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
12extern crate test;
b7449926
XL
13
14const RAND_BENCH_N: u64 = 1000;
15const BYTES_LEN: usize = 1024;
16
17use std::mem::size_of;
18use test::{black_box, Bencher};
19
20use rand::prelude::*;
b7449926 21use rand::rngs::adapter::ReseedingRng;
dfeec247
XL
22use rand::rngs::{mock::StepRng, OsRng};
23use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng};
24use rand_hc::Hc128Rng;
416331ca 25use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg};
b7449926
XL
26
27macro_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 44gen_bytes!(gen_bytes_step, StepRng::new(0, 1));
416331ca
XL
45gen_bytes!(gen_bytes_pcg32, Pcg32::from_entropy());
46gen_bytes!(gen_bytes_pcg64, Pcg64::from_entropy());
47gen_bytes!(gen_bytes_pcg64mcg, Pcg64Mcg::from_entropy());
48gen_bytes!(gen_bytes_chacha8, ChaCha8Rng::from_entropy());
49gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_entropy());
50gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_entropy());
b7449926 51gen_bytes!(gen_bytes_hc128, Hc128Rng::from_entropy());
b7449926 52gen_bytes!(gen_bytes_std, StdRng::from_entropy());
dfeec247 53#[cfg(feature = "small_rng")]
b7449926 54gen_bytes!(gen_bytes_small, SmallRng::from_entropy());
416331ca 55gen_bytes!(gen_bytes_os, OsRng);
b7449926
XL
56
57macro_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 74gen_uint!(gen_u32_step, u32, StepRng::new(0, 1));
416331ca
XL
75gen_uint!(gen_u32_pcg32, u32, Pcg32::from_entropy());
76gen_uint!(gen_u32_pcg64, u32, Pcg64::from_entropy());
77gen_uint!(gen_u32_pcg64mcg, u32, Pcg64Mcg::from_entropy());
78gen_uint!(gen_u32_chacha8, u32, ChaCha8Rng::from_entropy());
79gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_entropy());
80gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_entropy());
b7449926 81gen_uint!(gen_u32_hc128, u32, Hc128Rng::from_entropy());
b7449926 82gen_uint!(gen_u32_std, u32, StdRng::from_entropy());
dfeec247 83#[cfg(feature = "small_rng")]
b7449926 84gen_uint!(gen_u32_small, u32, SmallRng::from_entropy());
416331ca 85gen_uint!(gen_u32_os, u32, OsRng);
b7449926 86
416331ca 87gen_uint!(gen_u64_step, u64, StepRng::new(0, 1));
416331ca
XL
88gen_uint!(gen_u64_pcg32, u64, Pcg32::from_entropy());
89gen_uint!(gen_u64_pcg64, u64, Pcg64::from_entropy());
90gen_uint!(gen_u64_pcg64mcg, u64, Pcg64Mcg::from_entropy());
91gen_uint!(gen_u64_chacha8, u64, ChaCha8Rng::from_entropy());
92gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_entropy());
93gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_entropy());
b7449926 94gen_uint!(gen_u64_hc128, u64, Hc128Rng::from_entropy());
b7449926 95gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
dfeec247 96#[cfg(feature = "small_rng")]
b7449926 97gen_uint!(gen_u64_small, u64, SmallRng::from_entropy());
416331ca 98gen_uint!(gen_u64_os, u64, OsRng);
b7449926
XL
99
100macro_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
113init_gen!(init_pcg32, Pcg32);
114init_gen!(init_pcg64, Pcg64);
115init_gen!(init_pcg64mcg, Pcg64Mcg);
b7449926 116init_gen!(init_hc128, Hc128Rng);
416331ca 117init_gen!(init_chacha, ChaCha20Rng);
b7449926 118
416331ca
XL
119const RESEEDING_BYTES_LEN: usize = 1024 * 1024;
120const RESEEDING_BENCH_N: u64 = 16;
b7449926 121
416331ca
XL
122macro_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
139reseeding_bytes!(reseeding_chacha20_4k, 4);
140reseeding_bytes!(reseeding_chacha20_16k, 16);
141reseeding_bytes!(reseeding_chacha20_32k, 32);
142reseeding_bytes!(reseeding_chacha20_64k, 64);
143reseeding_bytes!(reseeding_chacha20_256k, 256);
144reseeding_bytes!(reseeding_chacha20_1M, 1024);
b7449926
XL
145
146
147macro_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
164threadrng_uint!(thread_rng_u32, u32);
165threadrng_uint!(thread_rng_u64, u64);