bench: Improve benchmark_decrypting.rs benchmark

This commit is contained in:
Hocuri
2025-08-11 16:29:34 +02:00
parent 479a5632fb
commit 2efbbcc669
4 changed files with 92 additions and 51 deletions

View File

@@ -1,19 +1,45 @@
//! Benchmarks for message decryption,
//! comparing decryption of symmetrically-encrypted messages
//! to decryption of asymmetrically-encrypted messages.
//!
//! Call with
//!
//! ```text
//! cargo bench --bench benchmark_decrypting --features="internals"
//! ```
//!
//! or, if you want to only run e.g. the 'Decrypt a symmetrically encrypted message' benchmark:
//!
//! ```text
//! cargo bench --bench benchmark_decrypting --features="internals" -- 'Decrypt a symmetrically encrypted message'
//! ```
//!
//! You can also pass a substring.
//! So, you can run all 'Decrypt and parse' benchmarks with:
//!
//! ```text
//! cargo bench --bench benchmark_decrypting --features="internals" -- 'Decrypt and parse'
//! ```
//!
//! Symmetric decryption has to try out all known secrets,
//! You can benchmark this by adapting the `NUM_SECRETS` variable.
use std::hint::black_box; use std::hint::black_box;
use criterion::{Criterion, criterion_group, criterion_main}; use criterion::{Criterion, criterion_group, criterion_main};
use deltachat::benchmark_internals::create_dummy_keypair; use deltachat::internals_for_benchmarks::create_broadcast_shared_secret;
use deltachat::benchmark_internals::save_broadcast_shared_secret; use deltachat::internals_for_benchmarks::create_dummy_keypair;
use deltachat::internals_for_benchmarks::save_broadcast_shared_secret;
use deltachat::{ use deltachat::{
Events, Events,
benchmark_internals::key_from_asc,
benchmark_internals::parse_and_get_text,
benchmark_internals::store_self_keypair,
chat::ChatId, chat::ChatId,
config::Config, config::Config,
context::Context, context::Context,
internals_for_benchmarks::key_from_asc,
internals_for_benchmarks::parse_and_get_text,
internals_for_benchmarks::store_self_keypair,
pgp::{KeyPair, decrypt, encrypt_for_broadcast, pk_encrypt}, pgp::{KeyPair, decrypt, encrypt_for_broadcast, pk_encrypt},
stock_str::StockStrings, stock_str::StockStrings,
tools::create_broadcast_shared_secret_pub,
}; };
use rand::{Rng, thread_rng}; use rand::{Rng, thread_rng};
use tempfile::tempdir; use tempfile::tempdir;
@@ -45,15 +71,17 @@ async fn create_context() -> Context {
fn criterion_benchmark(c: &mut Criterion) { fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Decrypt"); let mut group = c.benchmark_group("Decrypt");
// ===========================================================================================
// Benchmarks for decryption only, without any other parsing
// ===========================================================================================
group.sample_size(10); group.sample_size(10);
group.bench_function("Decrypt symmetrically encrypted", |b| {
let rt = tokio::runtime::Runtime::new().unwrap(); group.bench_function("Decrypt a symmetrically encrypted message", |b| {
let mut plain: Vec<u8> = vec![0; 500]; let plain = generate_plaintext();
thread_rng().fill(&mut plain[..]); let secrets = generate_secrets();
let (secrets, encrypted) = rt.block_on(async { let encrypted = tokio::runtime::Runtime::new().unwrap().block_on(async {
let secrets: Vec<String> = (0..NUM_SECRETS)
.map(|_| create_broadcast_shared_secret_pub())
.collect();
let secret = secrets[NUM_SECRETS / 2].clone(); let secret = secrets[NUM_SECRETS / 2].clone();
let encrypted = encrypt_for_broadcast( let encrypted = encrypt_for_broadcast(
plain.clone(), plain.clone(),
@@ -64,7 +92,7 @@ fn criterion_benchmark(c: &mut Criterion) {
.await .await
.unwrap(); .unwrap();
(secrets, encrypted) encrypted
}); });
b.iter(|| { b.iter(|| {
@@ -75,16 +103,12 @@ fn criterion_benchmark(c: &mut Criterion) {
assert_eq!(black_box(decrypted), plain); assert_eq!(black_box(decrypted), plain);
}); });
}); });
group.bench_function("Decrypt pk encrypted", |b| {
// TODO code duplication with previous benchmark group.bench_function("Decrypt a public-key encrypted message", |b| {
let rt = tokio::runtime::Runtime::new().unwrap(); let plain = generate_plaintext();
let mut plain: Vec<u8> = vec![0; 500];
thread_rng().fill(&mut plain[..]);
let key_pair = create_dummy_keypair("alice@example.org").unwrap(); let key_pair = create_dummy_keypair("alice@example.org").unwrap();
let (secrets, encrypted) = rt.block_on(async { let secrets = generate_secrets();
let secrets: Vec<String> = (0..NUM_SECRETS) let encrypted = tokio::runtime::Runtime::new().unwrap().block_on(async {
.map(|_| create_broadcast_shared_secret_pub())
.collect();
let encrypted = pk_encrypt( let encrypted = pk_encrypt(
plain.clone(), plain.clone(),
vec![black_box(key_pair.public.clone())], vec![black_box(key_pair.public.clone())],
@@ -94,7 +118,7 @@ fn criterion_benchmark(c: &mut Criterion) {
.await .await
.unwrap(); .unwrap();
(secrets, encrypted) encrypted
}); });
b.iter(|| { b.iter(|| {
@@ -110,12 +134,15 @@ fn criterion_benchmark(c: &mut Criterion) {
}); });
}); });
let rt = tokio::runtime::Runtime::new().unwrap(); // ===========================================================================================
let mut secrets: Vec<String> = (0..NUM_SECRETS) // Benchmarks for the whole parsing pipeline, incl. decryption (but excl. receive_imf())
.map(|_| create_broadcast_shared_secret_pub()) // ===========================================================================================
.collect();
// "secret" is the shared secret that was used to encrypt text_symmetrically_encrypted.eml: let rt = tokio::runtime::Runtime::new().unwrap();
let mut secrets = generate_secrets();
// "secret" is the shared secret that was used to encrypt text_symmetrically_encrypted.eml.
// Put it into the middle of our secrets:
secrets[NUM_SECRETS / 2] = "secret".to_string(); secrets[NUM_SECRETS / 2] = "secret".to_string();
let context = rt.block_on(async { let context = rt.block_on(async {
@@ -128,21 +155,7 @@ fn criterion_benchmark(c: &mut Criterion) {
context context
}); });
group.bench_function("Receive a public-key encrypted message", |b| { group.bench_function("Decrypt and parse a symmetrically encrypted message", |b| {
b.to_async(&rt).iter(|| {
let ctx = context.clone();
async move {
let text = parse_and_get_text(
&ctx,
include_bytes!("../test-data/message/text_from_alice_encrypted.eml"),
)
.await
.unwrap();
assert_eq!(text, "hi");
}
});
});
group.bench_function("Receive a symmetrically encrypted message", |b| {
b.to_async(&rt).iter(|| { b.to_async(&rt).iter(|| {
let ctx = context.clone(); let ctx = context.clone();
async move { async move {
@@ -156,8 +169,37 @@ fn criterion_benchmark(c: &mut Criterion) {
} }
}); });
}); });
group.bench_function("Decrypt and parse a public-key encrypted message", |b| {
b.to_async(&rt).iter(|| {
let ctx = context.clone();
async move {
let text = parse_and_get_text(
&ctx,
include_bytes!("../test-data/message/text_from_alice_encrypted.eml"),
)
.await
.unwrap();
assert_eq!(text, "hi");
}
});
});
group.finish(); group.finish();
} }
fn generate_secrets() -> Vec<String> {
let secrets: Vec<String> = (0..NUM_SECRETS)
.map(|_| create_broadcast_shared_secret())
.collect();
secrets
}
fn generate_plaintext() -> Vec<u8> {
let mut plain: Vec<u8> = vec![0; 500];
thread_rng().fill(&mut plain[..]);
plain
}
criterion_group!(benches, criterion_benchmark); criterion_group!(benches, criterion_benchmark);
criterion_main!(benches); criterion_main!(benches);

View File

@@ -38,3 +38,7 @@ pub async fn save_broadcast_shared_secret(
pub fn create_dummy_keypair(addr: &str) -> Result<KeyPair> { pub fn create_dummy_keypair(addr: &str) -> Result<KeyPair> {
pgp::create_keypair(EmailAddress::new(addr)?) pgp::create_keypair(EmailAddress::new(addr)?)
} }
pub fn create_broadcast_shared_secret() -> String {
crate::tools::create_broadcast_shared_secret()
}

View File

@@ -113,7 +113,7 @@ pub mod peer_channels;
pub mod reaction; pub mod reaction;
#[cfg(feature = "internals")] #[cfg(feature = "internals")]
pub mod benchmark_internals; pub mod internals_for_benchmarks;
/// If set IMAP/incoming and SMTP/outgoing MIME messages will be printed. /// If set IMAP/incoming and SMTP/outgoing MIME messages will be printed.
pub const DCC_MIME_DEBUG: &str = "DCC_MIME_DEBUG"; pub const DCC_MIME_DEBUG: &str = "DCC_MIME_DEBUG";

View File

@@ -319,11 +319,6 @@ pub(crate) fn create_broadcast_shared_secret() -> String {
res res
} }
#[cfg(feature = "internals")]
pub fn create_broadcast_shared_secret_pub() -> String {
create_broadcast_shared_secret()
}
/// Returns true if given string is a valid ID. /// Returns true if given string is a valid ID.
/// ///
/// All IDs generated with `create_id()` should be considered valid. /// All IDs generated with `create_id()` should be considered valid.