-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bench: Add the room_list
bench
#3661
Open
Hywan
wants to merge
1
commit into
matrix-org:main
Choose a base branch
from
Hywan:bench-room-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+186
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
use criterion::{criterion_group, criterion_main, 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}; | ||
use serde::Deserialize; | ||
use serde_json::{json, Map, Value}; | ||
use tokio::runtime::Builder; | ||
use wiremock::{http::Method, Match, Mock, Request, ResponseTemplate}; | ||
|
||
struct SlidingSyncMatcher; | ||
|
||
impl Match for SlidingSyncMatcher { | ||
fn matches(&self, request: &Request) -> bool { | ||
request.url.path() == "/_matrix/client/unstable/org.matrix.msc3575/sync" | ||
&& request.method == Method::POST | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub(crate) struct PartialSlidingSyncRequest { | ||
pub txn_id: Option<String>, | ||
} | ||
|
||
fn criterion() -> Criterion { | ||
#[cfg(target_os = "linux")] | ||
let criterion = Criterion::default().with_profiler(pprof::criterion::PProfProfiler::new( | ||
100, | ||
pprof::criterion::Output::Flamegraph(None), | ||
)); | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
let criterion = Criterion::default(); | ||
|
||
criterion | ||
} | ||
|
||
pub fn room_list(c: &mut Criterion) { | ||
let runtime = Builder::new_multi_thread() | ||
.enable_time() | ||
.enable_io() | ||
.build() | ||
.expect("Can't create runtime"); | ||
|
||
let mut benchmark = c.benchmark_group("room_list"); | ||
|
||
for number_of_rooms in [10, 100, 1000, 10_000] { | ||
benchmark.throughput(Throughput::Elements(number_of_rooms)); | ||
benchmark.bench_with_input( | ||
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) | ||
.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" | ||
}, | ||
], | ||
// 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, | ||
} | ||
], | ||
}); | ||
|
||
room_nth += 1; | ||
|
||
(room_id, room) | ||
}) | ||
.collect::<Map<_, _>>(), | ||
); | ||
|
||
// Mock the response from the server. | ||
let _mock_guard = Mock::given(SlidingSyncMatcher) | ||
.respond_with(move |request: &Request| { | ||
let partial_request: PartialSlidingSyncRequest = | ||
request.body_json().unwrap(); | ||
|
||
ResponseTemplate::new(200).set_body_json(json!({ | ||
"txn_id": partial_request.txn_id, | ||
"pos": room_nth.to_string(), | ||
"rooms": rooms_as_json, | ||
})) | ||
}) | ||
.mount_as_scoped(&server) | ||
.await; | ||
|
||
// Sync the room list service. | ||
assert!( | ||
room_list_stream.next().await.is_some(), | ||
"`room_list_stream` has stopped" | ||
); | ||
|
||
// Sync the room list entries. | ||
assert!( | ||
room_list_entries.next().await.is_some(), | ||
"`room_list_entries` has stopped" | ||
); | ||
} | ||
} | ||
}) | ||
}, | ||
); | ||
} | ||
|
||
benchmark.finish(); | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = criterion(); | ||
targets = room_list | ||
} | ||
criterion_main!(benches); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot of line in this benchmark that should be considered to be part of the setup. I don't think we want to time
wiremock
starting up.Since there are so many lines that are timed that shouldn't be, I think it's quite hard to get the benchmark to settle down. I.e. after some 10 runs I finally got it to settle down, but still it's quite the noisy benchmark:
Please take a look at the
Bencher
documentation and more specifically at theiter_batched()
method:Having that many lines as part of the benchmark also makes it quite hard to see what exactly we're measuring. So what are we intending to measure? The sync response processing, the room list creation?