@@ -2,6 +2,7 @@ use std::{ops::Deref, sync::Arc};
22
33use criterion:: * ;
44use matrix_sdk_crypto:: { EncryptionSettings , OlmMachine } ;
5+ use matrix_sdk_sled:: SledCryptoStore ;
56use matrix_sdk_sqlite:: SqliteCryptoStore ;
67use matrix_sdk_test:: response_from_file;
78use ruma:: {
@@ -65,11 +66,15 @@ pub fn keys_query(c: &mut Criterion) {
6566
6667 let name = format ! ( "{count} device and cross signing keys" ) ;
6768
69+ // Benchmark memory store.
70+
6871 group. bench_with_input ( BenchmarkId :: new ( "memory store" , & name) , & response, |b, response| {
6972 b. to_async ( & runtime)
7073 . iter ( || async { machine. mark_request_as_sent ( & txn_id, response) . await . unwrap ( ) } )
7174 } ) ;
7275
76+ // Benchmark sqlite store.
77+
7378 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
7479 let store = Arc :: new ( runtime. block_on ( SqliteCryptoStore :: open ( dir. path ( ) , None ) ) . unwrap ( ) ) ;
7580 let machine =
@@ -80,6 +85,23 @@ pub fn keys_query(c: &mut Criterion) {
8085 . iter ( || async { machine. mark_request_as_sent ( & txn_id, response) . await . unwrap ( ) } )
8186 } ) ;
8287
88+ {
89+ let _guard = runtime. enter ( ) ;
90+ drop ( machine) ;
91+ }
92+
93+ // Benchmark (deprecated) sled store.
94+
95+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
96+ let store = Arc :: new ( runtime. block_on ( SledCryptoStore :: open ( dir. path ( ) , None ) ) . unwrap ( ) ) ;
97+ let machine =
98+ runtime. block_on ( OlmMachine :: with_store ( alice_id ( ) , alice_device_id ( ) , store) ) . unwrap ( ) ;
99+
100+ group. bench_with_input ( BenchmarkId :: new ( "sled store" , & name) , & response, |b, response| {
101+ b. to_async ( & runtime)
102+ . iter ( || async { machine. mark_request_as_sent ( & txn_id, response) . await . unwrap ( ) } )
103+ } ) ;
104+
83105 group. finish ( )
84106}
85107
@@ -108,7 +130,10 @@ pub fn keys_claiming(c: &mut Criterion) {
108130 ( machine, & runtime, & txn_id)
109131 } ,
110132 move |( machine, runtime, txn_id) | {
111- runtime. block_on ( machine. mark_request_as_sent ( txn_id, response) ) . unwrap ( )
133+ runtime. block_on ( async {
134+ machine. mark_request_as_sent ( txn_id, response) . await . unwrap ( ) ;
135+ drop ( machine) ;
136+ } )
112137 } ,
113138 BatchSize :: SmallInput ,
114139 )
@@ -129,6 +154,31 @@ pub fn keys_claiming(c: &mut Criterion) {
129154 . unwrap ( ) ;
130155 ( machine, & runtime, & txn_id)
131156 } ,
157+ move |( machine, runtime, txn_id) | {
158+ runtime. block_on ( async {
159+ machine. mark_request_as_sent ( txn_id, response) . await . unwrap ( ) ;
160+ drop ( machine)
161+ } )
162+ } ,
163+ BatchSize :: SmallInput ,
164+ )
165+ } ) ;
166+
167+ group. bench_with_input ( BenchmarkId :: new ( "sled store" , & name) , & response, |b, response| {
168+ b. iter_batched (
169+ || {
170+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
171+ let store =
172+ Arc :: new ( runtime. block_on ( SledCryptoStore :: open ( dir. path ( ) , None ) ) . unwrap ( ) ) ;
173+
174+ let machine = runtime
175+ . block_on ( OlmMachine :: with_store ( alice_id ( ) , alice_device_id ( ) , store) )
176+ . unwrap ( ) ;
177+ runtime
178+ . block_on ( machine. mark_request_as_sent ( & txn_id, & keys_query_response) )
179+ . unwrap ( ) ;
180+ ( machine, & runtime, & txn_id)
181+ } ,
132182 move |( machine, runtime, txn_id) | {
133183 runtime. block_on ( machine. mark_request_as_sent ( txn_id, response) ) . unwrap ( )
134184 } ,
@@ -160,6 +210,8 @@ pub fn room_key_sharing(c: &mut Criterion) {
160210 group. throughput ( Throughput :: Elements ( count as u64 ) ) ;
161211 let name = format ! ( "{count} devices" ) ;
162212
213+ // Benchmark memory store.
214+
163215 group. bench_function ( BenchmarkId :: new ( "memory store" , & name) , |b| {
164216 b. to_async ( & runtime) . iter ( || async {
165217 let requests = machine
@@ -180,6 +232,9 @@ pub fn room_key_sharing(c: &mut Criterion) {
180232 machine. invalidate_group_session ( room_id) . await . unwrap ( ) ;
181233 } )
182234 } ) ;
235+
236+ // Benchmark sqlite store.
237+
183238 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
184239 let store = Arc :: new ( runtime. block_on ( SqliteCryptoStore :: open ( dir. path ( ) , None ) ) . unwrap ( ) ) ;
185240
@@ -209,6 +264,42 @@ pub fn room_key_sharing(c: &mut Criterion) {
209264 } )
210265 } ) ;
211266
267+ {
268+ let _guard = runtime. enter ( ) ;
269+ drop ( machine) ;
270+ }
271+
272+ // Benchmark (deprecated) sled store.
273+
274+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
275+ let store = Arc :: new ( runtime. block_on ( SledCryptoStore :: open ( dir. path ( ) , None ) ) . unwrap ( ) ) ;
276+
277+ let machine =
278+ runtime. block_on ( OlmMachine :: with_store ( alice_id ( ) , alice_device_id ( ) , store) ) . unwrap ( ) ;
279+ runtime. block_on ( machine. mark_request_as_sent ( & txn_id, & keys_query_response) ) . unwrap ( ) ;
280+ runtime. block_on ( machine. mark_request_as_sent ( & txn_id, & response) ) . unwrap ( ) ;
281+
282+ group. bench_function ( BenchmarkId :: new ( "sled store" , & name) , |b| {
283+ b. to_async ( & runtime) . iter ( || async {
284+ let requests = machine
285+ . share_room_key (
286+ room_id,
287+ users. iter ( ) . map ( Deref :: deref) ,
288+ EncryptionSettings :: default ( ) ,
289+ )
290+ . await
291+ . unwrap ( ) ;
292+
293+ assert ! ( !requests. is_empty( ) ) ;
294+
295+ for request in requests {
296+ machine. mark_request_as_sent ( & request. txn_id , & to_device_response) . await . unwrap ( ) ;
297+ }
298+
299+ machine. invalidate_group_session ( room_id) . await . unwrap ( ) ;
300+ } )
301+ } ) ;
302+
212303 group. finish ( )
213304}
214305
@@ -229,12 +320,16 @@ pub fn devices_missing_sessions_collecting(c: &mut Criterion) {
229320
230321 runtime. block_on ( machine. mark_request_as_sent ( & txn_id, & response) ) . unwrap ( ) ;
231322
323+ // Benchmark memory store.
324+
232325 group. bench_function ( BenchmarkId :: new ( "memory store" , & name) , |b| {
233326 b. to_async ( & runtime) . iter_with_large_drop ( || async {
234327 machine. get_missing_sessions ( users. iter ( ) . map ( Deref :: deref) ) . await . unwrap ( )
235328 } )
236329 } ) ;
237330
331+ // Benchmark sqlite store.
332+
238333 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
239334 let store = Arc :: new ( runtime. block_on ( SqliteCryptoStore :: open ( dir. path ( ) , None ) ) . unwrap ( ) ) ;
240335
@@ -249,6 +344,27 @@ pub fn devices_missing_sessions_collecting(c: &mut Criterion) {
249344 } )
250345 } ) ;
251346
347+ {
348+ let _guard = runtime. enter ( ) ;
349+ drop ( machine) ;
350+ }
351+
352+ // Benchmark (deprecated) sled store.
353+
354+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
355+ let store = Arc :: new ( runtime. block_on ( SledCryptoStore :: open ( dir. path ( ) , None ) ) . unwrap ( ) ) ;
356+
357+ let machine =
358+ runtime. block_on ( OlmMachine :: with_store ( alice_id ( ) , alice_device_id ( ) , store) ) . unwrap ( ) ;
359+
360+ runtime. block_on ( machine. mark_request_as_sent ( & txn_id, & response) ) . unwrap ( ) ;
361+
362+ group. bench_function ( BenchmarkId :: new ( "sled store" , & name) , |b| {
363+ b. to_async ( & runtime) . iter ( || async {
364+ machine. get_missing_sessions ( users. iter ( ) . map ( Deref :: deref) ) . await . unwrap ( )
365+ } )
366+ } ) ;
367+
252368 group. finish ( )
253369}
254370
0 commit comments