From d9b56c756670b54fdbcb9fab805ea9f44873b366 Mon Sep 17 00:00:00 2001 From: Radu Marias Date: Fri, 22 Nov 2024 18:37:34 +0200 Subject: [PATCH] migrate one test to criterion --- benches/crypto_read.rs | 59 ++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/benches/crypto_read.rs b/benches/crypto_read.rs index a9ad32c0..2b654bd7 100644 --- a/benches/crypto_read.rs +++ b/benches/crypto_read.rs @@ -33,42 +33,27 @@ fn bench_read_1mb_chacha_file(c: &mut Criterion) { }); } -// #[bench] -// fn bench_read_1mb_aes_file(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 file = tempfile::tempfile().unwrap(); -// let mut writer = crypto::create_write(file, 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 file = writer.finish().unwrap(); -// -// b.iter(|| { -// black_box({ -// let mut file = file.try_clone().unwrap(); -// file.seek(io::SeekFrom::Start(0)).unwrap(); -// let mut reader = crypto::create_read(file, cipher, &key); -// io::copy(&mut reader, &mut io::sink()).unwrap() -// }); -// }); -// } -// +fn bench_read_1mb_aes_file(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)); + + c.bench_function("bench_read_1mb_chacha_file", |b| { + b.iter(|| { + let file = tempfile::tempfile().unwrap(); + let mut writer = crypto::create_write(file, 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(); + black_box(writer.finish().unwrap()) + }) + }); +} + // #[bench] // fn bench_read_1mb_chacha_ram(b: &mut Bencher) { // use crate::crypto; @@ -141,5 +126,5 @@ fn bench_read_1mb_chacha_file(c: &mut Criterion) { // }); // } -criterion_group!(benches, bench_read_1mb_chacha_file); +criterion_group!(benches, bench_read_1mb_chacha_file, gs); criterion_main!(benches);