diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2021-01-19 15:45:34 +0100 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2021-01-19 16:17:34 +0100 |
commit | 8e87428b8debda2de6f18d3969668ecbbcda1e76 (patch) | |
tree | 79764df0603fe04307657de0c5616b44edffe438 | |
parent | ba723b54efebc0548150f6166e2d1192740d6b8d (diff) |
Add benchmark encrypt message.
-rw-r--r-- | openpgp/Cargo.toml | 4 | ||||
-rw-r--r-- | openpgp/benches/encrypt_message.rs | 43 |
2 files changed, 47 insertions, 0 deletions
diff --git a/openpgp/Cargo.toml b/openpgp/Cargo.toml index 883e485f..4064c044 100644 --- a/openpgp/Cargo.toml +++ b/openpgp/Cargo.toml @@ -92,3 +92,7 @@ harness = false [[bench]] name = "parse_cert" harness = false + +[[bench]] +name = "encrypt_message" +harness = false diff --git a/openpgp/benches/encrypt_message.rs b/openpgp/benches/encrypt_message.rs new file mode 100644 index 00000000..a81c9f31 --- /dev/null +++ b/openpgp/benches/encrypt_message.rs @@ -0,0 +1,43 @@ +use criterion::{criterion_group, criterion_main, Criterion}; + +use sequoia_openpgp::serialize::stream::{Encryptor, LiteralWriter, Message}; +use std::io::Write; + +lazy_static::lazy_static! { + static ref ZEROS_1_MB: Vec<u8> = vec![0; 1 * 1024 * 1024]; + static ref ZEROS_10_MB: Vec<u8> = vec![0; 10 * 1024 * 1024]; +} + +fn encrypt_with_password(bytes: &[u8]) -> sequoia_openpgp::Result<()> { + let mut sink = vec![]; + let message = Encryptor::with_passwords( + Message::new(&mut sink), + Some("ściśle tajne"), + ) + .build()?; + let mut w = LiteralWriter::new(message).build()?; + w.write_all(bytes)?; + w.finalize()?; + Ok(()) +} + +fn bench_encrypt_with_password(c: &mut Criterion) { + let mut group = c.benchmark_group("encrypt message"); + let bytes = b"Hello world."; + group.bench_function(format!("password {:?}", bytes.len()), |b| { + b.iter(|| encrypt_with_password(bytes)) + }); + let bytes = &ZEROS_1_MB[..]; + group.bench_function(format!("password {:?}", bytes.len()), |b| { + b.iter(|| encrypt_with_password(bytes)) + }); + let bytes = &ZEROS_10_MB[..]; + group.bench_function(format!("password {:?}", bytes.len()), |b| { + b.iter(|| encrypt_with_password(bytes)) + }); + + group.finish(); +} + +criterion_group!(benches, bench_encrypt_with_password); +criterion_main!(benches); |