-
-
Notifications
You must be signed in to change notification settings - Fork 257
Proof of Concept: Frame Tracking #699
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
6 commits
Select commit
Hold shift + click to select a range
80e2efe
Wip
ueman 803ab03
frame tracking cleanup
ueman 667d63f
fix types
ueman b63839d
Merge remote-tracking branch 'origin/main' into feature/frame-tracking
ueman d435299
track total, slow and frozen frames
ueman f49a663
Merge remote-tracking branch 'origin/main' into feature/frame-tracking
ueman 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
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,53 @@ | ||
class SentryMeasurement { | ||
SentryMeasurement(this.name, this.value); | ||
|
||
// Mobile / Desktop Vitals | ||
|
||
/// Amount of frames drawn during a transaction | ||
SentryMeasurement.totalFrames(this.value) : name = 'frames_total'; | ||
|
||
/// Amount of slow frames drawn during a transaction. | ||
/// A slow frame is any frame longer than 1s / refreshrate. | ||
/// So for example any frame slower than 16ms for a refresh rate of 60hz. | ||
SentryMeasurement.slowFrames(this.value) : name = 'frames_slow'; | ||
|
||
/// Amount of frozen frames drawn during a transaction. | ||
/// Typically defined as frames slower than 500ms. | ||
SentryMeasurement.frozenFrames(this.value) : name = 'frames_frozen'; | ||
|
||
SentryMeasurement.coldAppStart(Duration duration) | ||
: assert(!duration.isNegative), | ||
name = 'app_start_cold', | ||
value = duration.inMilliseconds; | ||
|
||
SentryMeasurement.warmAppStart(Duration duration) | ||
: assert(!duration.isNegative), | ||
name = 'app_start_warm', | ||
value = duration.inMilliseconds; | ||
|
||
// Web Vitals | ||
|
||
SentryMeasurement.firstContentfulPaint(Duration duration) | ||
: assert(!duration.isNegative), | ||
name = 'fcp', | ||
value = duration.inMilliseconds; | ||
|
||
SentryMeasurement.firstPaint(Duration duration) | ||
: assert(!duration.isNegative), | ||
name = 'fp', | ||
value = duration.inMilliseconds; | ||
|
||
SentryMeasurement.timeToFirstByte(Duration duration) | ||
: assert(!duration.isNegative), | ||
name = 'ttfb', | ||
value = duration.inMilliseconds; | ||
|
||
final String name; | ||
final num value; | ||
|
||
Map<String, dynamic> toJson() { | ||
return <String, num>{ | ||
'value': value, | ||
}; | ||
} | ||
} |
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
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,70 @@ | ||
import 'dart:ui'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:sentry/sentry.dart'; | ||
|
||
class FrameTracker { | ||
FrameTracker({ | ||
this.slowFrameThreshold = const Duration(milliseconds: 16), | ||
this.frozenFrameThreshold = const Duration(milliseconds: 500), | ||
required WidgetsBinding binding, | ||
}) : _binding = binding; | ||
|
||
final WidgetsBinding _binding; | ||
|
||
SingletonFlutterWindow get window => _binding.window; | ||
|
||
final Duration slowFrameThreshold; | ||
final Duration frozenFrameThreshold; | ||
|
||
DateTime? _timeStamp; | ||
|
||
var _frameCount = 0; | ||
var _slowCount = 0; | ||
var _frozenCount = 0; | ||
|
||
void _frameCallback(Duration _) { | ||
final timeStamp = _timeStamp; | ||
if (timeStamp == null) { | ||
return; | ||
} | ||
|
||
// postFrameCallbacks are called just once, | ||
// so we have to add it each frame. | ||
_binding.addPostFrameCallback(_frameCallback); | ||
|
||
final now = DateTime.now(); | ||
_timeStamp = now; | ||
final duration = timeStamp.difference(now).abs(); | ||
|
||
if (duration > frozenFrameThreshold) { | ||
_frozenCount++; | ||
} else if (duration > slowFrameThreshold) { | ||
_slowCount++; | ||
} | ||
|
||
_frameCount++; | ||
} | ||
|
||
void start() { | ||
_timeStamp = DateTime.now(); | ||
_binding.addPostFrameCallback(_frameCallback); | ||
} | ||
|
||
List<SentryMeasurement> finish() { | ||
final metrics = [ | ||
SentryMeasurement.totalFrames(_frameCount), | ||
SentryMeasurement.frozenFrames(_frozenCount), | ||
SentryMeasurement.slowFrames(_slowCount), | ||
]; | ||
|
||
_reset(); | ||
return metrics; | ||
} | ||
|
||
void _reset() { | ||
_timeStamp = null; | ||
_frameCount = 0; | ||
_slowCount = 0; | ||
_frozenCount = 0; | ||
} | ||
} |
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
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.
Maybe a ticker is a better candidate than this? See https://api.flutter.dev/flutter/scheduler/Ticker-class.html