From 999035675aedc7dbdccdfa67e95b8db6c0334769 Mon Sep 17 00:00:00 2001 From: Radu Marias Date: Fri, 22 Nov 2024 18:47:36 +0200 Subject: [PATCH] migrate one test to criterion --- .idea/rencfs.iml | 1 + benches/crypto_read.rs | 134 ++++++++++++++++++----------------------- 2 files changed, 61 insertions(+), 74 deletions(-) diff --git a/.idea/rencfs.iml b/.idea/rencfs.iml index cfd3cf3..0ecc648 100644 --- a/.idea/rencfs.iml +++ b/.idea/rencfs.iml @@ -8,6 +8,7 @@ + diff --git a/benches/crypto_read.rs b/benches/crypto_read.rs index 2b654bd..c04e9d0 100644 --- a/benches/crypto_read.rs +++ b/benches/crypto_read.rs @@ -41,7 +41,7 @@ fn bench_read_1mb_aes_file(c: &mut Criterion) { rand::thread_rng().fill_bytes(&mut key); let key = SecretVec::new(Box::new(key)); - c.bench_function("bench_read_1mb_chacha_file", |b| { + c.bench_function("bench_read_1mb_aes_file", |b| { b.iter(|| { let file = tempfile::tempfile().unwrap(); let mut writer = crypto::create_write(file, cipher, &key); @@ -54,77 +54,63 @@ fn bench_read_1mb_aes_file(c: &mut Criterion) { }); } -// #[bench] -// fn bench_read_1mb_chacha_ram(b: &mut Bencher) { -// use crate::crypto; -// use crate::crypto::write::CryptoWrite; -// use crate::crypto::Cipher; -// use rand::RngCore; -// use shush_rs::SecretVec; -// use std::io; -// use std::io::Seek; -// use test::black_box; -// -// let cipher = Cipher::ChaCha20Poly1305; -// let len = 1024 * 1024; -// -// let mut key: Vec = vec![0; cipher.key_len()]; -// rand::thread_rng().fill_bytes(&mut key); -// let key = SecretVec::new(Box::new(key)); -// -// let cursor_write = io::Cursor::new(vec![]); -// let mut writer = crypto::create_write(cursor_write, cipher, &key); -// let mut cursor_random = io::Cursor::new(vec![0; len]); -// rand::thread_rng().fill_bytes(cursor_random.get_mut()); -// cursor_random.seek(io::SeekFrom::Start(0)).unwrap(); -// io::copy(&mut cursor_random, &mut writer).unwrap(); -// let cursor_write = writer.finish().unwrap(); -// -// b.iter(|| { -// black_box({ -// let mut cursor = cursor_write.clone(); -// cursor.seek(io::SeekFrom::Start(0)).unwrap(); -// let mut reader = crypto::create_read(cursor, cipher, &key); -// io::copy(&mut reader, &mut io::sink()).unwrap() -// }); -// }); -// } -// -// #[bench] -// fn bench_read_1mb_aes_ram(b: &mut Bencher) { -// use crate::crypto; -// use crate::crypto::write::CryptoWrite; -// use crate::crypto::Cipher; -// use rand::RngCore; -// use shush_rs::SecretVec; -// use std::io; -// use std::io::Seek; -// use test::black_box; -// -// let cipher = Cipher::Aes256Gcm; -// let len = 1024 * 1024; -// -// let mut key: Vec = vec![0; cipher.key_len()]; -// rand::thread_rng().fill_bytes(&mut key); -// let key = SecretVec::new(Box::new(key)); -// -// let cursor_write = io::Cursor::new(vec![]); -// let mut writer = crypto::create_write(cursor_write, cipher, &key); -// let mut cursor_random = io::Cursor::new(vec![0; len]); -// rand::thread_rng().fill_bytes(cursor_random.get_mut()); -// cursor_random.seek(io::SeekFrom::Start(0)).unwrap(); -// io::copy(&mut cursor_random, &mut writer).unwrap(); -// let cursor_write = writer.finish().unwrap(); -// -// b.iter(|| { -// black_box({ -// let mut cursor = cursor_write.clone(); -// cursor.seek(io::SeekFrom::Start(0)).unwrap(); -// let mut reader = crypto::create_read(cursor, cipher, &key); -// io::copy(&mut reader, &mut io::sink()).unwrap() -// }); -// }); -// } - -criterion_group!(benches, bench_read_1mb_chacha_file, gs); +fn bench_read_1mb_chacha_ram(c: &mut Criterion) { + let cipher = Cipher::ChaCha20Poly1305; + let len = 1024 * 1024; + + let mut key: Vec = vec![0; cipher.key_len()]; + rand::thread_rng().fill_bytes(&mut key); + let key = SecretVec::new(Box::new(key)); + + let cursor_write = io::Cursor::new(vec![]); + let mut writer = crypto::create_write(cursor_write, cipher, &key); + let mut cursor_random = io::Cursor::new(vec![0; len]); + rand::thread_rng().fill_bytes(cursor_random.get_mut()); + cursor_random.seek(io::SeekFrom::Start(0)).unwrap(); + io::copy(&mut cursor_random, &mut writer).unwrap(); + let cursor_write = writer.finish().unwrap(); + + c.bench_function("bench_read_1mb_chacha_ram", |b| { + b.iter(|| { + let mut cursor = cursor_write.clone(); + cursor.seek(io::SeekFrom::Start(0)).unwrap(); + let mut reader = crypto::create_read(cursor, cipher, &key); + black_box(io::copy(&mut reader, &mut io::sink()).unwrap()) + }) + }); +} + +fn bench_read_1mb_aes_ram(c: &mut Criterion) { + let cipher = Cipher::Aes256Gcm; + let len = 1024 * 1024; + + let mut key: Vec = vec![0; cipher.key_len()]; + rand::thread_rng().fill_bytes(&mut key); + let key = SecretVec::new(Box::new(key)); + + let cursor_write = io::Cursor::new(vec![]); + let mut writer = crypto::create_write(cursor_write, cipher, &key); + let mut cursor_random = io::Cursor::new(vec![0; len]); + rand::thread_rng().fill_bytes(cursor_random.get_mut()); + cursor_random.seek(io::SeekFrom::Start(0)).unwrap(); + io::copy(&mut cursor_random, &mut writer).unwrap(); + let cursor_write = writer.finish().unwrap(); + + c.bench_function("bench_read_1mb_aes_file", |b| { + b.iter(|| { + let mut cursor = cursor_write.clone(); + cursor.seek(io::SeekFrom::Start(0)).unwrap(); + let mut reader = crypto::create_read(cursor, cipher, &key); + black_box(io::copy(&mut reader, &mut io::sink()).unwrap()) + }) + }); +} + +criterion_group!( + benches, + bench_read_1mb_chacha_file, + bench_read_1mb_aes_file, + bench_read_1mb_chacha_ram, + bench_read_1mb_aes_ram +); criterion_main!(benches);