Android APM
How Can I...
Overview
Site24x7 Mobile APM for Android lets you track the performance of your native mobile applications on actual end-user devices. For example, a news reader application may perform the following operations internally:
- Launch an activity to show a list of articles
- Load the list of articles using a REST API call
- Cache the list of articles in an SQLite database
- Download a thumbnail for each article
- Cache the thumbnails to the filesystem
- Build a complex UI such as a custom list view layout
All of the above are potentially long-running operations that impact user experience so it's important to benchmark and optimize them across various devices. Site24x7 Mobile APM gathers and aggregates metrics from all your users across the globe by embedding an APM agent in your applications in the form of a library.
The APM agent measures the execution time of your code using transactions and components. In the previous example, the entire sequence of operations from launching the activity to rendering the final UI can be considered a transaction. The individual operations can be grouped into different component types such as HTTP, SQLite, filesystem, UI etc. Simple operations can be measured using just transactions, while complex operations can be measured using transactions with components.
Add Mobile APM
- Login to Site24x7 and go to Android OS section under APM tab.
- Click on Add Application button.
- Enter the application name and Apdex threshold.
- On successful addition, you will be provided with an application key and a link to download the library files (Mobile_APM_Android.zip). The same application key must be used for all versions of your application.
Usage
Start by logging in to the Site24x7 website and creating a new Mobile APM application. You will be provided with an application key and a link to download the library files. The same application key must be used for all versions of your application.
Import apm-<version>.jar into your Android project as a referenced library and mark it for export. Edit the javadoc location for the library and specify apm-<version>-javadoc.jar as the archive location.
The APM agent requires the following permissions to be declared in AndroidManifest.xml:
< uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
< uses-permission android:name = "android.permission.INTERNET" />
Initialization
Initialize the APM agent by invoking Apm.startMonitoring() in the onCreate() method of your Application subclass with the application key you obtained earlier. The APM agent uploads data to Site24x7 every 60 seconds by default. You can customize the upload interval if desired. If no data has been recorded, the APM agent will not make unnecessary network connections regardless of the interval.
import com.site24x7.android.apm.Apm;
public class MyApplication extends Application {
@Override
public void onCreate() {
super .onCreate();
Apm. startMonitoring (getApplicationContext(), "0123456789abcdef0123456789abcdef" );
}
}
Using Transactions
Start a transaction by invoking Apm.startTransaction()and providing it with a name. You should typically start a transaction before a long-running operation and stop it when the operation is complete. Transactions are thread-safe and can be started and stopped from different threads. A transaction object can only be started and stopped once. Transactions with the same name are averaged across the application. Thus, when the same operation is executed multiple times using the same transaction name, the average execution time is recorded.
private void listArticles() {
Transaction transaction = Apm. startTransaction ( "List Articles" );
// Perform long-running operation
Apm. stopTransaction ( transaction );
}
Using Components
You can group operations within a transaction into components by invoking Transaction.startComponent()and specifying a type. You can use one of the predefined types such as Component.TYPE_HTTP, Component.TYPE_SQLITE, Component.TYPE_UI etc., or specify your own type. Components are thread-safe and can be started and stopped from different threads. A component object can only be started and stopped once. A component object cannot be stopped after its parent transaction has already been stopped. Multiple components in a transaction can overlap and run in parallel. Components with the same name within a transaction are averaged.
private void listArticles() {
Transaction transaction = Apm. startTransaction ( "List Articles" );
Component httpComponent = transaction .startComponent( Component.TYPE_HTTP );
Component articlesComponent = transaction .startComponent( "Download Articles" );
// Download articles
transaction .stopComponent( articlesComponent );
for (Article article in articles ) {
Component thumbnailComponent = transaction .startComponent( "Download Thumbnail" );
// Download thumbnail
transaction .stopComponent( thumbnailComponent );
}
transaction .stopComponent( httpComponent );
Apm. stopTransaction (transaction);
}
In the above example, the total time taken by HTTP operations (downloading articles and thumbnails) are measured by Component.TYPE_HTTP and the time taken for just the articles is measured by "Download Articles" . The time taken for each thumbnail is averaged and recorded by "Download Thumbnail" since it executes multiple times within a loop.
Flushing Data
Sometimes it's desirable to manually flush recently recorded data to Site24x7's servers using the Apm.flush()method. You may want to do this if you record transactions just before your application or activity is destroyed. If you've set a large upload interval (the default is 60 seconds), you should manually flush data whenever appropriate in case the application is terminated before the next upload interval.