diff --git a/matrix_sdk_crypto/benches/crypto_bench.rs b/matrix_sdk_crypto/benches/crypto_bench.rs index dabc7918..baaf51c5 100644 --- a/matrix_sdk_crypto/benches/crypto_bench.rs +++ b/matrix_sdk_crypto/benches/crypto_bench.rs @@ -7,11 +7,14 @@ use criterion::{async_executor::FuturesExecutor, *}; use futures::executor::block_on; use matrix_sdk_common::{ - api::r0::keys::{claim_keys, get_keys}, - identifiers::{user_id, DeviceIdBox, UserId}, + api::r0::{ + keys::{claim_keys, get_keys}, + to_device::send_event_to_device::Response as ToDeviceResponse, + }, + identifiers::{room_id, user_id, DeviceIdBox, UserId}, uuid::Uuid, }; -use matrix_sdk_crypto::OlmMachine; +use matrix_sdk_crypto::{EncryptionSettings, OlmMachine}; use matrix_sdk_test::response_from_file; use serde_json::Value; @@ -101,6 +104,82 @@ pub fn keys_claiming(c: &mut Criterion) { group.finish() } +pub fn room_key_sharing(c: &mut Criterion) { + let keys_query_response = keys_query_response(); + let uuid = Uuid::new_v4(); + let response = keys_claim_response(); + let room_id = room_id!("!test:localhost"); + + let to_device_response = ToDeviceResponse::new(); + let users: Vec = keys_query_response.device_keys.keys().cloned().collect(); + + let count = response + .one_time_keys + .values() + .fold(0, |acc, d| acc + d.len()); + + let machine = OlmMachine::new(&alice_id(), &alice_device_id()); + block_on(machine.mark_request_as_sent(&uuid, &keys_query_response)).unwrap(); + block_on(machine.mark_request_as_sent(&uuid, &response)).unwrap(); + + let mut group = c.benchmark_group("Room key sharing"); + group.throughput(Throughput::Elements(count as u64)); + let name = format!("{} devices", count); + + group.bench_function(BenchmarkId::new("memory store", &name), |b| { + b.to_async(FuturesExecutor).iter(|| async { + let requests = machine + .share_group_session(&room_id, users.iter(), EncryptionSettings::default()) + .await + .unwrap(); + + assert!(requests.len() >= 8); + + for request in requests { + machine + .mark_request_as_sent(&request.txn_id, &to_device_response) + .await + .unwrap(); + } + + machine.invalidate_group_session(&room_id); + }) + }); + + let dir = tempfile::tempdir().unwrap(); + let machine = block_on(OlmMachine::new_with_default_store( + &alice_id(), + &alice_device_id(), + dir.path(), + None, + )) + .unwrap(); + block_on(machine.mark_request_as_sent(&uuid, &keys_query_response)).unwrap(); + block_on(machine.mark_request_as_sent(&uuid, &response)).unwrap(); + + group.bench_function(BenchmarkId::new("sled store", &name), |b| { + b.to_async(FuturesExecutor).iter(|| async { + let requests = machine + .share_group_session(&room_id, users.iter(), EncryptionSettings::default()) + .await + .unwrap(); + + assert!(requests.len() >= 8); + + for request in requests { + machine + .mark_request_as_sent(&request.txn_id, &to_device_response) + .await + .unwrap(); + } + + machine.invalidate_group_session(&room_id); + }) + }); + + group.finish() +} + fn criterion() -> Criterion { #[cfg(target_os = "linux")] let criterion = Criterion::default().with_profiler(perf::FlamegraphProfiler::new(100)); @@ -113,6 +192,6 @@ fn criterion() -> Criterion { criterion_group! { name = benches; config = criterion(); - targets = keys_query, keys_claiming + targets = keys_query, keys_claiming, room_key_sharing } criterion_main!(benches);