We are on the LAST installment in our ‘Getting Started With’ series and it is on performance monitoring! Now there’s a reason why this was the last for me to cover, and probably the last that many of you would pay attention too (but please, correct me if I’m wrong).
For many of us, performance just isn’t an issue. Unless you’re rendering some real HD graphics or loading about a hundred images in a GridView within the size of a single screen, Android apps for the most part tend to run pretty smoothly. We have tools like RecyclerViews to handle list items effectively or RxJava to perform functions asynchronously and multithread like a boss.
Now if the above paragraph shares some of your heads, oh boy you’re in for a ride. Let me tell you this. Performance is important. Small discrepancies in your app’s performance could make a huge impact. The difference between a 2 second and a 4 second startup time could be the difference whether a user keeps your app or not.
Not to mention, this symbol we all love. We’ve all been there, and chances are, you’ve probably given your users a whole lot of this. How does that make you feel?
“But Eric, this is a network problem, not a performance one”
Exactly! Performance isn’t just the device’s CPU power. It’s the user’s connection as well, and there are TONS of ways you could optimise it to make your data load faster.
So what does Performance Monitoring do?
It’s like another Analytics metric. You gain insight on how your app is performing on your users’ devices, broken down by the usual country, device, and OS version.
Add it to your app
It’s real simple. Just import these dependencies and voila.
First add this in your project-level build.gradle file within buildscript -> repositories
jcenter()
Then in the buildscript -> dependencies section, add this.
- For Android Studio 2.x
classpath 'com.google.firebase:firebase-plugins:1.1.1'
- For Android Studio 3.x
classpath 'com.google.firebase:firebase-plugins:1.1.5'
Now in your app-level build.gradle file, add this below apply plugin: ‘com.android.application’
apply plugin: 'com.google.firebase.firebase-perf'
Then add this in the dependencies section
implementation 'com.google.firebase:firebase-perf:16.1.2'
Build your app, them boom! Performance Monitoring is on. You can head to the Firebase Console and see automatic traces and network requests from your users (assuming they have the new version of the app with the performance monitoring).
Under ‘automatic traces’, these traces fall into that category:
Trace Name | Description |
---|---|
App start | Starts when the app’s FirebasePerfProviderContentProvider completes its onCreate method and stops when the first activity’s onResume() method is called. If the app was not cold-started by an activity (for example, by a service or broadcast receiver), no trace is generated. |
Screen | Starts for every Activity class when the app calls onActivityStarted(), and stops when the app calls onActivityStopped(). |
App in background | Starts when the last activity to leave the foreground has itsonStop() method called and stops when the first activity to reach the foreground has its onResume() method called. |
App in foreground | Starts when the first activity to reach the foreground has its onResume() method called and stops when the last activity to leave the foreground has its onStop() method called. |
Now here’s the big HOWEVER: These aren’t the only traces you can track. With automatic tracing comes Manual Tracing.
Tracing your own events
Just before the code where you want to start tracing, add this. This starts a trace called “test_trace” (feel free to change the name though):
Trace myTrace = FirebasePerformance.getInstance().newTrace("test_trace"); myTrace.start();
During the trace, add code similar to the following to performance-related events in your code to track when they happen or fail to happen.
Item item = cache.fetch("item"); if (item != null) { myTrace.incrementMetric("item_cache_hit", 1); } else { myTrace.incrementMetric("item_cache_miss", 1); }
And when you want to stop the trace:
myTrace.stop();
The @AddTrace annotation
As if these traces weren’t easy enough, you can easily trace whole methods by simply adding the @AddTrace annotation.
@Override @AddTrace(name = "onCreateTrace", enabled = true /* optional */) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
Conclusion
As per my usual disclaimer, this information is just the official docs simplified for your better understanding. The code snippets here are taken directly from those docs so go check it out if you want to learn about Performance Monitoring in more detail.
And that about sums it up for the ‘Getting Started With’ series. I’m preparing a whole variety of changes here at EricDecanini.com that you should be excited about. I’ll soon be making a post about what these changes are going to be in more detail, but we’ll be sticking with the Firebase jig for quite a while. Stay tuned!