Measuring app performance – Android App

An app is considered to have poor performance if it responds slowly, freezes, crashes, or consumes a lot of power. To monitor the app where is its making inefficient use of resources such as CPU, memory, network and device battery, Traceview tool helps to trace the logs. You can generate the trace logs by instrumenting your code with the Debug class. This method of tracing is very precise because you can specify exactly where in the code you want to start and stop logging trace data

Note: While profiling an app, you should disable Instant Run.

Generate Trace Logs by Instrumenting Your App

To create trace logs, call startMethodTracing() where you want the system to start logging tracing data.

In the call, you can specify the name for the .trace file, and the system saves it to a package-specific directory that’s intended for persistent app data on the target device—this is the same directory that is returned by getExternalFilesDir() and is located in the ~/sdcard/ directory on most devices. This file contains the binary method trace data and a mapping table with thread and method names. To stop tracing, callstopMethodTracing().
The following sample starts and stops recording a trace log with the name sample.trace:

// Starts recording a trace log with the name you provide. For example, the
// following code tells the system to start recording a .trace file to the
// device with the name “sample.trace”.
Debug.startMethodTracing(“sample”); …
// The system begins buffering the generated trace data, until your
// application calls stopMethodTracing(), at which time it writes
// the buffered data to the output file. Debug.stopMethodTracing();

Note that if your app calls the startMethodTracing() again without changing the name of the trace log, it overwrites the existing log saved to the device. To learn how to dynamically change the name of each trace log, go to the section about saving multiple logs. If the system reaches the maximum buffer size before you call stopMethodTracing(), the system stops tracing and sends a notification to the console. The methods that start and stop traces work across your entire app process. That is, you could call startMethodTracing() in your activity’ sonCreate(Bundle) method, and call stopMethodTracing() in that activity’s onDestroy() method.

Note that your app runs more slowly when profiling is enabled. That is, you shouldn’t use the profiling data to determine absolute timings (such as, “method foo() takes 2.5 seconds to run”). The timing information in the trace logs are useful only when comparing it to previous trace logs, so you can see if recent changes make your app faster or slower.

When deploying to devices running Android 5.0 (API level 21) and higher, you can use sample-based profiling to profile with less runtime performance impact. To enable sample profiling, call startMethodTracingSampling() (instead of calling startMethodTracing()) with a specified sampling interval. The system gathers samples periodically until your app calls stopMethodTracing().

Save multiple logs

If your app starts and stops a method trace multiple times without specifying a new name for the trace log, the device overwrites the older trace log with the new one—that is, it only keeps the most recent trace log. To save multiple trace logs to your device, dynamically rename the trace log each time your app calls startMethodTracing(). The sample below uses the SimpleDateFormat class to include the current date and time when naming each trace log:

// Uses the SimpleDateFormat class to create a String with
// the current date and time. SimpleDateFormat date = new SimpleDateFormat(“dd_MM_yyyy_hh_mm_ss”); String logDate = date.format(new Date());
// Applies the date and time to the name of the trace log. Debug.startMethodTracing( “sample-” + logDate);

Access trace logs on the device

  1. After the system creates the trace log on your device, you can access the file in one of the following ways: · Use the Device File Explorer. To open the Device File Explorer, click View > Tool Windows > Device File Explorer (or click the Device File Explorer button in the tool window bar). As shown in figure 1 you can locate the .trace files by navigating to your app’s package-specific directory.
  2. Copy the file to your local machine using the adb pull command. The command below copies a trace log named sample.trace from the device to the ~/Documents/trace-logs/ directory of your local machine. You can then view the .trace file using Traceview.

db pull path-on-device/sample.trace ~/Documents/trace-logs/

Inspect Trace Logs with Traceview

Traceview is a tool that provides a graphical representations of trace logs. You can generate the logs by instrumenting your code with the Debug class. This method of tracing is very precise because you can specify exactly where in the code you want to start and stop logging trace data.

Open a trace log using Traceview

To open a trace log with Traceview from Android Studio, proceed as follows:

  1. Select Tools > Android > Android Device Monitor from the main menu.
  2. In the Android device monitor, select File > Open File.
  3. Navigate to the .trace file you want to inspect.
  4. Click Open.

Note: If you are trying to view the trace logs of an application that is built with ProGuard enabled (release mode build), some method and member names might be obfuscated. You can use the Proguard mapping.txt file to figure out the original unobfuscated names. For more information on this file, see the Proguard documentation.
Note: Running traceview from the command line has been deprecated

References:
https://developer.android.com/studio/profile/traceview.html
https://developer.android.com/studio/profile/generate-trace-logs.html
https://developer.android.com/studio/profile/cpu-profiler.html

