Android Monkey scripting and checking for memory leaks

Source: Internet
Author: User

One, monkey script to write 1.Monkey script format
    • Scripting Benefits:

Simple and fast, no need to touch any tools, only need a Notepad file

    • Scripting Disadvantages:

Corresponding steps for basic operations such as coordinates, keystrokes, and sequential scripts are not logical


    • Script Source:
      \development\cmds\monkey\src\com\android\commands\monkey\monkeysourcescrip.java
#头文件, Control monkey the parameters for sending messages type=raw Eventscount=tenspeed =1.0> >dispatchpress (keycode_home) dispatchpress (keycode_menu)
2.Monkey Scripting API

1) Common API

Api

Description

Launchactivity (Pkg_name,cl_name) Activate your app's activity
Tap (x,y,tapduration) Simulate a finger click event
Dispatchpress (Keycodename) Keys

Rotatescreen (Ratationdegree,peresist)
Rotate the screen
Ratationdegree: Four parameters 0123 represent 0,90,180,270
Peresist: Two parameters ^0,0 fixed after rotation and not fixed after rotation
Dispatchflip (True/false) Turn the soft keyboard on or off
Longpress () Long press for two seconds
Pressandhold (x,t,pressduration) Simulate long press events
Dispatchstring (Input) Input string
Drag (Sstart,ystart,xend,yend.stepcount) Used to simulate a drag-and-drop operation
Pinchzoom (Pt1xstart,pt1ystart,pt1xend,pt1yend,pt2xstart,pt2ystart,pt2xend,pt2yend,stepcount) Analog Zoom gestures
Userwait (Sleeptime) Let the script break for some time
Devicewakeup () Wake-up screen

2) Other APIs

Api

Description

Powerlog (Power_log_type,test_case_status) Simulating battery charge information
Writelog () Write battery information to SD card
Runcmd () Run the shell command
Dispatchpointer (Downtime,eventtime,action,x,yxpressure,size,metastate,xprecision,yprecision,device,edgefilags) Send a single gesture to a specified location
Action:0 is pressed, 1 is bouncing up
Dispatchkey (Downtime,eventtime,action,code,repeat,metastatue,device,scancode) Send a key message
Launchinstrumentation (Test_name,runner_name) Run a instrumentation test case
Dispatchtrackball Simulate sending Trackball events
Profilewait Wait 5 seconds
Startcaptureframerate () Get frame rate
Endcaptureappframerate (Input) End Get frame rate
Startcaptureappframerate (APP) Gets the specified app frame rate
Endcaptureappframerate (App,input) End Get frame rate

3.Monkey Script Instance
    • Open Browser, enter www.baidu.com, test 10 times
#头文件, controlling the parameters of the monkey sending message type=Raw Eventscount=Ten Speed=1.0#一下为脚本正文start Data>>#1. Open Browser launchactivity (com.android.browser,com.android.browser.browseractivity) profilewait () #2. Clear the URL tap (223,146) profilewait () dispatchpress ( the) profilewait () #3. Enter URL dispatchstring (www.baidu.com) profilewait () #4confirm, load URL dispatchpress (keycode_enter) profilewait () #5. Complete Exit browser dispatchpress (Keycode_home) profilewait () #6After the script completes, push the. txt file to the phone, and then use the command"adb shell monkey-f path/name.txt--throttle 500-v-V 1"Command debug run, where path refers to the file storage path, name refers to the filename
Second, monkey server

1) Open the phone port using the command "ADB shell Monkey–port 1080"
2) connect the PC and mobile port using the command "ADB forward tcp:1080 tcp:1080"
3) Create a communication using the command "telnet localhost 1080"

    • #注意事项:
 the adb forward TCP:  tcp: +/////Windows does not have Telnet turned on by default, Control Panel--programs and features--turn on or off the Windows feature--telnet related all tick ok then restart the computer
1.monkey Server Commands
    • Command Source:


\development\cmds\monkey\src\com\android\commands\monkey\monkeysourcenetwork.java


1) Common API

Command

Command format

Description

Flip Flip Open, Filp closed Turn the keyboard on or off
Touch touch [Down|up|move] [x] [y] Simulates the manipulation of finger-pressed boundaries
Trackball trackball [DX] [dy] Send a Trackball action event
Key Key [Down|up] [keycode] Send a key event
Sleep Sleep [Pause Time] Server paused a little time
Type Type [String] Send a string to the current app
Wake Wake Wake-up device
Tap Tap [x] [y] Send a command to click coordinates
Press Press [KeyCode] Send a button
Deferreturn Deferrenturn [Event] [timepue] [command] Executes a command to wait for an event incident within a specified delay

2) Query API

Command

Command format

Description

Listvar Listvar List the system variables you can view
GetVar GetVar [variable name] Gets the value of a system variable
ListViews ListViews List the IDs of all views in the application under test
Getrootview Getrootview Gets the ID of the topmost control for the app under test
Getviewswithtext Getviewswithtext [Text] Returns all control IDs that contain the specified text
QueryView QueryView viewed [id] [command]
QueryView accessibilityids [ID1] [ID2] [command]
Querying a control based on the specified ID

2.monkey Server Instance
// Click Coordinates 500,500  -  -                            //  Press keycode_menu        //10086                              / /  List viewable system variables                                      listvar// get build.product value GetVar build.product
Iii. monkey Checking for memory leaks 1. Analyzing memory leaks tools and commands

1) hprof file: Hprof can monitor CPU usage, heap allocation statistics
2) Mat tool: (http:www.eclipse.org/mat/)
3) Generate hprof File command: "ADB shell am dumpheap < process name > < save path >"
4) View app Memory command: "adb shell Dumpsys meminfo < process name >"
5) Convert to Mat readable file directive: "Hprof-conv < original hprof file path > < converted hprof file path >"

2. Basic Operation steps

1) Run Monkey or fixed automation use cases
2) Monitor the memory value and save the Hprof file if there is a large increment exception
3) Convert the saved hprof file into a mat readable file
4) Use the mat to open files and compare and analyze multiple files

3. Example
    • Android Spin screen creates a memory leak

1) Enter the URL "http:www.eclipse.org/mat/" in the links below the page click Download, and then in the new page select the appropriate system to download, download and then unzip it.
2) Prepare a script for the. bat suffix name to view the memory in real time, with the following script code:

 set  process=com.android.browser #需要测试的 Process name @adb Shell Dumpsys meminfo %process% | findstr  pss   " %process% | findstr  total   "  @ping -n 5  127.1  >nul@goto m  

3) Use the Monkey command in the cmd window "adb shell monkey-p com.android.browser–pct-rotation 100–throttle 2000-v 20"
4) Open the previously prepared. bat file to monitor the application memory situation
5) After monkey run, use the command "adb shell am dumpheap com.android.browser/data/local/tmp/test.hprof" to save the results
6) Save the. hprof file to the local PC "adb pull/data/local/tmp/test.hprof F:\test"
7) under F:\test path shift+ right mouse button to open cmd window input command "Hprof-conv test.hprof test1.hprof"
8) Use the downloaded Mat.exe to open the converted Test1.hprof file for viewing analysis (using the command "SELECT * from instanceof android.app.Activity to view objects in the database, the more objects the more memory leaks the more serious)

Android Monkey scripting and checking for memory leaks

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.