Skip to content

Commit 74ab452

Browse files
committed
Improve benchmark
1 parent 6801e3d commit 74ab452

File tree

3 files changed

+100
-24
lines changed

3 files changed

+100
-24
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ harness = false
158158
name = "get_chat_msgs"
159159
harness = false
160160

161+
[[bench]]
162+
name = "marknoticed_chat"
163+
harness = false
164+
161165
[[bench]]
162166
name = "get_chatlist"
163167
harness = false

benches/get_chat_msgs.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use deltachat::chatlist::Chatlist;
77
use deltachat::context::Context;
88
use deltachat::stock_str::StockStrings;
99
use deltachat::Events;
10-
use tempfile::tempdir;
1110

1211
async fn get_chat_msgs_benchmark(dbfile: &Path, chats: &[ChatId]) {
1312
let id = 100;
@@ -20,17 +19,6 @@ async fn get_chat_msgs_benchmark(dbfile: &Path, chats: &[ChatId]) {
2019
}
2120
}
2221

23-
async fn marknoticed_chat_benchmark(dbfile: &Path, chats: &[ChatId]) {
24-
let id = 100;
25-
let context = Context::new(dbfile, id, Events::new(), StockStrings::new())
26-
.await
27-
.unwrap();
28-
29-
for c in chats.iter().take(20) {
30-
black_box(chat::marknoticed_chat(&context, *c).await.unwrap());
31-
}
32-
}
33-
3422
fn criterion_benchmark(c: &mut Criterion) {
3523
// To enable this benchmark, set `DELTACHAT_BENCHMARK_DATABASE` to some large database with many
3624
// messages, such as your primary account.
@@ -46,19 +34,9 @@ fn criterion_benchmark(c: &mut Criterion) {
4634
(0..len).map(|i| chatlist.get_chat_id(i).unwrap()).collect()
4735
});
4836

49-
// c.bench_function("chat::get_chat_msgs (load messages from 10 chats)", |b| {
50-
// b.to_async(&rt)
51-
// .iter(|| get_chat_msgs_benchmark(black_box(path.as_ref()), black_box(&chats)))
52-
// });
53-
54-
c.bench_function("chat::marknoticed_chat (mark 10 chats as noticed)", |b| {
55-
let dir = tempdir().unwrap();
56-
let dir = dir.path();
57-
let new_db = dir.join("dc.db");
58-
std::fs::copy(&path, &new_db).unwrap();
59-
37+
c.bench_function("chat::get_chat_msgs (load messages from 10 chats)", |b| {
6038
b.to_async(&rt)
61-
.iter(|| marknoticed_chat_benchmark(black_box(new_db.as_ref()), black_box(&chats)))
39+
.iter(|| get_chat_msgs_benchmark(black_box(path.as_ref()), black_box(&chats)))
6240
});
6341
} else {
6442
println!("env var not set: DELTACHAT_BENCHMARK_DATABASE");

benches/marknoticed_chat.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#![recursion_limit = "256"]
2+
use std::path::Path;
3+
4+
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
5+
use deltachat::chat::{self, ChatId};
6+
use deltachat::chatlist::Chatlist;
7+
use deltachat::context::Context;
8+
use deltachat::stock_str::StockStrings;
9+
use deltachat::Events;
10+
use futures_lite::future::block_on;
11+
use tempfile::tempdir;
12+
13+
async fn marknoticed_chat_benchmark(context: &Context, chats: &[ChatId]) {
14+
for c in chats.iter().take(20) {
15+
chat::marknoticed_chat(context, *c).await.unwrap();
16+
}
17+
}
18+
19+
fn criterion_benchmark(c: &mut Criterion) {
20+
// To enable this benchmark, set `DELTACHAT_BENCHMARK_DATABASE` to some large database with many
21+
// messages, such as your primary account.
22+
if let Ok(path) = std::env::var("DELTACHAT_BENCHMARK_DATABASE") {
23+
let rt = tokio::runtime::Runtime::new().unwrap();
24+
25+
let chats: Vec<_> = rt.block_on(async {
26+
let context = Context::new(Path::new(&path), 100, Events::new(), StockStrings::new())
27+
.await
28+
.unwrap();
29+
let chatlist = Chatlist::try_load(&context, 0, None, None).await.unwrap();
30+
let len = chatlist.len();
31+
(1..len).map(|i| chatlist.get_chat_id(i).unwrap()).collect()
32+
});
33+
34+
// This mainly tests the performance of marknoticed_chat()
35+
// when nothing has to be done
36+
c.bench_function(
37+
"chat::marknoticed_chat (mark 20 chats as noticed repeatedly)",
38+
|b| {
39+
let dir = tempdir().unwrap();
40+
let dir = dir.path();
41+
let new_db = dir.join("dc.db");
42+
std::fs::copy(&path, &new_db).unwrap();
43+
44+
let context = block_on(async {
45+
Context::new(Path::new(&new_db), 100, Events::new(), StockStrings::new())
46+
.await
47+
.unwrap()
48+
});
49+
50+
b.to_async(&rt)
51+
.iter(|| marknoticed_chat_benchmark(&context, black_box(&chats)))
52+
},
53+
);
54+
55+
// If the first 20 chats contain fresh messages or reactions,
56+
// this tests the performance of marking them as noticed.
57+
c.bench_function(
58+
"chat::marknoticed_chat (mark 20 chats as noticed, resetting after every iteration)",
59+
|b| {
60+
b.to_async(&rt).iter_batched(
61+
|| {
62+
let dir = tempdir().unwrap();
63+
let new_db = dir.path().join("dc.db");
64+
std::fs::copy(&path, &new_db).unwrap();
65+
66+
let context = block_on(async {
67+
Context::new(
68+
Path::new(&new_db),
69+
100,
70+
Events::new(),
71+
StockStrings::new(),
72+
)
73+
.await
74+
.unwrap()
75+
});
76+
(dir, context)
77+
},
78+
|(_dir, context)| {
79+
let chats = &chats;
80+
async move {
81+
marknoticed_chat_benchmark(black_box(&context), black_box(chats)).await
82+
}
83+
},
84+
BatchSize::PerIteration,
85+
);
86+
},
87+
);
88+
} else {
89+
println!("env var not set: DELTACHAT_BENCHMARK_DATABASE");
90+
}
91+
}
92+
93+
criterion_group!(benches, criterion_benchmark);
94+
criterion_main!(benches);

0 commit comments

Comments
 (0)