Measuring app performance – Android App

An app is considered to have poor performance if it responds slowly, freezes, crashes, or consumes a lot of power. To monitor the app where is its making inefficient use of resources such as CPU, memory, network and device battery, Traceview tool helps to trace the logs. You can generate the trace logs by instrumenting your code with the Debug class. This method of tracing is very precise because you can specify exactly where in the code you want to start and stop logging trace data

Note: While profiling an app, you should disable Instant Run.

Generate Trace Logs by Instrumenting Your App

To create trace logs, call startMethodTracing() where you want the system to start logging tracing data.

In the call, you can specify the name for the .trace file, and the system saves it to a package-specific directory that’s intended for persistent app data on the target device—this is the same directory that is returned by getExternalFilesDir() and is located in the ~/sdcard/ directory on most devices. This file contains the binary method trace data and a mapping table with thread and method names. To stop tracing, callstopMethodTracing().
The following sample starts and stops recording a trace log with the name sample.trace:

// Starts recording a trace log with the name you provide. For example, the
// following code tells the system to start recording a .trace file to the
// device with the name “sample.trace”.
Debug.startMethodTracing(“sample”); …
// The system begins buffering the generated trace data, until your
// application calls stopMethodTracing(), at which time it writes
// the buffered data to the output file. Debug.stopMethodTracing();

Note that if your app calls the startMethodTracing() again without changing the name of the trace log, it overwrites the existing log saved to the device. To learn how to dynamically change the name of each trace log, go to the section about saving multiple logs. If the system reaches the maximum buffer size before you call stopMethodTracing(), the system stops tracing and sends a notification to the console. The methods that start and stop traces work across your entire app process. That is, you could call startMethodTracing() in your activity’ sonCreate(Bundle) method, and call stopMethodTracing() in that activity’s onDestroy() method.

Note that your app runs more slowly when profiling is enabled. That is, you shouldn’t use the profiling data to determine absolute timings (such as, “method foo() takes 2.5 seconds to run”). The timing information in the trace logs are useful only when comparing it to previous trace logs, so you can see if recent changes make your app faster or slower.

When deploying to devices running Android 5.0 (API level 21) and higher, you can use sample-based profiling to profile with less runtime performance impact. To enable sample profiling, call startMethodTracingSampling() (instead of calling startMethodTracing()) with a specified sampling interval. The system gathers samples periodically until your app calls stopMethodTracing().

Save multiple logs

If your app starts and stops a method trace multiple times without specifying a new name for the trace log, the device overwrites the older trace log with the new one—that is, it only keeps the most recent trace log. To save multiple trace logs to your device, dynamically rename the trace log each time your app calls startMethodTracing(). The sample below uses the SimpleDateFormat class to include the current date and time when naming each trace log:

// Uses the SimpleDateFormat class to create a String with
// the current date and time. SimpleDateFormat date = new SimpleDateFormat(“dd_MM_yyyy_hh_mm_ss”); String logDate = date.format(new Date());
// Applies the date and time to the name of the trace log. Debug.startMethodTracing( “sample-” + logDate);

Access trace logs on the device

  1. After the system creates the trace log on your device, you can access the file in one of the following ways: · Use the Device File Explorer. To open the Device File Explorer, click View > Tool Windows > Device File Explorer (or click the Device File Explorer button in the tool window bar). As shown in figure 1 you can locate the .trace files by navigating to your app’s package-specific directory.
  2. Copy the file to your local machine using the adb pull command. The command below copies a trace log named sample.trace from the device to the ~/Documents/trace-logs/ directory of your local machine. You can then view the .trace file using Traceview.

db pull path-on-device/sample.trace ~/Documents/trace-logs/

Inspect Trace Logs with Traceview

Traceview is a tool that provides a graphical representations of trace logs. You can generate the logs by instrumenting your code with the Debug class. This method of tracing is very precise because you can specify exactly where in the code you want to start and stop logging trace data.

Open a trace log using Traceview

To open a trace log with Traceview from Android Studio, proceed as follows:

  1. Select Tools > Android > Android Device Monitor from the main menu.
  2. In the Android device monitor, select File > Open File.
  3. Navigate to the .trace file you want to inspect.
  4. Click Open.

Note: If you are trying to view the trace logs of an application that is built with ProGuard enabled (release mode build), some method and member names might be obfuscated. You can use the Proguard mapping.txt file to figure out the original unobfuscated names. For more information on this file, see the Proguard documentation.
Note: Running traceview from the command line has been deprecated

References:
https://developer.android.com/studio/profile/traceview.html
https://developer.android.com/studio/profile/generate-trace-logs.html
https://developer.android.com/studio/profile/cpu-profiler.html