Using Application States in Trepn Profiler to Pinpoint App Problems

Tuesday 1/28/14 10:58am
|
Posted By Rick Schwartz
  • Up0
  • Down0

Qualcomm products mentioned within this post are offered by
Qualcomm Technologies, Inc. and/or its subsidiaries.

We’ve been blogging about power and resource consumption lately to emphasize a few points:

  • Apps that quickly drain the battery frustrate consumers and can result in poor ratings
  • Simple changes to your code can reduce power consumption and extend battery life
  • If you can locate the performance bottlenecks in your apps, you can optimize your code to eliminate them
  • Trepn™ Profiler and Trepn plug-in for Eclipse are profiling tools designed to help you identify and fix common problems that affect app performance or lead to excessive power consumption

But how do you pinpoint these bottlenecks and the causes of excessive power consumption? How can you be sure your app is responsible for them? And if it is, how do you know what to do to fix things?

In this post, I’ll show you how to send messages from your app that Trepn Profiler will display while it’s profiling to help you answer the questions above.

Application States

To see what your app is doing and when it is doing it, you can profile it with Trepn. Once you’ve installed Trepn and profiled your app, you can modify your code to correlate application behavior with activity graphed in Trepn Profiler. This is made possible because Trepn can capture Android Intents sent from your app.

Suppose your app communicates with a Web server and a local SQLite database to get and send data. If the app executes several requests and Trepn Profiler shows a spike in power consumption, you may not be able to see which task caused the spike. Application States can help you determine whether it is your app that is consuming too much power and, if so, where to start addressing the problem in your code.

Finding Power Spikes

Let’s assume a simple browser app with functions like opening a URL and scrolling. Run Trepn Profiler on one of your test devices and configure it so battery power is a selected data point (Note: Trepn Profiler tracks and charts many different system data points on the device). Then you run your app and put it through its paces while Trepn Profiler generates a graph and compiles a database of activity. The graph looks something like this (red markup added): 

    Finding Power Spikes 

As you analyze the graph, you may be wondering about the jumps in battery usage. “What was my app doing when those spikes occurred?”

With Application States, you have a way of finding out.

Instrumenting your code

Trepn Profiler can send a simple call from your app to update the state whenever it changes. Trepn will then chart the Application State updates on the y-axis of its graph.

The syntax for the update is:

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value", <int_value>);

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value.Desc", <string_value>);

Here, you assign values to each state:

  • 2: pause scrolling/idle
  • 7: open a URL
  • 9: scrolling in progress

Then you add the code for each method as follows:

Intent stateUpdate = new Intent("com.quicinc.Trepn.UpdateAppState");

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value", 2);

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value.Desc", "scrollingPaused()");

sendBroadcast(stateUpdate);
.

.

openURL()

.

Intent stateUpdate = new Intent("com.quicinc.Trepn.UpdateAppState");

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value", 7);

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value.Desc", "openUrl()");

sendBroadcast(stateUpdate);

.

.

pageScrolling()

.

Intent stateUpdate = new Intent("com.quicinc.Trepn.UpdateAppState");

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value", 9);

stateUpdate.putExtra("com.quicinc.Trepn.UpdateAppState.Value.Desc", "pageScrolling()");

sendBroadcast(stateUpdate);

.

.

Compile your app and install it on the test device.

Charting Application States

This time, you configure data points in Trepn Profiler to include Application State and put your app through the same paces as before. When you stop profiling and analyze a graph of your data, you’ll see something like this (red markup added): 

    Application States 

Trepn Profiler has charted Time on the x-axis as before, but this time it has also charted Application State (int_value) in blue against battery power in green on the y-axis. Looking at the spikes that concerned you before and the state of your app, you can draw some conclusions:

  • Spike 1 – Your app was idle (Application State = 2) when this spike occurred, so it is likely that something else in the system caused it.
  • Spike 2 – Your app was loading a URL (Application State = 7), so this spike was expected.
  • Spike 3 – Your app had finished loading the page and was scrolling (Application State = 9), so this spike also was expected.

If you do see tasks that need optimization, the code around your Android Intents is a good place to start.

Next, analyze the run and select “Stats" to see a table like this: 

    Analyze stats 

Each task is shown here by the string_value you assigned, so you can easily find average and total battery power. Each time you optimize your code to reduce power consumption, have a look at these statistics to measure your progress.

Two important notes:

  • This example tracks and charts battery power. You can configure Trepn Profiler to track many other data points including CPU load/frequency, GPU, Wi-Fi network usage, mobile network usage, wakelocks and much more.
  • If you’re serious about optimizing your app’s effects on system resources, you’ll probably want to write an automated version of your app that executes the most important tasks almost identically on each run.
Next Steps

So, why optimize in the dark? Application States in Trepn Profiler can help you identify power consumption and performance bottleneck issues and determine where to optimize your code to address them.

  • Trepn Profiler is available at no charge, so start using it now.
  • Inside the Trepn Profiler download you’ll also find the Trepn User Guide. For more details, have a look at Chapter 3, “Using Intents for Application States and Automated Testing.”

Questions? Visit the Trepn Profiler Support Forum or let me know in the comments below.