diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2021-01-19 15:45:34 +0100 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2021-04-13 12:38:16 +0200 |
commit | eebd9c6661a82f01646f993d54d696982c68cf6c (patch) | |
tree | a99b8abb60eab461a72ecac646c47a3bc5d82bca | |
parent | b2c2862ba89eeef728c77e441af1d0f6c008adb9 (diff) |
bench: Add encrypt benchmark.
- Prevent the compiler from optimizing for the inputs by using
bench_with_input.
-rw-r--r-- | openpgp/Cargo.toml | 4 | ||||
-rw-r--r-- | openpgp/benches/common/encrypt.rs | 42 | ||||
-rw-r--r-- | openpgp/benches/common/mod.rs | 1 | ||||
-rw-r--r-- | openpgp/benches/encrypt_message.rs | 52 |
4 files changed, 99 insertions, 0 deletions
diff --git a/openpgp/Cargo.toml b/openpgp/Cargo.toml index cd6395c4..d36022f8 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/common/encrypt.rs b/openpgp/benches/common/encrypt.rs new file mode 100644 index 00000000..0b5c94d7 --- /dev/null +++ b/openpgp/benches/common/encrypt.rs @@ -0,0 +1,42 @@ +use sequoia_openpgp as openpgp; +use openpgp::cert::Cert; +use openpgp::policy::StandardPolicy; +use openpgp::serialize::stream::{Encryptor, LiteralWriter, Message}; + +use std::io::Write; + +// naively encrypt, without caring for revocation or expiration +pub fn encrypt_to_cert( + bytes: &[u8], + cert: &Cert, +) -> openpgp::Result<Vec<u8>> { + let mut sink = vec![]; + let p = &StandardPolicy::new(); + let recipients = cert + .keys() + .with_policy(p, None) + .supported() + .for_transport_encryption() + .for_storage_encryption(); + let message = + Encryptor::for_recipients(Message::new(&mut sink), recipients) + .build()?; + let mut w = LiteralWriter::new(message).build()?; + w.write_all(bytes)?; + w.finalize()?; + Ok(sink) +} + +pub fn encrypt_with_password( + bytes: &[u8], + password: &str, +) -> openpgp::Result<Vec<u8>> { + let mut sink = vec![]; + let message = + Encryptor::with_passwords(Message::new(&mut sink), Some(password)) + .build()?; + let mut w = LiteralWriter::new(message).build()?; + w.write_all(bytes)?; + w.finalize()?; + Ok(sink) +} diff --git a/openpgp/benches/common/mod.rs b/openpgp/benches/common/mod.rs new file mode 100644 index 00000000..b9c16b39 --- /dev/null +++ b/openpgp/benches/common/mod.rs @@ -0,0 +1 @@ +pub(super) mod encrypt; diff --git a/openpgp/benches/encrypt_message.rs b/openpgp/benches/encrypt_message.rs new file mode 100644 index 00000000..d83601ad --- /dev/null +++ b/openpgp/benches/encrypt_message.rs @@ -0,0 +1,52 @@ +use criterion::{ + criterion_group, criterion_main, BenchmarkId, Criterion, Throughput, +}; + +use sequoia_openpgp as openpgp; +use openpgp::cert::Cert; +use openpgp::parse::Parse; + +mod common; +use common::encrypt; + +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]; +} + +pub fn encrypt_to_testy(bytes: &[u8]) { + let testy = + Cert::from_bytes(&include_bytes!("../tests/data/keys/testy.pgp")[..]) + .unwrap(); + encrypt::encrypt_to_cert(bytes, &testy).unwrap(); +} + +pub fn encrypt_with_password(bytes: &[u8]) { + let password = "ściśle tajne"; + encrypt::encrypt_with_password(bytes, password).unwrap(); +} + +fn bench_encrypt(c: &mut Criterion) { + let mut group = c.benchmark_group("encrypt message"); + + // Encrypt a very short, medium and very long message. + let messages = &[b"Hello world.", &ZEROS_1_MB[..], &ZEROS_10_MB[..]]; + + for message in messages { + group.throughput(Throughput::Bytes(message.len() as u64)); + group.bench_with_input( + BenchmarkId::new("password", message.len()), + &message, + |b, m| b.iter(|| encrypt_with_password(&m)), + ); + group.bench_with_input( + BenchmarkId::new("cert", message.len()), + &message, + |b, m| b.iter(|| encrypt_to_testy(&m)), + ); + } + group.finish(); +} + +criterion_group!(benches, bench_encrypt); +criterion_main!(benches); |