Thursday, October 29, 2015

Measuring Activity Startup Time

In recent talks I've given, as well as the Developing for Android series, I talk about the need to launch quickly, and about how to ensure that you're testing the right launch speed (cold start, like after reboot and (mostly) after killing a task, vs. warm start, which is way faster because the activity just has to be brought to the foreground).

Then someone asked me, quite reasonably, "So how do I get my app's launch time?"

Then I paused and wondered the same thing...

Whenever I've done this kind of benchmarking on framework code, I've had the ability to instrument exactly the bits I needed to. But how can non-framework developers get the information they need from just running the normal build?

Fortunately, this information exists, and has since API 19. So if you're running on any release later than 4.4 (Kitkat), you should be set.

All you have to do is launch your activity and look in logcat for something like this:

ActivityManager: Displayed com.android.myexample/.StartupTiming: +768ms
Update 11/18/20: This log is now issued by ActivityTaskManager, not ActivityManager]

This information is output whenever an activity window is first drawn, after it goes through all of the startup stuff. This time includes the entire time that it took to launch the process, until the application ran layout and drew for the first time. This is basically the main information you need. It doesn't include the amount of time it took between the user clicking your app icon and the system getting ready to launch your activity, which is fine, because you cannot (as an app developer) affect that time, so there's no need to measure it. Instead, it includes all of the time it took to load your code, initialize your classes that are used at start time, run layout, and draw your app that first time. All of which is really what you want to measure, because that's what you can and should try to optimize.

There's an additional option to be aware of. The 'Displayed' time is automatically reported, to give you a quick measure of how long that initial launch took. But what if you are also loading in some other content asynchronously and want to know how long it took for everything to be loaded, drawn, and ready to go? In that case, you'll want to additionally call Activity.reportFullyDrawn(), which will then report, in the log, the time between that initial apk start (the same time as that used for the Displayed time) and the time when you call the onReportFullyDrawn() method. This secondary time is a superset of the initial one (assuming you call it after the initial launch time, which is preferred), giving you the additional information about how long it took to do everything, including the follow-on work after the app was first displayed.

There is another way of measuring startup time which is worth mentioning for completeness, especially since it uses my favorite device tool, screenrecord. This technique involves recording the entire experience of tapping on your app's icon to launch it and waiting until your app window is up and ready to go.

First, start screenrecord with the --bugreport option (which adds timestamps to the frames - this was a feature added in L. I think):
$ adb shell screenrecord --bugreport /sdcard/launch.mp4

Then tap your app icon, wait until your app is displayed, ctrl-C screenrecord, and pull the file up onto your host system with adb pull:
$ adb pull /sdcard/launch.mp4

Now you can open the resulting video and see what's happening when. To do this effectively, you'll need to have a video player that allows you to step frame-by-frame (Quicktime does this, not sure what the best player with this feature is on other OSs). Now step through the frames, noticing that there's a frame timestamp at the top of the video window.

Step forward until you see the app icon highlighted - this happens after the system has processed the click event on the icon and has started to launch the app. Note the frame time when this happened. Now frame-step forward until you see the first frame that your application's full UI begins to be visible. Depending on your launch experience (whether you have a starting window, a splash screen, etc.), the exact sequence of events and windows may vary. For a simple application you'll see the starting window come up first, then a cross-fade with the real UI in your application when it's ready. You want to note the first frame where you see any of the real UI content of your app. This happens when your app has finished layout and drawn itself, and is now ready to be shown. Note the time at this frame as well.

Now subtract the two times ((UI displayed) - (icon tapped)); this is the full time that it took for your app to go all the way from the initial user tap to being drawn and ready. It is a superset of the "Displayed" log described above, since it includes time before the process launches and after that first rendering (when the system starts the cross-fade animation), but it is at least something that you can use for comparison purposes with other launches after you make things faster and want to see how much better it is.

As with any performance testing, it's good to try to run your tests in similar situations multiple times (including making sure you're testing 'cold start' as noted above), as various things can happen to vary the results on any one run.

Now that you know how to figure out your launch times, whichever approach you use, go make it faster.
Please.

Tuesday, June 9, 2015

Developing for Android

A series of articles have been posted on the Google Developers publication on medium.com that explain the constraints of mobile applications and a set of rules to keep in mind in order to develop good, well-performing Android applications.

Introduction
I: Understanding the Mobile Context
II: Memory
III: Performance
IV: Networking
V: Language & Libraries
VI: Storage
VII: Framework
VIII: The Rules: User Interface
IX: Tools

Wednesday, January 28, 2015

Android Development: Lotsa Links

