-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated ChunkedDecryptor to use Write trait, and created criterion be…
…nchmarks
- Loading branch information
1 parent
c6b907b
commit 7563819
Showing
7 changed files
with
475 additions
and
156 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::io::Write; | ||
|
||
use bitwarden::crypto::{encrypt_aes256, ChunkedDecryptor, SymmetricCryptoKey}; | ||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use rand::RngCore; | ||
|
||
struct SizeFmt(usize); | ||
impl std::fmt::Display for SizeFmt { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
const SUFFIXES: [&str; 5] = ["B", "KB", "MB", "GB", "TB"]; | ||
let mut size = self.0 as f64; | ||
for suffix in SUFFIXES { | ||
if size < 1024.0 { | ||
return write!(f, "{:.1}{}", size, suffix); | ||
} | ||
size /= 1024.0; | ||
} | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
pub fn criterion_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("decryption"); | ||
|
||
for size in [100 * 1024, 15 * 1024 * 1024, 200 * 1024 * 1024] { | ||
group.throughput(criterion::Throughput::Bytes(size as u64)); | ||
if size > 1024 * 1024 { | ||
group.sample_size(20); | ||
} | ||
|
||
let mut initial_buf = Vec::with_capacity(size); | ||
initial_buf.resize(size, 0); | ||
rand::thread_rng().fill_bytes(&mut initial_buf[..size]); | ||
let key: SymmetricCryptoKey = SymmetricCryptoKey::generate("test"); | ||
let enc_str = encrypt_aes256(&initial_buf, key.mac_key, key.key).unwrap(); | ||
let enc_buf = enc_str.to_buffer().unwrap(); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("decrypt_with_key", SizeFmt(size)), | ||
&size, | ||
|b, _size| b.iter(|| black_box(enc_str.decrypt_with_key(&key).unwrap())), | ||
); | ||
|
||
for chunk_size in [64, 2048, 8192] { | ||
group.bench_with_input( | ||
BenchmarkId::new(format!("ChunkedDecryptor[{chunk_size}]"), SizeFmt(size)), | ||
&size, | ||
|b, _size| { | ||
b.iter(|| { | ||
let mut decrypted_buf = Vec::with_capacity(size); | ||
let mut cd = ChunkedDecryptor::new(&key, &mut decrypted_buf); | ||
|
||
for chunk in enc_buf.chunks(chunk_size) { | ||
cd.write_all(chunk).unwrap(); | ||
} | ||
cd.finalize().unwrap(); | ||
|
||
// | ||
}) | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
Oops, something went wrong.