Skip to content

Commit 6468806

Browse files
authored
test: Fix panic in receive_emails benchmark (#6306)
The benchmark function (e.g. `recv_all_emails()`) is executed multiple times on the same context. During the second iteration, all the emails were already in the database, so, receiving them again failed. This PR fixes that by passing in a second `iteration` counter that is different for every invocation of the benchmark function.
1 parent 825455d commit 6468806

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

benches/receive_emails.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ use deltachat::{
1212
};
1313
use tempfile::tempdir;
1414

15-
async fn recv_all_emails(context: Context) -> Context {
15+
async fn recv_all_emails(context: Context, iteration: u32) -> Context {
1616
for i in 0..100 {
1717
let imf_raw = format!(
1818
"Subject: Benchmark
19-
Message-ID: Mr.OssSYnOFkhR.{i}@testrun.org
19+
Message-ID: Mr.{iteration}.{i}@testrun.org
2020
Date: Sat, 07 Dec 2019 19:00:27 +0000
2121
To: alice@example.com
2222
From: sender@testrun.org
2323
Chat-Version: 1.0
2424
Chat-Disposition-Notification-To: sender@testrun.org
2525
Chat-User-Avatar: 0
26-
In-Reply-To: Mr.OssSYnOFkhR.{i_dec}@testrun.org
26+
In-Reply-To: Mr.{iteration}.{i_dec}@testrun.org
2727
MIME-Version: 1.0
2828
2929
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
@@ -41,25 +41,24 @@ Hello {i}",
4141

4242
/// Receive 100 emails that remove charlie@example.com and add
4343
/// him back
44-
async fn recv_groupmembership_emails(context: Context) -> Context {
44+
async fn recv_groupmembership_emails(context: Context, iteration: u32) -> Context {
4545
for i in 0..50 {
4646
let imf_raw = format!(
4747
"Subject: Benchmark
48-
Message-ID: Gr.OssSYnOFkhR.{i}@testrun.org
48+
Message-ID: Gr.{iteration}.ADD.{i}@testrun.org
4949
Date: Sat, 07 Dec 2019 19:00:27 +0000
5050
To: alice@example.com, b@example.com, c@example.com, d@example.com, e@example.com, f@example.com
5151
From: sender@testrun.org
5252
Chat-Version: 1.0
5353
Chat-Disposition-Notification-To: sender@testrun.org
5454
Chat-User-Avatar: 0
5555
Chat-Group-Member-Added: charlie@example.com
56-
In-Reply-To: Gr.OssSYnOFkhR.{i_dec}@testrun.org
56+
In-Reply-To: Gr.{iteration}.REMOVE.{i_dec}@testrun.org
5757
MIME-Version: 1.0
5858
5959
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
6060
6161
Hello {i}",
62-
i = i,
6362
i_dec = i - 1,
6463
);
6564
receive_imf(&context, black_box(imf_raw.as_bytes()), false)
@@ -68,22 +67,20 @@ Hello {i}",
6867

6968
let imf_raw = format!(
7069
"Subject: Benchmark
71-
Message-ID: Gr.OssSYnOFkhR.{i}@testrun.org
70+
Message-ID: Gr.{iteration}.REMOVE.{i}@testrun.org
7271
Date: Sat, 07 Dec 2019 19:00:27 +0000
7372
To: alice@example.com, b@example.com, c@example.com, d@example.com, e@example.com, f@example.com
7473
From: sender@testrun.org
7574
Chat-Version: 1.0
7675
Chat-Disposition-Notification-To: sender@testrun.org
7776
Chat-User-Avatar: 0
7877
Chat-Group-Member-Removed: charlie@example.com
79-
In-Reply-To: Gr.OssSYnOFkhR.{i_dec}@testrun.org
78+
In-Reply-To: Gr.{iteration}.ADD.{i}@testrun.org
8079
MIME-Version: 1.0
8180
8281
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
8382
84-
Hello {i}",
85-
i = i,
86-
i_dec = i - 1,
83+
Hello {i}"
8784
);
8885
receive_imf(&context, black_box(imf_raw.as_bytes()), false)
8986
.await
@@ -129,11 +126,13 @@ fn criterion_benchmark(c: &mut Criterion) {
129126
group.bench_function("Receive 100 simple text msgs", |b| {
130127
let rt = tokio::runtime::Runtime::new().unwrap();
131128
let context = rt.block_on(create_context());
129+
let mut i = 0;
132130

133131
b.to_async(&rt).iter(|| {
134132
let ctx = context.clone();
133+
i += 1;
135134
async move {
136-
recv_all_emails(black_box(ctx)).await;
135+
recv_all_emails(black_box(ctx), i).await;
137136
}
138137
});
139138
});
@@ -142,11 +141,13 @@ fn criterion_benchmark(c: &mut Criterion) {
142141
|b| {
143142
let rt = tokio::runtime::Runtime::new().unwrap();
144143
let context = rt.block_on(create_context());
144+
let mut i = 0;
145145

146146
b.to_async(&rt).iter(|| {
147147
let ctx = context.clone();
148+
i += 1;
148149
async move {
149-
recv_groupmembership_emails(black_box(ctx)).await;
150+
recv_groupmembership_emails(black_box(ctx), i).await;
150151
}
151152
});
152153
},

0 commit comments

Comments
 (0)