-
Notifications
You must be signed in to change notification settings - Fork 4.4k
perf(mobile): avoid redundant message list sorting #7647
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
Merged
+290
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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,30 @@ | ||
| # Mobile performance diagnostics | ||
|
|
||
| With the repository's Hermit environment active, run from `mobile/`: | ||
|
|
||
| ```sh | ||
| flutter test --dart-define=BUZZ_RUN_BENCHMARKS=true test/benchmark/channel_live_burst_test.dart | ||
| ``` | ||
|
|
||
| The diagnostic is skipped by default. It does not use a relay, credentials, or | ||
| community content. | ||
|
|
||
| The buffered-channel diagnostic sends synthetic events through the real | ||
| `RelaySessionNotifier` event buffer and `ChannelMessagesNotifier` live callback. | ||
| Only connection state and history responses are replaced. It measures one | ||
| synchronous flush and a zero-delay timer queued just before that flush. The timer | ||
| shows how long other event-loop work waits behind the consumer callbacks. | ||
|
|
||
| It varies retained history (50, 500, 2000 events) and live burst size (50, 200, | ||
| 1000 events). Each case warms up twice before emitting four JSON measurement | ||
| records in microseconds. Compare medians on the same host under similar load. | ||
| No wall-clock threshold is asserted because debug/JIT compilation and host load | ||
| make it unsuitable for a stable CI performance gate. Final event counts are | ||
| asserted to ensure the measured work actually completes. | ||
|
|
||
| This is a bounded stress diagnostic, not a representative release benchmark. | ||
| Retained history is modeled as one synthetic page instead of real pagination. | ||
| It excludes widgets, media, network traffic, signature verification, and other | ||
| subscriptions. It can identify synchronous consumer costs, but cannot establish | ||
| that a measured burst occurred in a real community or predict release frame | ||
| latency. |
This file contains hidden or 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,134 @@ | ||
| // Opt-in synthetic diagnostic; see README.md for invocation and limitations. | ||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:buzz/features/channels/channel_messages_provider.dart'; | ||
| import 'package:buzz/shared/relay/relay.dart'; | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
|
|
||
| void main() { | ||
| test( | ||
| 'buffered live channel delivery', | ||
| () async { | ||
| for (final historySize in [50, 500, 2000]) { | ||
| for (final burstSize in [50, 200, 1000]) { | ||
| // Discard the first two runs to reduce debug/JIT warm-up effects. | ||
| for (var run = 0; run < 6; run++) { | ||
| await _measureBurst(historySize, burstSize, run); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| skip: !const bool.fromEnvironment('BUZZ_RUN_BENCHMARKS'), | ||
| timeout: const Timeout(Duration(minutes: 3)), | ||
| ); | ||
| } | ||
|
|
||
| Future<void> _measureBurst(int historySize, int burstSize, int run) async { | ||
| final session = _SyntheticSession(historySize); | ||
| final container = ProviderContainer( | ||
| overrides: [relaySessionProvider.overrideWith(() => session)], | ||
| ); | ||
| try { | ||
| final loaded = Completer<void>(); | ||
| container.listen(channelMessagesProvider(_channelId), (_, next) { | ||
| if (!loaded.isCompleted && next.value?.length == historySize) { | ||
| loaded.complete(); | ||
| } | ||
| }); | ||
| await loaded.future.timeout(const Duration(seconds: 5)); | ||
|
|
||
| for (var index = 0; index < burstSize; index++) { | ||
| session.debugHandleMessage([ | ||
| 'EVENT', | ||
| 'l-1', | ||
| _event('live-$index', historySize + index).toJson(), | ||
| ]); | ||
| } | ||
| final tick = Completer<int>(); | ||
| final watch = Stopwatch()..start(); | ||
| Timer.run(() => tick.complete(watch.elapsedMicroseconds)); | ||
| session.debugFlushEventBuffer(); | ||
| final synchronousUs = watch.elapsedMicroseconds; | ||
| final eventLoopDelayUs = await tick.future; | ||
|
|
||
| expect( | ||
| container.read(channelMessagesProvider(_channelId)).value!.length, | ||
| historySize + burstSize, | ||
| ); | ||
| if (run >= 2) { | ||
| debugPrint( | ||
| jsonEncode({ | ||
| 'history': historySize, | ||
| 'burst': burstSize, | ||
| 'run': run - 2, | ||
| 'synchronous_us': synchronousUs, | ||
| 'event_loop_delay_us': eventLoopDelayUs, | ||
| }), | ||
| ); | ||
| } | ||
| } finally { | ||
| container.dispose(); | ||
| // The original 16 ms buffer timer remains armed after a debug flush. | ||
| await Future<void>.delayed(const Duration(milliseconds: 20)); | ||
| } | ||
| } | ||
|
|
||
| const _channelId = '11111111-1111-4111-8111-111111111111'; | ||
|
|
||
| NostrEvent _event(String id, int createdAt) => NostrEvent( | ||
| id: id, | ||
| pubkey: 'synthetic-author', | ||
| createdAt: createdAt, | ||
| kind: EventKind.streamMessageV2, | ||
| tags: const [ | ||
| ['h', _channelId], | ||
| ], | ||
| content: 'Synthetic message', | ||
| sig: 'synthetic-signature', | ||
| ); | ||
|
|
||
| class _SyntheticSession extends RelaySessionNotifier { | ||
| final int historySize; | ||
|
|
||
| _SyntheticSession(this.historySize); | ||
|
|
||
| @override | ||
| SessionState build() => const SessionState(status: SessionStatus.connected); | ||
|
|
||
| @override | ||
| Future<void Function()> subscribe( | ||
| NostrFilter filter, | ||
| void Function(NostrEvent) onEvent, { | ||
| void Function(String message)? onClosed, | ||
| }) { | ||
| // Preserve the real subscription and buffer delivery implementation. This | ||
| // fixture creates exactly one subscription, whose generated id is l-1. | ||
| final ready = super.subscribe(filter, onEvent, onClosed: onClosed); | ||
| debugHandleMessage(['EOSE', 'l-1']); | ||
| return ready; | ||
| } | ||
|
|
||
| @override | ||
| Future<List<NostrEvent>> queryRelay( | ||
| List<NostrFilter> filters, { | ||
| Duration timeout = const Duration(seconds: 8), | ||
| }) async => [ | ||
| // Model retained history in one synthetic page; real windows page by 50. | ||
| for (var index = historySize - 1; index >= 0; index--) | ||
| _event('history-$index', index), | ||
| NostrEvent( | ||
| id: 'bounds', | ||
| pubkey: 'synthetic-relay', | ||
| createdAt: 0, | ||
| kind: EventKind.channelWindowBounds, | ||
| tags: const [ | ||
| ['d', '$_channelId:head'], | ||
| ], | ||
| content: jsonEncode({'has_more': false, 'next_cursor': null}), | ||
| sig: 'synthetic-signature', | ||
| ), | ||
| ]; | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.