Android Application Performance Test

Source: Internet
Author: User

For Web pages, page access and loading speeds are very important for user experience. If every Activity in Android is regarded as a page, it is difficult to accurately measure the startup speed of an Activity. Therefore, it is better to test the startup speed of each Activity or obtain other basic indicators for routine monitoring.

I. Compile the LaunchPerformanceBase class inherited from the Instrumentation class

/*** Base class for all launch performance Instrumentation classes.*/public class LaunchPerformanceBase extends Instrumentation {public static final String TAG = "LaunchPerformanceBase";protected Bundle mResults;protected Intent mIntent;/*** Constructor.*/public LaunchPerformanceBase() {mResults = new Bundle();mIntent = new Intent(Intent.ACTION_MAIN);mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);setAutomaticPerformanceSnapshots();}/*** Launches intent {@link #mIntent}, and waits for idle before* returning.*/protected void LaunchApp() {startActivitySync(mIntent);waitForIdleSync();}@Overridepublic void finish(int resultCode, Bundle results) {super.finish(resultCode, results);}}

In the constructor of this class, setAutomaticPerformanceSnapshots () is set to enable the performance snapshot function for Instrumentation.

The LaunchApp () method is used to start the corresponding Activity. The waitForIdleSync () method is used to wait for the Activity to be idle, that is, wait for the Activity to start.

Ii. Compile the ActivityLaunchPerformanceTest class

public class ActivityLaunchPerformanceTest extends LaunchPerformanceBase { /**    * Outfile argument name.    * This argument can be passed to the instrumentation using    -e. */    private static final String launchActivityName = "launch_activity";    /**     * Output file name.     */    private String classNameForIntent;  private String DEFAULT_ACTIVITY = "SplashActivity";/*** Constructor.*/public ActivityLaunchPerformanceTest() {    super();}@Overridepublic void onCreate(Bundle arguments) {if ( arguments != null ) {classNameForIntent = arguments.getString(launchActivityName);        }    if ( classNameForIntent == null ) {    classNameForIntent = DEFAULT_ACTIVITY ;        }super.onCreate(arguments);mIntent.setClassName("com.company.example","com.company.example.ui.activity." + classNameForIntent);start();}/*** Calls LaunchApp and finish.*/@Overridepublic void onStart() {super.onStart();LaunchApp();finish(Activity.RESULT_OK, mResults);}}
In this class, the onCreate () method is used to input the name of the Activity to be tested.

When the test starts, Instrumentation starts and the onStart method is executed, run the LaunchApp () method to start the tested Activity. finish after the test is completed.

Instrumentation exits and the test is complete.

3. Modify the AndroidManifest. xml file

Add the Instrumentation statement to the Manifest file.

 

Iv. Test Cases for running Activity Startup Performance

adb shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest
-E launch_activity parameter and specify the name of the Activity to be tested.

The test results are as follows:

INSTRUMENTATION_RESULT: other_pss=7437INSTRUMENTATION_RESULT: java_allocated=4839INSTRUMENTATION_RESULT: global_freed_size=2583696INSTRUMENTATION_RESULT: native_private_dirty=1684INSTRUMENTATION_RESULT: native_free=81INSTRUMENTATION_RESULT: global_alloc_count=51608INSTRUMENTATION_RESULT: other_private_dirty=5468INSTRUMENTATION_RESULT: global_freed_count=18818INSTRUMENTATION_RESULT: sent_transactions=-1INSTRUMENTATION_RESULT: java_free=2784INSTRUMENTATION_RESULT: received_transactions=-1INSTRUMENTATION_RESULT: pre_sent_transactions=-1INSTRUMENTATION_RESULT: other_shared_dirty=7300INSTRUMENTATION_RESULT: pre_received_transactions=-1INSTRUMENTATION_RESULT: execution_time=749INSTRUMENTATION_RESULT: native_size=4772INSTRUMENTATION_RESULT: native_shared_dirty=620INSTRUMENTATION_RESULT: cpu_time=678INSTRUMENTATION_RESULT: java_private_dirty=320INSTRUMENTATION_RESULT: native_allocated=4690INSTRUMENTATION_RESULT: gc_invocation_count=5INSTRUMENTATION_RESULT: java_shared_dirty=1972INSTRUMENTATION_RESULT: global_alloc_size=3883815INSTRUMENTATION_RESULT: java_pss=2618INSTRUMENTATION_RESULT: java_size=7623INSTRUMENTATION_RESULT: native_pss=1699INSTRUMENTATION_CODE: -1
The results with great significance are as follows: cpu_time and execution_time respectively indicate the cpu time used by the process during Activity startup and the actual execution time, in milliseconds.

If you are interested in other parameters, refer to the Instrumentation source code.

5. process the test results in text

1. Perform formatting

 adb  shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest | sed 's/=/:/' | sed 's/ //' | sed 's/\r//'
Replace = with: Remove spaces and other formatting for the test results

2. Write the gawk script named txt_to_xml.gawk.

#!/bin/bashBEGIN{print "
 "print "
 
  "FS=":"}$2 ~ /execution_time|cpu_time/{print "
  "}END{print "
 "}
Here, we only extract the values of the required cpu_time and execution_time fields, and generate an xml file in unit test format.

The command for executing the test case is as follows:

adb shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest | sed 's/=/:/' | sed 's/ //' | sed 's/\r//' | gawk -f txt_to_xml.gawk > TEST-HomeActivity.xml
The xml result is as follows:

 
 
  
  
 

Vi. Jenkins Result Display
Test cases can be executed using the command line. Therefore, Jenkins can be used for automated testing. For the generated xml report in unit test format, the Jenkins Performance Plugin plug-in can be used for graphical display:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.