-
-
Notifications
You must be signed in to change notification settings - Fork 276
Proof of Concept: Frame Timing Integration #519
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 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
117 changes: 117 additions & 0 deletions
117
flutter/lib/src/integrations/frame_timing_integration.dart
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,117 @@ | ||
| import 'dart:async'; | ||
| import 'dart:ui'; | ||
|
|
||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:sentry/sentry.dart'; | ||
|
|
||
| import '../sentry_flutter_options.dart'; | ||
|
|
||
| /// Records the time which a frame takes to draw, if it's above | ||
| /// a certain threshold, i.e. [FrameTimingIntegration.badFrameThreshold]. | ||
| /// | ||
| /// Should not be added in debug mode because the performance of the debug mode | ||
| /// is not indicativ of the performance in release mode. | ||
| /// | ||
| /// Remarks: | ||
| /// See [SchedulerBinding.addTimingsCallback](https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addTimingsCallback.html) | ||
| /// to learn more about the performance impact of using this. | ||
| /// | ||
| /// Adding a `timingsCallback` has a real significant performance impact as | ||
| /// noted above. Thus this integration should only be added if it's enabled. | ||
| /// The enabled check should not happen inside the `timingsCallback`. | ||
| class FrameTimingIntegration extends Integration<SentryFlutterOptions> { | ||
| FrameTimingIntegration({ | ||
| required this.reporter, | ||
| this.badFrameThreshold = const Duration(milliseconds: 16), | ||
| }); | ||
|
|
||
| final Duration badFrameThreshold; | ||
| final FrameTimingReporter reporter; | ||
|
|
||
| late Hub _hub; | ||
|
|
||
| @override | ||
| FutureOr<void> call(Hub hub, SentryFlutterOptions options) { | ||
| // We don't need to call `WidgetsFlutterBinding.ensureInitialized()` | ||
| // because `WidgetsFlutterBindingIntegration` already calls it. | ||
| // If the instance is not created, we skip it to keep going. | ||
| final instance = WidgetsBinding.instance; | ||
| if (instance != null) { | ||
| _hub = hub; | ||
| instance.addTimingsCallback(_timingsCallback); | ||
| options.sdk.addIntegration('FrameTimingsIntegration'); | ||
| } else { | ||
| options.logger( | ||
| SentryLevel.error, | ||
| 'FrameTimingsIntegration failed to be installed', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| void _timingsCallback(List<FrameTiming> timings) { | ||
| var count = 0; | ||
| var worstFrameDuration = Duration.zero; | ||
| for (final timing in timings) { | ||
| if (timing.totalSpan > badFrameThreshold) { | ||
| count = count + 1; | ||
| if (timing.totalSpan > worstFrameDuration) { | ||
| worstFrameDuration = timing.totalSpan; | ||
| } | ||
| } | ||
| } | ||
| if (count > 0) { | ||
| final totalDuration = | ||
| timings.map((e) => e.totalSpan).reduce((a, b) => a + b); | ||
|
|
||
| final message = _message(count, worstFrameDuration, totalDuration); | ||
|
|
||
| if (reporter == FrameTimingReporter.breadcrumb) { | ||
| _reportAsBreadcrumb(message); | ||
| } else { | ||
| // This callback does not allow async, so we captureMessage as | ||
| // a fire and forget action. | ||
| _reportAsEvent(message); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Future<void> _reportAsEvent(String message) { | ||
| return _hub.captureMessage( | ||
| message, | ||
| level: SentryLevel.warning, | ||
| ); | ||
| } | ||
|
|
||
| void _reportAsBreadcrumb(String message) { | ||
| _hub.addBreadcrumb(Breadcrumb( | ||
| type: 'info', | ||
| category: 'ui', | ||
| message: message, | ||
| level: SentryLevel.warning, | ||
| )); | ||
| } | ||
|
|
||
| String _message( | ||
| int badFrameCount, | ||
| Duration worstFrameDuration, | ||
| Duration totalDuration, | ||
| ) { | ||
| return '$badFrameCount frames exceeded ${_formatMS(badFrameThreshold)} ' | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Average frame time would be nice to know, too. |
||
| 'in the last ${_formatMS(totalDuration)}. ' | ||
| 'The worst frame time in this time span was ' | ||
| '${_formatMS(worstFrameDuration)}'; | ||
| } | ||
|
|
||
| @override | ||
| FutureOr<void> close() { | ||
| WidgetsBinding.instance?.removeTimingsCallback(_timingsCallback); | ||
| } | ||
|
|
||
| /// Format milliseconds with more precision than absolut milliseconds | ||
| String _formatMS(Duration duration) => '${duration.inMicroseconds * 0.001}ms'; | ||
| } | ||
|
|
||
| enum FrameTimingReporter { | ||
| breadcrumb, | ||
| event, | ||
| } | ||
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.
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.
I haven't yet found a way to determine the device current refresh rate.