Skip to content

Instantly share code, notes, and snippets.

@yrom
Created August 1, 2019 11:37
Show Gist options
  • Save yrom/ac4f30b26ee02ce3bd3a1d260bb9ffb4 to your computer and use it in GitHub Desktop.
Save yrom/ac4f30b26ee02ce3bd3a1d260bb9ffb4 to your computer and use it in GitHub Desktop.
calculate fps in flutter app
var orginalCallback;
void main() {
runApp(...);
orginalCallback = window.onReportTimings;
window.onReportTimings = onReportTimings;
}
const maxframes = 60;
final lastFrames = ListQueue<FrameTiming>(maxframes);
void onReportTimings(List<FrameTiming> timings) {
for (FrameTiming timing in timings) {
lastFrames.addFirst(timing);
}
while (lastFrames.length >= maxframes) {
lastFrames.removeLast();
}
if (orginalCallback != null) {
orginalCallback(timings);
}
print("fps : $fps");
}
const frameInterval = const Duration(microseconds: Duration.microsecondsPerSecond ~/ 60);
double get fps {
var lastFramesSet = <FrameTiming>[];
for (FrameTiming timing in lastFrames) {
if (lastFramesSet.isEmpty) {
lastFramesSet.add(timing);
} else {
var lastStart = lastFramesSet.last.timestampInMicroseconds(FramePhase.buildStart);
if (lastStart - timing.timestampInMicroseconds(FramePhase.rasterFinish) > ( frameInterval.inMicroseconds * 2)) {
// in different set
break;
}
lastFramesSet.add(timing);
}
}
var frameCount = lastFramesSet.length;
var costCount = lastFramesSet.map((t) {
return (t.totalSpan.inMicroseconds ~/ frameInterval.inMicroseconds) + 1;
}).fold(0, (a, b)=> a + b);
return frameCount * 60 / costCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment