Skip to content

Commit

Permalink
migrate one test to criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
radumarias committed Nov 22, 2024
1 parent d9b56c7 commit 9990356
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 74 deletions.
1 change: 1 addition & 0 deletions .idea/rencfs.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 60 additions & 74 deletions benches/crypto_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<u8> = 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<u8> = 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<u8> = 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<u8> = 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);

0 comments on commit 9990356

Please sign in to comment.