7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | use std::{ops::Deref, sync::Arc}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | use matrix_sdk_crypto::{EncryptionSettings, OlmMachine}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | use matrix_sdk_sqlite::SqliteCryptoStore; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | use matrix_sdk_test::ruma_response_from_json; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | use ruma::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | DeviceId, OwnedUserId, TransactionId, UserId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | api::client::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | keys::{claim_keys, get_keys}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | to_device::send_event_to_device::v3::Response as ToDeviceResponse, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | device_id, room_id, user_id, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use serde_json::Value; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | use tokio::runtime::Builder; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | fn alice_id() -> &'static UserId { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | user_id!("@alice:example.org") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | fn alice_device_id() -> &'static DeviceId { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | device_id!("JLAFKJWSCS") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | fn keys_query_response() -> get_keys::v3::Response { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | let data = include_bytes!("crypto_bench/keys_query.json"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | let data: Value = serde_json::from_slice(data).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | ruma_response_from_json(&data) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | fn keys_claim_response() -> claim_keys::v3::Response { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | let data = include_bytes!("crypto_bench/keys_claim.json"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | let data: Value = serde_json::from_slice(data).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | ruma_response_from_json(&data) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | fn huge_keys_query_response() -> get_keys::v3::Response { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | let data = include_bytes!("crypto_bench/keys_query_2000_members.json"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | let data: Value = serde_json::from_slice(data).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | ruma_response_from_json(&data) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | pub fn keys_query(c: &mut Criterion) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | let runtime = Builder::new_multi_thread().build().expect("Can't create runtime"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | let machine = runtime.block_on(OlmMachine::new(alice_id(), alice_device_id())); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | let response = keys_query_response(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | let txn_id = TransactionId::new(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | let count = response.device_keys.values().fold(0, |acc, d| acc + d.len()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | + response.master_keys.len() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | + response.self_signing_keys.len() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | + response.user_signing_keys.len(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | let mut group = c.benchmark_group("Keys querying"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | group.throughput(Throughput::Elements(count as u64)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | let name = format!("{count} device and cross signing keys"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | // Benchmark memory store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | group.bench_with_input( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | BenchmarkId::new("Device keys query [memory]", &name), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | &response, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | |b, response| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | b.to_async(&runtime) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | .iter(|| async { machine.mark_request_as_sent(&txn_id, response).await.unwrap() }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | // Benchmark sqlite store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | let dir = tempfile::tempdir().unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | let store = Arc::new(runtime.block_on(SqliteCryptoStore::open(dir.path(), None)).unwrap()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | let machine = runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | .block_on(OlmMachine::with_store(alice_id(), alice_device_id(), store, None)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | group.bench_with_input( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | BenchmarkId::new("Device keys query [SQLite]", &name), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | &response, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | |b, response| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | b.to_async(&runtime) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | .iter(|| async { machine.mark_request_as_sent(&txn_id, response).await.unwrap() }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | let _guard = runtime.enter(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | drop(machine); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | group.finish() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | /// This test panics on the CI, not sure why so we're disabling it for now. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | #[cfg(not(feature = "codspeed"))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | pub fn keys_claiming(c: &mut Criterion) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | let runtime = Builder::new_multi_thread().build().expect("Can't create runtime"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | let keys_query_response = keys_query_response(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | let txn_id = TransactionId::new(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | let response = keys_claim_response(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 106 | let count = response.one_time_keys.values().fold(0, |acc, d| acc + d.len()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 107 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 108 | let mut group = c.benchmark_group("Olm session creation"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 109 | group.throughput(Throughput::Elements(count as u64)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 110 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 111 | let name = format!("{count} one-time keys"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 112 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 113 | group.bench_with_input( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 114 | BenchmarkId::new("One-time keys claiming [memory]", &name), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 115 | &response, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 116 | |b, response| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 117 | b.iter_batched( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 118 | || { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 119 | let machine = runtime.block_on(OlmMachine::new(alice_id(), alice_device_id())); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 120 | runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 121 | .block_on(machine.mark_request_as_sent(&txn_id, &keys_query_response)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 122 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 123 | (machine, &runtime, &txn_id) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 124 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 125 | move |(machine, runtime, txn_id)| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 126 | runtime.block_on(async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 127 | machine.mark_request_as_sent(txn_id, response).await.unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 128 | drop(machine); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 129 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 130 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 131 | criterion::BatchSize::SmallInput, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 132 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 133 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 134 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 135 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 136 | group.bench_with_input( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 137 | BenchmarkId::new("One-time keys claiming [SQLite]", &name), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 138 | &response, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 139 | |b, response| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 140 | b.iter_batched( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 141 | || { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 142 | let dir = tempfile::tempdir().unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 143 | let store = Arc::new( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 144 | runtime.block_on(SqliteCryptoStore::open(dir.path(), None)).unwrap(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 145 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 146 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 147 | let machine = runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 148 | .block_on(OlmMachine::with_store( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 149 | alice_id(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 150 | alice_device_id(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 151 | store, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 152 | None, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 153 | )) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 154 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 155 | runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 156 | .block_on(machine.mark_request_as_sent(&txn_id, &keys_query_response)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 157 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 158 | (machine, &runtime, &txn_id) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 159 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 160 | move |(machine, runtime, txn_id)| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 161 | runtime.block_on(async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 162 | machine.mark_request_as_sent(txn_id, response).await.unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 163 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 164 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 165 | let _ = runtime.enter(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 166 | drop(machine); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 167 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 168 | criterion::BatchSize::SmallInput, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 169 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 170 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 171 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 172 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 173 | group.finish() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 174 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 175 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 176 | pub fn room_key_sharing(c: &mut Criterion) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 177 | let runtime = Builder::new_multi_thread().build().expect("Can't create runtime"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 178 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 179 | let keys_query_response = keys_query_response(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 180 | let txn_id = TransactionId::new(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 181 | let response = keys_claim_response(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 182 | let room_id = room_id!("!test:localhost"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 183 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 184 | let to_device_response = ToDeviceResponse::new(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 185 | let users: Vec<OwnedUserId> = keys_query_response.device_keys.keys().cloned().collect(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 186 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 187 | let count = response.one_time_keys.values().fold(0, |acc, d| acc + d.len()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 188 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 189 | let machine = runtime.block_on(OlmMachine::new(alice_id(), alice_device_id())); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 190 | runtime.block_on(machine.mark_request_as_sent(&txn_id, &keys_query_response)).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 191 | runtime.block_on(machine.mark_request_as_sent(&txn_id, &response)).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 192 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 193 | let mut group = c.benchmark_group("Room key sharing"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 194 | group.throughput(Throughput::Elements(count as u64)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 195 | let name = format!("{count} devices"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 196 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 197 | // Benchmark memory store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 198 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 199 | group.bench_function(BenchmarkId::new("Room key sharing [memory]", &name), |b| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 200 | b.to_async(&runtime).iter(|| async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 201 | let requests = machine |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 202 | .share_room_key( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 203 | room_id, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 204 | users.iter().map(Deref::deref), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 205 | EncryptionSettings::default(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 206 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 207 | .await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 208 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 209 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 210 | assert!(!requests.is_empty()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 211 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 212 | for request in requests { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 213 | machine.mark_request_as_sent(&request.txn_id, &to_device_response).await.unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 214 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 215 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 216 | machine.discard_room_key(room_id).await.unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 217 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 218 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 219 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 220 | // Benchmark sqlite store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 221 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 222 | let dir = tempfile::tempdir().unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 223 | let store = Arc::new(runtime.block_on(SqliteCryptoStore::open(dir.path(), None)).unwrap()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 224 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 225 | let machine = runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 226 | .block_on(OlmMachine::with_store(alice_id(), alice_device_id(), store, None)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 227 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 228 | runtime.block_on(machine.mark_request_as_sent(&txn_id, &keys_query_response)).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 229 | runtime.block_on(machine.mark_request_as_sent(&txn_id, &response)).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 230 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 231 | group.bench_function(BenchmarkId::new("Room key sharing [SQLite]", &name), |b| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 232 | b.to_async(&runtime).iter(|| async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 233 | let requests = machine |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 234 | .share_room_key( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 235 | room_id, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 236 | users.iter().map(Deref::deref), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 237 | EncryptionSettings::default(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 238 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 239 | .await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 240 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 241 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 242 | assert!(!requests.is_empty()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 243 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 244 | for request in requests { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 245 | machine.mark_request_as_sent(&request.txn_id, &to_device_response).await.unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 246 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 247 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 248 | machine.discard_room_key(room_id).await.unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 249 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 250 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 251 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 252 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 253 | let _guard = runtime.enter(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 254 | drop(machine); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 255 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 256 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 257 | group.finish() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 258 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 259 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 260 | pub fn devices_missing_sessions_collecting(c: &mut Criterion) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 261 | let runtime = Builder::new_multi_thread().build().expect("Can't create runtime"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 262 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 263 | let machine = runtime.block_on(OlmMachine::new(alice_id(), alice_device_id())); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 264 | let response = huge_keys_query_response(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 265 | let txn_id = TransactionId::new(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 266 | let users: Vec<OwnedUserId> = response.device_keys.keys().cloned().collect(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 267 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 268 | let count = response.device_keys.values().fold(0, |acc, d| acc + d.len()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 269 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 270 | let mut group = c.benchmark_group("Devices missing sessions collecting"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 271 | group.throughput(Throughput::Elements(count as u64)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 272 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 273 | let name = format!("{count} devices"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 274 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 275 | runtime.block_on(machine.mark_request_as_sent(&txn_id, &response)).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 276 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 277 | // Benchmark memory store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 278 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 279 | group.bench_function(BenchmarkId::new("Devices collecting [memory]", &name), |b| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 280 | b.to_async(&runtime).iter_with_large_drop(|| async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 281 | machine.get_missing_sessions(users.iter().map(Deref::deref)).await.unwrap() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 282 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 283 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 284 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 285 | // Benchmark sqlite store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 286 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 287 | let dir = tempfile::tempdir().unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 288 | let store = Arc::new(runtime.block_on(SqliteCryptoStore::open(dir.path(), None)).unwrap()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 289 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 290 | let machine = runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 291 | .block_on(OlmMachine::with_store(alice_id(), alice_device_id(), store, None)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 292 | .unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 293 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 294 | runtime.block_on(machine.mark_request_as_sent(&txn_id, &response)).unwrap(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 295 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 296 | group.bench_function(BenchmarkId::new("Devices collecting [SQLite]", &name), |b| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 297 | b.to_async(&runtime).iter(|| async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 298 | machine.get_missing_sessions(users.iter().map(Deref::deref)).await.unwrap() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 299 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 300 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 301 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 302 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 303 | let _guard = runtime.enter(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 304 | drop(machine); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 305 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 306 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 307 | group.finish() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 308 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 309 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 310 | #[cfg(not(feature = "codspeed"))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 311 | criterion_group! { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 312 | name = benches; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 313 | config = Criterion::default(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 314 | targets = keys_query, keys_claiming, room_key_sharing, devices_missing_sessions_collecting, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 315 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 316 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 317 | #[cfg(feature = "codspeed")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 318 | criterion_group! { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 319 | name = benches; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 320 | config = Criterion::default(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 321 | targets = keys_query, room_key_sharing, devices_missing_sessions_collecting, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 322 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 323 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 324 | criterion_main!(benches); |