// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. #![allow(non_snake_case)] const RAND_BENCH_N: u64 = 1000; const BYTES_LEN: usize = 1024; use core::mem::size_of; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use rand::prelude::*; use rand::rngs::adapter::ReseedingRng; use rand::rngs::{mock::StepRng, OsRng}; use rand_blake3::{Rng as Blake3Rng, UnbufferedRng as Blake3UnbufferedRng}; use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng}; use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg}; macro_rules! gen_bytes { ($c:ident, $fnn:ident, $gen:expr) => { $c.bench_function(stringify!($fnn), |b| { let mut rng = $gen; let mut buf = [0u8; BYTES_LEN]; b.iter(|| { for _ in 0..RAND_BENCH_N { rng.fill_bytes(&mut buf); black_box(buf); } }); }); }; } macro_rules! gen_uint { ($c:ident, $fnn:ident, $ty:ty, $gen:expr) => { $c.bench_function(stringify!($fnn), |b| { let mut rng = $gen; b.iter(|| { let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { accum = accum.wrapping_add(rng.gen::<$ty>()); } accum }); }); }; } macro_rules! init_gen { ($c:ident, $fnn:ident, $gen:ident) => { $c.bench_function(stringify!($fnn), |b| { let mut rng = Pcg32::from_entropy(); b.iter(|| { let r2 = $gen::from_rng(&mut rng).unwrap(); r2 }); }); }; } const RESEEDING_BYTES_LEN: usize = 1024 * 1024; const RESEEDING_BENCH_N: u64 = 16; macro_rules! reseeding_bytes { ($c:ident, $fnn:ident, $thresh:expr) => { $c.bench_function(stringify!($fnn), |b| { let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(), $thresh * 1024, OsRng); let mut buf = [0u8; RESEEDING_BYTES_LEN]; b.iter(|| { for _ in 0..RESEEDING_BENCH_N { rng.fill_bytes(&mut buf); black_box(&buf); } }); }); }; } macro_rules! threadrng_uint { ($c:ident, $fnn:ident, $ty:ty) => { $c.bench_function(stringify!($fnn), |b| { let mut rng = thread_rng(); b.iter(|| { let mut accum: $ty = 0; for _ in 0..RAND_BENCH_N { accum = accum.wrapping_add(rng.gen::<$ty>()); } accum }); }); }; } fn benchmark(c: &mut Criterion) { gen_bytes!(c, gen_bytes_step, StepRng::new(0, 1)); gen_bytes!(c, gen_bytes_pcg32, Pcg32::from_entropy()); gen_bytes!(c, gen_bytes_pcg64, Pcg64::from_entropy()); gen_bytes!(c, gen_bytes_pcg64mcg, Pcg64Mcg::from_entropy()); gen_bytes!(c, gen_bytes_chacha8, ChaCha8Rng::from_entropy()); gen_bytes!(c, gen_bytes_chacha12, ChaCha12Rng::from_entropy()); gen_bytes!(c, gen_bytes_chacha20, ChaCha20Rng::from_entropy()); gen_bytes!(c, gen_bytes_blake3, Blake3Rng::from_entropy()); gen_bytes!( c, gen_bytes_blake3_unbuffered, Blake3UnbufferedRng::from_entropy() ); gen_bytes!(c, gen_bytes_std, StdRng::from_entropy()); gen_bytes!(c, gen_bytes_small, SmallRng::from_entropy()); gen_bytes!(c, gen_bytes_os, OsRng); gen_uint!(c, gen_u32_step, u32, StepRng::new(0, 1)); gen_uint!(c, gen_u32_pcg32, u32, Pcg32::from_entropy()); gen_uint!(c, gen_u32_pcg64, u32, Pcg64::from_entropy()); gen_uint!(c, gen_u32_pcg64mcg, u32, Pcg64Mcg::from_entropy()); gen_uint!(c, gen_u32_chacha8, u32, ChaCha8Rng::from_entropy()); gen_uint!(c, gen_u32_chacha12, u32, ChaCha12Rng::from_entropy()); gen_uint!(c, gen_u32_chacha20, u32, ChaCha20Rng::from_entropy()); gen_uint!(c, gen_u32_blake3, u32, Blake3Rng::from_entropy()); gen_uint!( c, gen_u32_blake3_unbuffered, u32, Blake3UnbufferedRng::from_entropy() ); gen_uint!(c, gen_u32_std, u32, StdRng::from_entropy()); gen_uint!(c, gen_u32_small, u32, SmallRng::from_entropy()); gen_uint!(c, gen_u32_os, u32, OsRng); gen_uint!(c, gen_u64_step, u64, StepRng::new(0, 1)); gen_uint!(c, gen_u64_pcg32, u64, Pcg32::from_entropy()); gen_uint!(c, gen_u64_pcg64, u64, Pcg64::from_entropy()); gen_uint!(c, gen_u64_pcg64mcg, u64, Pcg64Mcg::from_entropy()); gen_uint!(c, gen_u64_chacha8, u64, ChaCha8Rng::from_entropy()); gen_uint!(c, gen_u64_chacha12, u64, ChaCha12Rng::from_entropy()); gen_uint!(c, gen_u64_chacha20, u64, ChaCha20Rng::from_entropy()); gen_uint!(c, gen_u64_blake3, u64, Blake3Rng::from_entropy()); gen_uint!( c, gen_u64_blake3_unbuffered, u64, Blake3UnbufferedRng::from_entropy() ); gen_uint!(c, gen_u64_std, u64, StdRng::from_entropy()); gen_uint!(c, gen_u64_small, u64, SmallRng::from_entropy()); gen_uint!(c, gen_u64_os, u64, OsRng); init_gen!(c, init_pcg32, Pcg32); init_gen!(c, init_pcg64, Pcg64); init_gen!(c, init_pcg64mcg, Pcg64Mcg); init_gen!(c, init_chacha, ChaCha20Rng); init_gen!(c, init_blake3, Blake3Rng); init_gen!(c, init_blake3_unbuffered, Blake3UnbufferedRng); reseeding_bytes!(c, reseeding_chacha20_4k, 4); reseeding_bytes!(c, reseeding_chacha20_16k, 16); reseeding_bytes!(c, reseeding_chacha20_32k, 32); reseeding_bytes!(c, reseeding_chacha20_64k, 64); reseeding_bytes!(c, reseeding_chacha20_256k, 256); reseeding_bytes!(c, reseeding_chacha20_1M, 1024); threadrng_uint!(c, thread_rng_u32, u32); threadrng_uint!(c, thread_rng_u64, u64); } criterion_group!(benches, benchmark); criterion_main!(benches);