Stability testing using the monkey tool

First understand what monkey is

        Monkey is a command-line tool that comes with the Android system, which can run in an emulator or in a real device. Monkey sends a pseudo-random stream of user events to the system, enabling stress testing of applications under development.

monkey includes many options, which can be roughly divided into four categories:

1. Basic configuration options like setting the number of events to try.

2. Run constraint options, such as setting only a single package to be tested.

3. Event type and frequency.

4. Debug options

Monkey's command startup method

Before starting monkey, you need to prepare the JDK and SDK environment on the machine. After installation, you can start your monkey in the following three ways

  1. Monkey test can be performed by executing in the PC CMD window: adb shell monkey {+command parameters}
  2. Enter the Android system on the adb shell on the PC, and perform the Monkey test by executing monkey {+command parameters}
  3. Execute the monkey command directly on the Android machine or emulator, and install the Android terminal emulator on the Android machine

Preparation commands before monkey

  1. windows+r enter cmd to enter the dos command line
  2. Enter adb  devices and a combination similar to the one shown below will succeed.             
  3. Enter adb shell pm list packages to view all the installation packages on the phone;
  4. Enter adb shell pm list packages -3 to view all third-party installation packages on the phone

Introduction to common parameters of monkey

1. Parameter -p: used for constraints, use this parameter to specify one or more packages. The -p space is followed by the package name; 100 indicates the number of test events.

After specifying the package, monkey will only allow the system to start the specified APP. If no package is specified, monkey will allow the system to start all APPs in the device.

No package specified: adb shell monkey 100
Specify a package: adb shell monkey -p com.tencent.news 100
Specify multiple packages: adb shell monkey -p com.tencent.news 100 -p com.tencent.news 100*

2. Parameter -v: Used to specify the level of detail of the feedback log, a total of 3 levels within the range
Log level level 0 (Note: only a small amount of information such as startup prompts, test completion and final results are provided)
adb shell monkey -p com.tencent .news -v 100
log level level 1 (Description: provide more detailed logs, including each event information sent to the Activity)
adb shell monkey -p com.tencent.news -v -v 100
log level level 2 (Description: Provide the most detailed log, including the event information of the selected Activity in the test)
adb shell monkey -p com.tencent.news -v -v -v 100

3. Parameter -s: Used to specify the send value of the pseudo-random number generator. If the send is the same, the sequence of events generated by the two monkey tests is also the same.
Although the operation sequence is randomly generated, if the same send value is specified, the random operation sequence generated by the two tests can be guaranteed to be exactly the same, so the operation sequence is pseudo-random.

4. --throttle: Set a fixed delay between two events, which can slow down the execution speed of monkey. If not specified, monkey will not be delayed and events will generate and send messages as fast as possible. Unit: millisecond

例:adb shell monkey  --throttle  3000  -p com.baidu.news  100    

Send 1000 random events to Baidu News, with an interval of 3 seconds between each event.

5. --pct-touch : (add a number after the space) Set the percentage of touch screen event generation. A touch screen event is a gesture with a finger down and up event.

例:   adb shell monkey   --throttle  3000   --pct-touch  50  -p  com.baidu.news  100 

Send 1000 random events to Baidu News, with an interval of 3 seconds between each event. Among them, the proportion of touch screen events is set to 50%.

6. --pct-motion : Set the percentage of sliding event generation. A swipe event is a gesture in which the finger is pressed down at a certain position, and then the finger is lifted after sliding for a certain distance.

例:   adb shell monkey   --throttle  3000 --pct-motion  50  -p  com.baidu.news  100 

Send 1000 random events to Baidu News, with an interval of 3 seconds between each event. Among them, the proportion of sliding events is set to 50%.

 7. --pct-trackball : Set the percentage of trackball event generation. A trackball event is an event consisting of a sequence of random move and click events

例:  adb shell monkey --throttle  3000 --pct-trackball  50  -p  com.baidu.news  100 

Send 1000 random events to Baidu News, with an interval of 3 seconds between each event. Among them, the event of setting the trackball accounted for 50%.

8. --pct-nav : Set the percentage of basic navigation event generation. Basic navigation events are events that simulate directional input up, down, left, and right on the device.

例:  adb shell monkey --throttle  3000 --pct-nav  40  -p  com.baidu.news  100 

Send 1000 random events to Baidu News, with an interval of 3 seconds between each event. Among them, the proportion of setting basic navigation events is 40%.

9. --pct-majornav : Set the percentage of major navigation events generated. Main navigation events usually cause the UI to generate feedback information, such as clicking the Back key, Home key, Menu key, etc.,

例:  adb shell monkey  --throttle  3000   --pct-majornav  40  -p  com.baidu.news  100 

Send 1000 random events to Baidu News, with an interval of 3 seconds between each event. Among them, the proportion of main navigation events is set to 40%.

10. --hprof: Specify this parameter, and Monkey will generate a performance analysis report before and after sending the event sequence. Usually a file with a size of about 5MB will be generated in the data/misc directory