This is meant to be a living archive of Android presentations, articles, videos, whatever that I've presented, co-presented, written, been a witness to, or simply enjoyed and learned from. People ask for this stuff occasionally ("Where can I learn more about performance tuning on Android?" or "Where can I see more videos of Romain? He's so dreamy, with that almost-real French accent!"), so I thought it would be worth recording the links somewhere where I can add new ones over time as stuff comes online (and delete old ones as they become obsolete).

I'll attempt to categorize things, but there is overlap on these topics. So the studious developer will, of course, watch and read everything. Twice.

The links are presented in rough reverse-chronological order in each section. Some talks date way back to 2010, but they're still relevant today (the advantage of APIs that don't go away...).

General Android Development

Android Developers Backstage (Tor Norbye, Chet Haase, and guests)
Tor and I interview other Android developers to talk about whatever it is that they do to help developers better understand how that stuff works.

Performance

Android Performance Workshop, Part 1 (Devoxx 2013) (Romain Guy & Chet Haase)
This presentation is all about memory on Android: how the system works, things to think about to avoid garbage collection, and tools to use to help detect and debug problems.

Android Performance Workshop, Part 2 (Devoxx 2013) (Romain Guy & Chet Haase)
This talk covers some platform improvements, performance tips, and case studies of chasing and fixing performance issues.

Android Graphics Performance (Google I/O 2013) (Romain Guy & Chet Haase)
More performance tips with demos of using the tools to find and fix problems.

Android Performance Case Study (Romain Guy)
This article from Romain shows how he used many of Android's performance tools to debug performance issues like overdraw on a real world app.

For Butter or Worse (Google I/O 2012) (Romain Guy & Chet Haase)
Romain and I discussed the graphics architecture of Android, along with various tips for achieving better performance.

Important Android Stuff, Part 2 (Devoxx 2012) (Romain Guy & Chet Haase)
More performance tips, more tools usage, more finding and fixing performance problems. More, more, more.

Android Tools (Devoxx 2011) (Romain Guy & Chet Haase)
A talk about some of the tools and techniques used for finding and fixing performance problems.

Android Performance Patterns (Colt McAnlis)
This series of videos from Colt helps you understand how things work and what you need to know to write better performing Android apps.

Graphics & Animation

Material Design (Devoxx 2014 keynote) (Nick Butcher & Chet Haase)
This talk is a combination of the design underpinnings of Material Design and some the platform API details for writing Material Design applications on both Android and Polymer.

Material Witness (Devoxx 2014) (Romain Guy & Chet Haase)
A talk about some of the Material Design APIs and techniques in the Android 5.0 Lollipop release, showing how they are used in a couple of sample applications.
This talk overlaps with a talk by the same name at Google I/O 2014, but this version is updated to the final APIs (the Google I/O talk was based on the APIs in the L Developer Preview release).

Material Science (Google I/O 2014) (Adam Powell & Chet Haase)
This is a talk on writing Material Design applications. Some of the API details have changed since this presentation, since it was based on the L Developer Preview release, but the underlying ideas of developing for Material Design is the same.

Important Android Stuff, Part 1 (Devoxx 2012) (Romain Guy & Chet Haase)
An overview of the Animation APIs, both pre-3.0 (the android.view.animation classes) and post-3.0 (the android.animation classes, Object Animator, etc.).

Curved Motion in Android (Chet Haase)
New APIs in Android 5.0 Lollipop make this much easier (and built into the platform), but this article explains how to use ObjectAnimator and TypeEvaluator to make your animations curve on earlier releases.

Android Graphics and Animation (Devoxx 2010) (Romain Guy & Chet Haase)
Romain and I talk about the general process of rendering Views on Android, graphics APIs for achieving various graphical effects, and the pre-3.0 Animation APIs.

Dive Into Android, Part 1 (Devoxx 2010) (Romain Guy)
Romain talks about the broader concepts of layout on Android, and the various built-in layout classes to use. He then steps through an example of creating a simple custom layout, to explain the process of measurement and layout that such a subclass must handle.

Dive Into Android, Part 2 (Devoxx 2010) (Romain Guy & Chet Haase)
Tips and techniques for creating graphical effects in Android applications.

Writing Custom Views for Android (Google I/O 2013) (Romain Guy & Adam Powell)
Romain and Adam Powell talk about custom views.

Stick GUIs (Romain Guy & Chet Haase)
Romain and I talk about various rich graphical effects for Android applications.

Android's Font Renderer (Romain Guy)
Romain's article about how Android renders text using the GPU.

DevBytes (Chet Haase & many others)
It's definitely worth checking out the DevBytes playlist. The content there is diverse, but it's clear to tell from the title whether it's something that you're interested in, and they all provide a quick deep dive into their topic of choice. There are a bunch of videos specific to animation and graphical effects, but there are many more videos on a wide range of Android topics.