|
| 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