11. --ignore-crashes: monkey will stop running after the test application crashes or an exception occurs. If this parameter is specified, monkey will continue to send events to the system after an exception occurs until the specified events are all run.

12. --ignore-timeouts: When any timeout error (application not responding) occurs in the application, monkey will stop running. If this parameter is specified, monkey will continue to send events to the system after generating an error message until all specified events are completed.

13. --ignore-security-exceptions : When the specified application has a license error (such as certificate license, network license, etc.), monkey will stop running. If this parameter is specified, even if a license error occurs in the application, monkey will continue to send events to the system until all specified events are completed.

例:adb shell monkey  --throttle  3000 --pct-trackball  50  -p  com.baidu.news  --ignore-security-exceptions  100 

5. Monkey report composition

The first part: contains the seed value, the number of executions, and the package name.                                
The second part: the names of all application packages on the device under test, which packages are used in this test, and which packages are not used. The third part
: the percentage of each event, only displayed The code of the event is given, but the specific time is not displayed. You can query the events corresponding to each code in the monkey source code. All events include:

Event percentages:

0: Percentage of touch events, parameter --pct-touch

1: Percentage of sliding events, parameter --pct-motion

2: Zoom event percentage, parameter --pct-pinchzoom

3: Percentage of trackball events, parameter --pct-trackball

4: Percentage of screen rotation events, parameter --pct-rotation

5: I don't know what this is

6: Basic navigation event percentage, parameter --pct-nav

7: Percentage of major navigation events, parameter --pct-majornav

8: Percentage of system events, parameter --pct-syskeys

9: Activity start event percentage, parameter --pct-appswitch

10: Percentage of keyboard flip events, parameter --pct-flip

11: Percentage of other events, parameter --pct-anyevent

Part Four: Specific Events

Part 5: log completion
If the Monkey test is successfully executed, at the end of the log, the number of current execution events and the time spent will be printed;
// Monkey finished means that the execution is completed.
If the execution of Monkey is interrupted, the current execution times can also be viewed at the end of the log.

The log completed by Monkey is as follows:

6. Monkey analysis report

1. Search for keywords in the log:
        1) Search for the keyword "ANR" in the report to see if there is an application not responding event (Application Not Responding)
        2) Search for the keyword "crash" in the report to see if there is a crash
        3) Search for the keyword "exception" in the report to see if there are other abnormal events . If there is a null pointer NullPointerException, you need to pay attention.
        4) Search for "GC" for memory leaks
2. Preliminary analysis method: After an error occurs in the monkey, the general analysis steps are
        1) Find the location where the error occurred
        2) Check the activity between the two switches before the error occurred
        3) Manually execute the event , to reproduce the problem
        4) If the above steps still cannot be found, when an error occurs, there will be a seed value, enter the same seed value, and run monkey according to the previous command again 3. Detailed analysis
method:
        1) ANR problem: search in the log "ANR" ("Application Not Responding") indicates that there is a bug and ANR occurs. Generally, the response of the main thread exceeds 5 seconds, or the BroadcastReceiver does not respond within 10 seconds. This is a more serious flaw. It is enough to set up another thread to process the time-consuming operation.
        2) Analyze the specific information in the log: check the first Switch in the log, mainly to check which Activity is executed by Monkey, for example, in the following log, com.tencent.smtt.SplashActivity is executed, between the next swtich Yes, if there is a crash or other exception, you can find the problem in the Activity.
        3) Memory leaks
                1) The out of memory dialog pops up
                for memory leaks 2) If there is a memory leak, but there is no single out of memory dialog box, you can use the logcat file to GC the information, (GC: java garbage collection mechanism)
                GC_FOR_ALLOC: GC_EXPLICIT caused by insufficient memory when allocating memory
                : indicates that the GC is triggered by an explicit request, such as System.gc call, GC_CONCCURRENT: indicates that the GC automatically triggers                 GC_BEFORE_OOM
                when the memory usage reaches a certain warning value
Before the machine throws out of memory exception oom, perform the last recovery of memory garbage
                3) Find memory leaks – memory report analysis (use the memory snapshot of the hprof parameter to generate a memory report), after the memory leak is found, you can execute the same monkey, just Add one more parameter –hprof

adb shell monkey -p 包名 --hprof --throttle 100 --pct-touch 50 --pct-motion 50 -v -v -v 1000 >c:\monkey.txt

           Note: If this option is specified, monkey will generate APP memory snapshot files before and after the sending time, generally hprof files will be generated in the /data/misc directory of the mobile device.

            (/data/misc requires root privileges, you can install a RE on the phone to view or view it through the mobile phone assistant)
File conversion:

        Check the hprof-conv command under sdk-tools when configuring the monkey test, enter hprof-conv -help on the command line to know the file conversion usage, just convert it directly, from .hprof to .conv format.
        The converted file can be viewed with the Memory Analyzer tool (MAT) of eclipse, and a report can be generated by clicking on the Reports->Leak Suspects link.

Guess you like

Origin blog.csdn.net/weixin_45730522/article/details/128472838