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
+
1
27
use std:: hint:: black_box;
2
28
3
29
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;
6
33
use deltachat:: {
7
34
Events ,
8
- benchmark_internals:: key_from_asc,
9
- benchmark_internals:: parse_and_get_text,
10
- benchmark_internals:: store_self_keypair,
11
35
chat:: ChatId ,
12
36
config:: Config ,
13
37
context:: Context ,
38
+ internals_for_benchmarks:: key_from_asc,
39
+ internals_for_benchmarks:: parse_and_get_text,
40
+ internals_for_benchmarks:: store_self_keypair,
14
41
pgp:: { KeyPair , decrypt, encrypt_for_broadcast, pk_encrypt} ,
15
42
stock_str:: StockStrings ,
16
- tools:: create_broadcast_shared_secret_pub,
17
43
} ;
18
44
use rand:: { Rng , thread_rng} ;
19
45
use tempfile:: tempdir;
@@ -45,15 +71,17 @@ async fn create_context() -> Context {
45
71
46
72
fn criterion_benchmark ( c : & mut Criterion ) {
47
73
let mut group = c. benchmark_group ( "Decrypt" ) ;
74
+
75
+ // ===========================================================================================
76
+ // Benchmarks for decryption only, without any other parsing
77
+ // ===========================================================================================
78
+
48
79
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 {
57
85
let secret = secrets[ NUM_SECRETS / 2 ] . clone ( ) ;
58
86
let encrypted = encrypt_for_broadcast (
59
87
plain. clone ( ) ,
@@ -64,7 +92,7 @@ fn criterion_benchmark(c: &mut Criterion) {
64
92
. await
65
93
. unwrap ( ) ;
66
94
67
- ( secrets , encrypted)
95
+ encrypted
68
96
} ) ;
69
97
70
98
b. iter ( || {
@@ -75,16 +103,12 @@ fn criterion_benchmark(c: &mut Criterion) {
75
103
assert_eq ! ( black_box( decrypted) , plain) ;
76
104
} ) ;
77
105
} ) ;
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 ( ) ;
83
109
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 {
88
112
let encrypted = pk_encrypt (
89
113
plain. clone ( ) ,
90
114
vec ! [ black_box( key_pair. public. clone( ) ) ] ,
@@ -94,7 +118,7 @@ fn criterion_benchmark(c: &mut Criterion) {
94
118
. await
95
119
. unwrap ( ) ;
96
120
97
- ( secrets , encrypted)
121
+ encrypted
98
122
} ) ;
99
123
100
124
b. iter ( || {
@@ -110,12 +134,15 @@ fn criterion_benchmark(c: &mut Criterion) {
110
134
} ) ;
111
135
} ) ;
112
136
137
+ // ===========================================================================================
138
+ // Benchmarks for the whole parsing pipeline, incl. decryption (but excl. receive_imf())
139
+ // ===========================================================================================
140
+
113
141
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 ( ) ;
117
143
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:
119
146
secrets[ NUM_SECRETS / 2 ] = "secret" . to_string ( ) ;
120
147
121
148
let context = rt. block_on ( async {
@@ -128,36 +155,51 @@ fn criterion_benchmark(c: &mut Criterion) {
128
155
context
129
156
} ) ;
130
157
131
- group. bench_function ( "Receive a public-key encrypted message" , |b| {
158
+ group. bench_function ( "Decrypt and parse a symmetrically encrypted message" , |b| {
132
159
b. to_async ( & rt) . iter ( || {
133
160
let ctx = context. clone ( ) ;
134
161
async move {
135
162
let text = parse_and_get_text (
136
163
& ctx,
137
- include_bytes ! ( "../test-data/message/text_from_alice_encrypted .eml" ) ,
164
+ include_bytes ! ( "../test-data/message/text_symmetrically_encrypted .eml" ) ,
138
165
)
139
166
. await
140
167
. unwrap ( ) ;
141
- assert_eq ! ( text, "hi " ) ;
168
+ assert_eq ! ( text, "Symmetrically encrypted message " ) ;
142
169
}
143
170
} ) ;
144
171
} ) ;
145
- group. bench_function ( "Receive a symmetrically encrypted message" , |b| {
172
+
173
+ group. bench_function ( "Decrypt and parse a public-key encrypted message" , |b| {
146
174
b. to_async ( & rt) . iter ( || {
147
175
let ctx = context. clone ( ) ;
148
176
async move {
149
177
let text = parse_and_get_text (
150
178
& ctx,
151
- include_bytes ! ( "../test-data/message/text_symmetrically_encrypted .eml" ) ,
179
+ include_bytes ! ( "../test-data/message/text_from_alice_encrypted .eml" ) ,
152
180
)
153
181
. await
154
182
. unwrap ( ) ;
155
- assert_eq ! ( text, "Symmetrically encrypted message " ) ;
183
+ assert_eq ! ( text, "hi " ) ;
156
184
}
157
185
} ) ;
158
186
} ) ;
187
+
159
188
group. finish ( ) ;
160
189
}
161
190
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
+
162
204
criterion_group ! ( benches, criterion_benchmark) ;
163
205
criterion_main ! ( benches) ;
0 commit comments