Skip to content

Commit

Permalink
Updated ChunkedDecryptor to use Write trait, and created criterion be…
Browse files Browse the repository at this point in the history
…nchmarks
  • Loading branch information
dani-garcia committed Oct 6, 2023
1 parent c6b907b commit 7563819
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 156 deletions.
181 changes: 180 additions & 1 deletion Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@ bitwarden-api-identity = { path = "../bitwarden-api-identity", version = "=0.2.1
bitwarden-api-api = { path = "../bitwarden-api-api", version = "=0.2.1" }

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
tokio = { version = "1.28.2", features = ["rt", "macros"] }
wiremock = "0.5.18"

[[bench]]
name = "chunked_decryption"
harness = false
67 changes: 67 additions & 0 deletions crates/bitwarden/benches/chunked_decryption.rs
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);
Loading

0 comments on commit 7563819

Please sign in to comment.