Skip to content

Commit 49f1b37

Browse files
committed
bench: Improve benchmark_decrypting.rs benchmark
1 parent bfedba9 commit 49f1b37

File tree

4 files changed

+82
-41
lines changed

4 files changed

+82
-41
lines changed

benches/benchmark_decrypting.rs

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1+
//! Benchmarks for message decryption,
2+
//! comparing decryption of symmetrically-encrypted messages
3+
//! to decryption of asymmetrically-encrypted messages.
4+
//!
5+
//! Call with
6+
//!
7+
//! ```text
8+
//! cargo bench --bench benchmark_decrypting --features="internals"
9+
//! ```
10+
//!
11+
//! or, if you want to only run e.g. the 'Decrypt a symmetrically encrypted message' benchmark:
12+
//!
13+
//! ```text
14+
//! cargo bench --bench benchmark_decrypting --features="internals" -- 'Decrypt a symmetrically encrypted message'
15+
//! ```
16+
//!
17+
//! You can also pass a substring.
18+
//! So, you can run all 'Decrypt and parse' benchmarks with:
19+
//!
20+
//! ```text
21+
//! cargo bench --bench benchmark_decrypting --features="internals" -- 'Decrypt and parse'
22+
//! ```
23+
//!
24+
//! Symmetric decryption has to try out all known secrets,
25+
//! You can benchmark this by adapting the `NUM_SECRETS` variable.
26+
127
use std::hint::black_box;
228

