This document discusses techniques for measuring and improving mobile app performance to achieve 60 frames per second (FPS). It explains that the UI thread is responsible for Vsync, input, animation, and drawing each frame. Measuring FPS accurately requires using the Choreographer class to detect dropped or delayed frames. The document also presents approaches like AutoBlame and AutoProfiler used by Facebook to identify sources of jank or stutter. It recommends using Systrace with custom markers on a Nexus 4 running KitKat to quickly profile problem areas and improve performance.
7. Choreographer
▪ FrameCallback
▪ Delivered during the next
frame’s animation callback
▪ Measure the difference
between two inputs
VSync
Input
Animation
Draw
17ms 17ms17ms
9. public
interface
FrameCallback
{
/**
*
@param
frameTimeNanos
The
time
in
nanoseconds
when
the
frame
started
being
rendered
*/
void
doFrame(long
frameTimeNanos);
}
10. mFPSMeasuringCallback
=
new
Choreographer.FrameCallback()
{
@Override
public
void
doFrame(long
frameTimeNanos)
{
long
frameTimeMs
=
nsToMs(frameTimeNanos);
if
(mLastFrameTimeMs
==
FIRST_FRAME)
{
mLastFrameTimeMs
=
frameTimeMs;
mChoreographer.postFrameCallback(mFPSMeasuringCallback);
return;
}
long
milliSecondDelay
=
frameTimeMs
-‐
mLastFrameTimeMs;
mLastFrameTimeMs
=
frameTimeMs;
onFrameRendered(milliSecondDelay);
mChoreographer.postFrameCallback(mFPSMeasuringCallback);
}
};
First frame!
Report time between frames
Always post the callback
11. An FPS measuring API
public
void
enable()
{
mLastFrameTimeMs
=
FIRST_FRAME;
mChoreographer.postFrameCallback(mFPSMeasuringCallback);
}
public
void
disable()
{
mChoreographer.removeFrameCallback(mFPSMeasuringCallback);
report();
}
18. AutoProfiler*
Method Time
com/android/widget/RecyclerView.onScrolled 16%
> com/facebook/logging/FeedLogger.onScroll 10%
> com/facebook/feed/NewsFeedFragment.maybeFetch 3.3%
>> com/facebook/feed/NewsFeedRecyclerView.notifyDataSetChange 3%
>> com/facebook/feed/FetchController.shouldFetch .3%
> com/facebook/prefetch/ImagePrefetcher.onScroll 2.7%
*totally made up shit
19. AutoProfiler
for
i
in
range(20):
time.sleep(1)
device.drag((start_x,
start_y),
(stop_x,
stop_y),
0.1,
15)
Traceview sample based profiling
MonkeyRunner Script
Left as an exercise to the developer Post-process Traceview file
20. Systrace
▪ Add your own markers
▪ Sample base profiling
VSync
Input
Animation
Draw
Expensive Work
Marker
21. How can I quickly improve performance?
▪ Ideal Profiling Device
▪ Nexus 4
▪ KitKat
▪ Systrace
▪ Custom markers for problem areas
▪ Sample based profiling
▪ Find an interaction or event which consistently causes lag or jitter
and profile it