aboutsummaryrefslogtreecommitdiff
path: root/rand/benches/generators.rs
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2018-12-11 23:50:45 +0100
committerDaniel Mueller <deso@posteo.net>2018-12-17 07:52:13 -0800
commit986ad2f782cf944990e4eda8bf88ea1821233302 (patch)
tree1717075a4eb11861c32e5c45d01e47360fb1264d /rand/benches/generators.rs
parente97c287c01cf22a1b582a7da9b309b58f3935d0e (diff)
downloadnitrocli-986ad2f782cf944990e4eda8bf88ea1821233302.tar.gz
nitrocli-986ad2f782cf944990e4eda8bf88ea1821233302.tar.bz2
Add nitrokey as a dependency to nitrocli
The nitrokey crate provides a simple interface to the Nitrokey Storage and the Nitrokey Pro based on the libnitrokey library developed by Nitrokey UG. The low-level bindings to this library are available in the nitrokey-sys crate. This patch adds version v0.2.1 of the nitrokey crate as a dependency for nitrocli. It includes the indirect dependencies nitrokey-sys (version 3.4.1) and rand (version 0.4.3). Import subrepo nitrokey/:nitrokey at 2eccc96ceec2282b868891befe9cda7f941fbe7b Import subrepo nitrokey-sys/:nitrokey-sys at f1a11ebf72610fb9cf80ac7f9f147b4ba1a5336f Import subrepo rand/:rand at d7d5da49daf7ceb3e5940072940d495cced3a1b3
Diffstat (limited to 'rand/benches/generators.rs')
-rw-r--r--rand/benches/generators.rs133
1 files changed, 133 insertions, 0 deletions
diff --git a/rand/benches/generators.rs b/rand/benches/generators.rs
new file mode 100644
index 0000000..daee7c5
--- /dev/null
+++ b/rand/benches/generators.rs
@@ -0,0 +1,133 @@
+#![feature(test)]
+
+extern crate test;
+extern crate rand;
+
+const RAND_BENCH_N: u64 = 1000;
+const BYTES_LEN: usize = 1024;
+
+use std::mem::size_of;
+use test::{black_box, Bencher};
+
+use rand::{Rng, StdRng, OsRng, JitterRng};
+use rand::{XorShiftRng, IsaacRng, Isaac64Rng, ChaChaRng};
+
+macro_rules! gen_bytes {
+ ($fnn:ident, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng: $gen = OsRng::new().unwrap().gen();
+ let mut buf = [0u8; BYTES_LEN];
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ rng.fill_bytes(&mut buf);
+ black_box(buf);
+ }
+ });
+ b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
+ }
+ }
+}
+
+macro_rules! gen_bytes_new {
+ ($fnn:ident, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng = $gen::new().unwrap();
+ let mut buf = [0u8; BYTES_LEN];
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ rng.fill_bytes(&mut buf);
+ black_box(buf);
+ }
+ });
+ b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
+ }
+ }
+}
+
+gen_bytes!(gen_bytes_xorshift, XorShiftRng);
+gen_bytes!(gen_bytes_isaac, IsaacRng);
+gen_bytes!(gen_bytes_isaac64, Isaac64Rng);
+gen_bytes!(gen_bytes_chacha, ChaChaRng);
+gen_bytes_new!(gen_bytes_std, StdRng);
+gen_bytes_new!(gen_bytes_os, OsRng);
+
+
+macro_rules! gen_uint {
+ ($fnn:ident, $ty:ty, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng: $gen = OsRng::new().unwrap().gen();
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ black_box(rng.gen::<$ty>());
+ }
+ });
+ b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
+ }
+ }
+}
+
+macro_rules! gen_uint_new {
+ ($fnn:ident, $ty:ty, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng = $gen::new().unwrap();
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ black_box(rng.gen::<$ty>());
+ }
+ });
+ b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
+ }
+ }
+}
+
+gen_uint!(gen_u32_xorshift, u32, XorShiftRng);
+gen_uint!(gen_u32_isaac, u32, IsaacRng);
+gen_uint!(gen_u32_isaac64, u32, Isaac64Rng);
+gen_uint!(gen_u32_chacha, u32, ChaChaRng);
+gen_uint_new!(gen_u32_std, u32, StdRng);
+gen_uint_new!(gen_u32_os, u32, OsRng);
+
+gen_uint!(gen_u64_xorshift, u64, XorShiftRng);
+gen_uint!(gen_u64_isaac, u64, IsaacRng);
+gen_uint!(gen_u64_isaac64, u64, Isaac64Rng);
+gen_uint!(gen_u64_chacha, u64, ChaChaRng);
+gen_uint_new!(gen_u64_std, u64, StdRng);
+gen_uint_new!(gen_u64_os, u64, OsRng);
+
+#[bench]
+fn gen_u64_jitter(b: &mut Bencher) {
+ let mut rng = JitterRng::new().unwrap();
+ b.iter(|| {
+ black_box(rng.gen::<u64>());
+ });
+ b.bytes = size_of::<u64>() as u64;
+}
+
+macro_rules! init_gen {
+ ($fnn:ident, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng: XorShiftRng = OsRng::new().unwrap().gen();
+ b.iter(|| {
+ let r2: $gen = rng.gen();
+ black_box(r2);
+ });
+ }
+ }
+}
+
+init_gen!(init_xorshift, XorShiftRng);
+init_gen!(init_isaac, IsaacRng);
+init_gen!(init_isaac64, Isaac64Rng);
+init_gen!(init_chacha, ChaChaRng);
+
+#[bench]
+fn init_jitter(b: &mut Bencher) {
+ b.iter(|| {
+ black_box(JitterRng::new().unwrap());
+ });
+}