Skip to content

Commit

Permalink
cont
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Nov 5, 2024
1 parent 462f9c6 commit a2b1df1
Showing 1 changed file with 68 additions and 78 deletions.
146 changes: 68 additions & 78 deletions benchmarks/benches/room_list_bench.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
use futures_util::{pin_mut, StreamExt};
use matrix_sdk::test_utils::logged_in_client_with_server;
use matrix_sdk_ui::{room_list_service::filters::new_filter_non_left, RoomListService};
Expand Down Expand Up @@ -49,88 +49,54 @@ pub fn room_list(c: &mut Criterion) {
BenchmarkId::new("sync", number_of_rooms),
&number_of_rooms,
|benchmark, maximum_number_of_rooms| {
benchmark.to_async(&runtime).iter(|| async {
// Create a fresh new `Client` and a mocked server.
let (client, server) = logged_in_client_with_server().await;

// Create a new `RoomListService`.
let room_list_service = RoomListService::new(client.clone())
.await
.expect("Failed to create the `RoomListService`");

// Get the `RoomListService` sync stream.
let room_list_stream = room_list_service.sync();
pin_mut!(room_list_stream);

// Get the `RoomList` itself with a default filter.
let room_list = room_list_service
.all_rooms()
.await
.expect("Failed to fetch the `all_rooms` room list");
let (room_list_entries, room_list_entries_controller) = room_list
.entries_with_dynamic_adapters(100, client.roominfo_update_receiver());
pin_mut!(room_list_entries);
room_list_entries_controller.set_filter(Box::new(new_filter_non_left()));
room_list_entries.next().await;

// “Send” (mocked) and receive rooms.
{
const ROOMS_BATCH_SIZE: u64 = 100;
let maximum_number_of_rooms = *maximum_number_of_rooms;
let mut number_of_sent_rooms = 0;
let mut room_nth = 0;

while number_of_sent_rooms < maximum_number_of_rooms {
let number_of_rooms_to_send = u64::max(
number_of_sent_rooms + ROOMS_BATCH_SIZE,
maximum_number_of_rooms,
) - number_of_sent_rooms;

number_of_sent_rooms += number_of_rooms_to_send;

let rooms_as_json = Value::Object(
(0..number_of_rooms_to_send)
benchmark.iter_batched(
|| {
runtime.block_on(async {
// Create a fresh new `Client` and a mocked server.
let (client, server) = logged_in_client_with_server().await;

// Create a new `RoomListService`.
let room_list_service = RoomListService::new(client.clone())
.await
.expect("Failed to create the `RoomListService`");

// Compute the response.
let rooms = {
let mut room_nth = 0u32;

(0..*maximum_number_of_rooms)
.map(|_| {
let room_id = format!("!r{room_nth}:matrix.org");

let room = json!({
// The recency timestamp is different, so that it triggers a re-ordering of the
// room list every time, just to stress the APIs.
"timestamp": room_nth % 10,
// One state event to activate the associated code path.
"required_state": [
{
"content": {
"name": format!("Room #{room_nth}"),
},
"event_id": format!("$s{room_nth}"),
"origin_server_ts": 1,
"sender": "@example:matrix.org",
"state_key": "",
"type": "m.room.name"
"bump_stamp": room_nth % 10,
"required_state": [{
"content": {
"name": format!("Room #{room_nth}"),
},
"event_id": format!("$s{room_nth}"),
"origin_server_ts": 1,
"sender": "@example:matrix.org",
"state_key": "",
"type": "m.room.name",
}],
"timeline": [{
"event_id": format!("$t{room_nth}"),
"sender": "@example:matrix.org",
"type": "m.room.message",
"content": {
"body": "foo",
"msgtype": "m.text",
},
],
// One room event to active the associated code path.
"timeline": [
{
"event_id": format!("$t{room_nth}"),
"sender": "@example:matrix.org",
"type": "m.room.message",
"content": {
"body": "foo",
"msgtype": "m.text",
},
"origin_server_ts": 1,
}
],
"origin_server_ts": 1,
}],
});

room_nth += 1;

(room_id, room)
})
.collect::<Map<_, _>>(),
);
.collect::<Map<String, Value>>()
};

// Mock the response from the server.
let _mock_guard = Mock::given(SlidingSyncMatcher)
Expand All @@ -140,13 +106,36 @@ pub fn room_list(c: &mut Criterion) {

ResponseTemplate::new(200).set_body_json(json!({
"txn_id": partial_request.txn_id,
"pos": room_nth.to_string(),
"rooms": rooms_as_json,
"pos": "raclette",
"rooms": rooms,
}))
})
.mount_as_scoped(&server)
.await;

(&runtime, client, server, room_list_service)
})
},
|(runtime, _client, _server, room_list_service)| {
runtime.block_on(async {
// Get the `RoomListService` sync stream.
let room_list_stream = room_list_service.sync();
pin_mut!(room_list_stream);

// Get the `RoomList` itself with a default filter.
let room_list = room_list_service
.all_rooms()
.await
.expect("Failed to fetch the `all_rooms` room list");

let (room_list_entries, room_list_entries_controller) =
room_list.entries_with_dynamic_adapters(100);
pin_mut!(room_list_entries);

room_list_entries_controller
.set_filter(Box::new(new_filter_non_left()));
room_list_entries.next().await;

// Sync the room list service.
assert!(
room_list_stream.next().await.is_some(),
Expand All @@ -158,9 +147,10 @@ pub fn room_list(c: &mut Criterion) {
room_list_entries.next().await.is_some(),
"`room_list_entries` has stopped"
);
}
}
})
})
},
BatchSize::SmallInput,
)
},
);
}
Expand Down

0 comments on commit a2b1df1

Please sign in to comment.