329
use criterion::{Criterion, criterion_group, criterion_main};
4-
use deltachat::benchmark_internals::create_dummy_keypair;
5-
use deltachat::benchmark_internals::save_broadcast_shared_secret;
30+
use deltachat::internals_for_benchmarks::create_broadcast_shared_secret;
31+
use deltachat::internals_for_benchmarks::create_dummy_keypair;
32+
use deltachat::internals_for_benchmarks::save_broadcast_shared_secret;
633
use deltachat::{
734
Events,
8-
benchmark_internals::key_from_asc,
9-
benchmark_internals::parse_and_get_text,
10-
benchmark_internals::store_self_keypair,
1135
chat::ChatId,
1236
config::Config,
1337
context::Context,
38+
internals_for_benchmarks::key_from_asc,
39+
internals_for_benchmarks::parse_and_get_text,
40+
internals_for_benchmarks::store_self_keypair,
1441
pgp::{KeyPair, decrypt, encrypt_for_broadcast, pk_encrypt},
1542
stock_str::StockStrings,
16-
tools::create_broadcast_shared_secret_pub,
1743
};
1844
use rand::{Rng, thread_rng};
1945
use tempfile::tempdir;
@@ -45,15 +71,17 @@ async fn create_context() -> Context {
4571

4672
fn criterion_benchmark(c: &mut Criterion) {
4773
let mut group = c.benchmark_group("Decrypt");
74+
75+
// ===========================================================================================
76+
// Benchmarks for decryption only, without any other parsing
77+
// ===========================================================================================
78+
4879
group.sample_size(10);
49-
group.bench_function("Decrypt symmetrically encrypted", |b| {
50-
let rt = tokio::runtime::Runtime::new().unwrap();
51-
let mut plain: Vec<u8> = vec![0; 500];
52-
thread_rng().fill(&mut plain[..]);
53-
let (secrets, encrypted) = rt.block_on(async {
54-
let secrets: Vec<String> = (0..NUM_SECRETS)
55-
.map(|_| create_broadcast_shared_secret_pub())
56-
.collect();
80+
81+
group.bench_function("Decrypt a symmetrically encrypted message", |b| {
82+
let plain = generate_plaintext();
83+
let secrets = generate_secrets();
84+
let encrypted = tokio::runtime::Runtime::new().unwrap().block_on(async {
5785
let secret = secrets[NUM_SECRETS / 2].clone();
5886
let encrypted = encrypt_for_broadcast(
5987
plain.clone(),
@@ -64,7 +92,7 @@ fn criterion_benchmark(c: &mut Criterion) {
6492
.await
6593
.unwrap();
6694

67-
(secrets, encrypted)
95+
encrypted
6896
});
6997

7098
b.iter(|| {
@@ -75,16 +103,12 @@ fn criterion_benchmark(c: &mut Criterion) {
75103
assert_eq!(black_box(decrypted), plain);
76104
});
77105
});
78-
group.bench_function("Decrypt pk encrypted", |b| {
79-
// TODO code duplication with previous benchmark
80-
let rt = tokio::runtime::Runtime::new().unwrap();
81-
let mut plain: Vec<u8> = vec![0; 500];
82-
thread_rng().fill(&mut plain[..]);
106+
107+
group.bench_function("Decrypt a public-key encrypted message", |b| {
108+
let plain = generate_plaintext();
83109
let key_pair = create_dummy_keypair("[email protected]").unwrap();
84-
let (secrets, encrypted) = rt.block_on(async {
85-
let secrets: Vec<String> = (0..NUM_SECRETS)
86-
.map(|_| create_broadcast_shared_secret_pub())
87-
.collect();
110+
let secrets = generate_secrets();
111+
let encrypted = tokio::runtime::Runtime::new().unwrap().block_on(async {
88112
let encrypted = pk_encrypt(
89113
plain.clone(),
90114
vec![black_box(key_pair.public.clone())],
@@ -94,7 +118,7 @@ fn criterion_benchmark(c: &mut Criterion) {
94118
.await
95119
.unwrap();
96120

97-
(secrets, encrypted)
121+
encrypted
98122
});
99123

100124
b.iter(|| {
@@ -110,12 +134,15 @@ fn criterion_benchmark(c: &mut Criterion) {
110134
});
111135
});
112136

137+
// ===========================================================================================
138+
// Benchmarks for the whole parsing pipeline, incl. decryption (but excl. receive_imf())
139+
// ===========================================================================================
140+
113141
let rt = tokio::runtime::Runtime::new().unwrap();
114-
let mut secrets: Vec<String> = (0..NUM_SECRETS)
115-
.map(|_| create_broadcast_shared_secret_pub())
116-
.collect();
142+
let mut secrets = generate_secrets();
117143

118-
// "secret" is the shared secret that was used to encrypt text_symmetrically_encrypted.eml:
144+
// "secret" is the shared secret that was used to encrypt text_symmetrically_encrypted.eml.
145+
// Put it into the middle of our secrets:
119146
secrets[NUM_SECRETS / 2] = "secret".to_string();
120147

121148
let context = rt.block_on(async {
@@ -128,36 +155,51 @@ fn criterion_benchmark(c: &mut Criterion) {
128155
context
129156
});
130157

131-
group.bench_function("Receive a public-key encrypted message", |b| {
158+
group.bench_function("Decrypt and parse a symmetrically encrypted message", |b| {
132159
b.to_async(&rt).iter(|| {
133160
let ctx = context.clone();
134161
async move {
135162
let text = parse_and_get_text(
136163
&ctx,
137-
include_bytes!("../test-data/message/text_from_alice_encrypted.eml"),
164+
include_bytes!("../test-data/message/text_symmetrically_encrypted.eml"),
138165
)
139166
.await
140167
.unwrap();
141-
assert_eq!(text, "hi");
168+
assert_eq!(text, "Symmetrically encrypted message");
142169
}
143170
});
144171
});
145-
group.bench_function("Receive a symmetrically encrypted message", |b| {
172+
173+
group.bench_function("Decrypt and parse a public-key encrypted message", |b| {
146174
b.to_async(&rt).iter(|| {
147175
let ctx = context.clone();
148176
async move {
149177
let text = parse_and_get_text(
150178
&ctx,
151-
include_bytes!("../test-data/message/text_symmetrically_encrypted.eml"),
179+
include_bytes!("../test-data/message/text_from_alice_encrypted.eml"),
152180
)
153181
.await
154182
.unwrap();
155-
assert_eq!(text, "Symmetrically encrypted message");
183+
assert_eq!(text, "hi");
156184
}
157185
});
158186
});
187+
159188
group.finish();
160189
}
161190

191+
fn generate_secrets() -> Vec<String> {
192+
let secrets: Vec<String> = (0..NUM_SECRETS)
193+
.map(|_| create_broadcast_shared_secret())
194+
.collect();
195+
secrets
196+
}
197+
198+
fn generate_plaintext() -> Vec<u8> {
199+
let mut plain: Vec<u8> = vec![0; 500];
200+
thread_rng().fill(&mut plain[..]);
201+
plain
202+
}
203+
162204
criterion_group!(benches, criterion_benchmark);
163205
criterion_main!(benches);

src/benchmark_internals.rs renamed to src/internals_for_benchmarks.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ pub async fn save_broadcast_shared_secret(
3838
pub fn create_dummy_keypair(addr: &str) -> Result<KeyPair> {
3939
pgp::create_keypair(EmailAddress::new(addr)?)
4040
}
41+
42+
pub fn create_broadcast_shared_secret() -> String {
43+
crate::tools::create_broadcast_shared_secret()
44+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub mod peer_channels;
112112
pub mod reaction;
113113

114114
#[cfg(feature = "internals")]
115-
pub mod benchmark_internals;
115+
pub mod internals_for_benchmarks;
116116

117117
/// If set IMAP/incoming and SMTP/outgoing MIME messages will be printed.
118118
pub const DCC_MIME_DEBUG: &str = "DCC_MIME_DEBUG";

src/tools.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,6 @@ pub(crate) fn create_broadcast_shared_secret() -> String {
319319
res
320320
}
321321

322-
#[cfg(feature = "internals")]
323-
pub fn create_broadcast_shared_secret_pub() -> String {
324-
create_broadcast_shared_secret()
325-
}
326-
327322
/// Returns true if given string is a valid ID.
328323
///
329324
/// All IDs generated with `create_id()` should be considered valid.

0 commit comments

Comments
 (0)