|
| 1 | +use criterion::*; |
| 2 | +use matrix_sdk::{config::StoreConfig, Client, RoomInfo, RoomState, Session, StateChanges}; |
| 3 | +use matrix_sdk_base::{store::MemoryStore, StateStore as _}; |
| 4 | +use matrix_sdk_sled::SledStateStore; |
| 5 | +use matrix_sdk_sqlite::SqliteStateStore; |
| 6 | +use ruma::{device_id, user_id, RoomId}; |
| 7 | +use tokio::runtime::Builder; |
| 8 | + |
| 9 | +fn criterion() -> Criterion { |
| 10 | + #[cfg(target_os = "linux")] |
| 11 | + let criterion = Criterion::default().with_profiler(pprof::criterion::PProfProfiler::new( |
| 12 | + 100, |
| 13 | + pprof::criterion::Output::Flamegraph(None), |
| 14 | + )); |
| 15 | + |
| 16 | + #[cfg(not(target_os = "linux"))] |
| 17 | + let criterion = Criterion::default(); |
| 18 | + |
| 19 | + criterion |
| 20 | +} |
| 21 | + |
| 22 | +/// Number of joined rooms in the benchmark. |
| 23 | +const NUM_JOINED_ROOMS: usize = 10000; |
| 24 | + |
| 25 | +/// Number of stripped rooms in the benchmark. |
| 26 | +const NUM_STRIPPED_JOINED_ROOMS: usize = 10000; |
| 27 | + |
| 28 | +pub fn restore_session(c: &mut Criterion) { |
| 29 | + let runtime = Builder::new_multi_thread().build().expect("Can't create runtime"); |
| 30 | + |
| 31 | + // Create a fake list of changes, and a session to recover from. |
| 32 | + let mut changes = StateChanges::default(); |
| 33 | + |
| 34 | + for i in 0..NUM_JOINED_ROOMS { |
| 35 | + let room_id = RoomId::parse(format!("!room{i}:example.com")).unwrap().to_owned(); |
| 36 | + changes.add_room(RoomInfo::new(&room_id, RoomState::Joined)); |
| 37 | + } |
| 38 | + |
| 39 | + for i in 0..NUM_STRIPPED_JOINED_ROOMS { |
| 40 | + let room_id = RoomId::parse(format!("!strippedroom{i}:example.com")).unwrap().to_owned(); |
| 41 | + changes.add_stripped_room(RoomInfo::new(&room_id, RoomState::Joined)); |
| 42 | + } |
| 43 | + |
| 44 | + let session = Session { |
| 45 | + access_token: "OHEY".to_owned(), |
| 46 | + refresh_token: None, |
| 47 | + user_id: user_id!("@somebody:example.com").to_owned(), |
| 48 | + device_id: device_id!("DEVICE_ID").to_owned(), |
| 49 | + }; |
| 50 | + |
| 51 | + // Start the benchmark. |
| 52 | + |
| 53 | + let mut group = c.benchmark_group("Client reload"); |
| 54 | + group.throughput(Throughput::Elements(100)); |
| 55 | + |
| 56 | + const NAME: &str = "restore a session"; |
| 57 | + |
| 58 | + // Memory |
| 59 | + let mem_store = MemoryStore::new(); |
| 60 | + runtime.block_on(mem_store.save_changes(&changes)).expect("initial filling of mem failed"); |
| 61 | + |
| 62 | + group.bench_with_input(BenchmarkId::new("memory store", NAME), &mem_store, |b, store| { |
| 63 | + b.to_async(&runtime).iter(|| async { |
| 64 | + let client = Client::builder() |
| 65 | + .homeserver_url("https://matrix.example.com") |
| 66 | + .store_config(StoreConfig::new().state_store(store.clone())) |
| 67 | + .build() |
| 68 | + .await |
| 69 | + .expect("Can't build client"); |
| 70 | + client.restore_session(session.clone()).await.expect("couldn't restore session"); |
| 71 | + }) |
| 72 | + }); |
| 73 | + |
| 74 | + for encryption_password in [None, Some("hunter2")] { |
| 75 | + let encrypted_suffix = if encryption_password.is_some() { "encrypted" } else { "clear" }; |
| 76 | + |
| 77 | + // Sled |
| 78 | + let sled_path = tempfile::tempdir().unwrap().path().to_path_buf(); |
| 79 | + let mut sled_store_builder = SledStateStore::builder().path(sled_path); |
| 80 | + if let Some(password) = encryption_password { |
| 81 | + sled_store_builder = sled_store_builder.passphrase(password.to_owned()); |
| 82 | + } |
| 83 | + let sled_store = sled_store_builder.build().expect("Can't create sled store"); |
| 84 | + runtime |
| 85 | + .block_on(sled_store.save_changes(&changes)) |
| 86 | + .expect("initial filling of sled failed"); |
| 87 | + |
| 88 | + group.bench_with_input( |
| 89 | + BenchmarkId::new(format!("sled store {encrypted_suffix}"), NAME), |
| 90 | + &sled_store, |
| 91 | + |b, store| { |
| 92 | + b.to_async(&runtime).iter(|| async { |
| 93 | + let client = Client::builder() |
| 94 | + .homeserver_url("https://matrix.example.com") |
| 95 | + .store_config(StoreConfig::new().state_store(store.clone())) |
| 96 | + .build() |
| 97 | + .await |
| 98 | + .expect("Can't build client"); |
| 99 | + client |
| 100 | + .restore_session(session.clone()) |
| 101 | + .await |
| 102 | + .expect("couldn't restore session"); |
| 103 | + }) |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + // Sqlite |
| 108 | + let sqlite_dir = tempfile::tempdir().unwrap(); |
| 109 | + let sqlite_store = runtime |
| 110 | + .block_on(SqliteStateStore::open(sqlite_dir.path(), encryption_password)) |
| 111 | + .unwrap(); |
| 112 | + runtime |
| 113 | + .block_on(sqlite_store.save_changes(&changes)) |
| 114 | + .expect("initial filling of sqlite failed"); |
| 115 | + |
| 116 | + group.bench_with_input( |
| 117 | + BenchmarkId::new(format!("sqlite store {encrypted_suffix}"), NAME), |
| 118 | + &sqlite_store, |
| 119 | + |b, store| { |
| 120 | + b.to_async(&runtime).iter(|| async { |
| 121 | + let client = Client::builder() |
| 122 | + .homeserver_url("https://matrix.example.com") |
| 123 | + .store_config(StoreConfig::new().state_store(store.clone())) |
| 124 | + .build() |
| 125 | + .await |
| 126 | + .expect("Can't build client"); |
| 127 | + client |
| 128 | + .restore_session(session.clone()) |
| 129 | + .await |
| 130 | + .expect("couldn't restore session"); |
| 131 | + }) |
| 132 | + }, |
| 133 | + ); |
| 134 | + |
| 135 | + { |
| 136 | + let _guard = runtime.enter(); |
| 137 | + drop(sqlite_store); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + group.finish() |
| 142 | +} |
| 143 | + |
| 144 | +criterion_group! { |
| 145 | + name = benches; |
| 146 | + config = criterion(); |
| 147 | + targets = restore_session |
| 148 | +} |
| 149 | +criterion_main!(benches); |
0 commit comments