A, Android Monkey introduction:
1.Monkey is Google's a stress test for stability and command-line tools
2.Monkey path:
Monkey Android system comes with the program
Path: /system/farmwork/Monkey.jar startup script path: / system / bin / Monkey
Monkey is initiated by adb command, so long as the environment can be configured adb:
1. Download the SDK Android
2. environment variable configuration is very simple, you can look at Baidu
3. Command Format:
1) Direct PC start
adb shell monkey [option] For example: "adb shell monkey 100" indicates the device 100 by chaos
2) shell end start
adb shell // enter the mobile phone terminal command monkey [option] // use the monkey command
3) Save Monkey running log
// saved on your PC: adb shell Monkey [the Option]> d: \ monkey.txt // stored in your phone: Monkey [the Option]> /mnt/sdcard/monkey.txt // standard error stream flow and kept separate: monkey [option] 1> /mnt/sdcard/monkey.txt 2 > /mnt/sdcart/error.txt
Two, Monkey basic parameters and parameter event
1. Basic parameters:
method |
Explanation |
-help |
Print Help |
-v |
Monkey to the command line printout of log information level |
- E.g:
"monkey -help" "monkey -v" "monkey -v -v""monkey -v -v -v"
2. Constraints:
Options |
Explanation |
-P <package name list of allowed executed> | If this parameter specifies one or more packets, Monkey will only allow the system to start Activity bag. If your application requires access to additional package of Activity (eg choose a contact), while those packages also need to specify here. If you do not specify any package, Monkey will allow the system to start Activity entire package. To specify multiple packages need to use multiple -p option, each -p option can only specify a package |
-c <intent of the general category> | If this parameter specifies one or more categories, Monkey will only allow the system to start Activity of these categories a category listed. If you do not specify any category, Monkey will select categories listed in the following Activity: Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories need to use multiple -c option, each -c option can only be used for one category |
- note:
1)使用-p启动必须在设备桌面上有图标才可以,-c就针对桌面没图标的应用。
2)Activity可以在应用的xml文件中<Activity>标签中查看
3)能用-p启动的应用,也就是在桌面上可以显示出图标的应用在xml文件的<Activity>标签中一定有这个:<category android:name="android.intent.category.LAUNCHER"/>
4)如果要使用-c参数,也就是要在xml文件的<Activity>标签中加入<category android:name="PackageName.MONKEY"/>相当于是我们自定义的。也是没有桌面图标的。自定义的时候最好是包名.Monkey,方便调用
- E.g:
Query package name command of the entire system: "adb shell pm list packages" can be added behind "> d: \ pag.txt" to output a list of all disk d // test clock 100, and outputs information "adb shell monkey -p -v 100 com.android.deskclock " // test clock and each email 100, and outputs the information " the adb the shell com.android.deskclock Monkey -p -v -p com.android.deskemail 100 " // -c parameter using . PackageName.MONKEY Note 4 to change the "adb shell monkey -c PackageName.MONKEY 300"
3. Event:
Options |
Explanation |
-s |
seed value of the pseudo-random number generator. If you run Monkey again with the same seed value, it will generate the same sequence of events |
-throttle <milliseconds> |
Insert fixed delay between events. Through this option may slow the speed of execution of the Monkey. If this option is not specified, Monkey will not be delayed, the event will be produced as soon as possible |
–randomize-throttle | Insert a random delay between events, it needs -throttle with the use of random delay range (0-throttle setting time) |
–pct-touch | Adjust the percentage of the touch event (the touch event is a down-up event, it happens on the screen one single location) |
–pct-motion | Adjust the percentage action event (action events generated by a down event somewhere on the screen, a series of events and a pseudo-random up of events) |
–pct-trackball | Adjust the percentage of track events (track events by one or more random movement composition, sometimes accompanied by clicks) |
–pct-nav | Adjusting the percentage of the "basic" navigation event (navigation event from an input device by the direction up / down / left / right composition) |
–pct-majornav | Adjust the "main" percent navigation event (typically initiates the operation of these navigation event graphical interface, such as: 5-way button keyboard intermediate rewind button, menu button, etc.) |
–pct-syskeys | The percentage adjustment "system" key event (these buttons are typically reserved for use by the system, such as Home, Back, volume control keys, etc.) |
–pct-appswitch | Start of activity adjustment percentage. At random intervals in, will perform a startActivity Monkey () call, as a method of maximally package covering all Activity |
-pct-anyevent | The percentage adjustment other type of event. Comprising: a key, other device button or the like is not used |
–pct-filp | Adjust the keyboard to swap out the hidden percentage |
–pct-pinchzoom | Adjust the percentage of two-finger zoom gesture of Compression |
- note:
1)-s 因为monkey是随机对actyivity进行操作,因此,当出现错误的时候如果需要复现或者回归,我们只需-s之前错误的事件序列就可以进行之前出错的按键操作
- E.g:
//使用-s操作时钟应用上activity100次。如果不用-s的话,每次操作的seed值就会不同,那样就没法固定复现或者回归 "adb shell monkey -p com.android.deskclock -s 100 -v -v 100" //以每次间隔200毫秒的延时操作时钟100次 "adb shell monkey -p com.android.deskclock --throttle 200 -v 100" //以每次间隔0-200毫秒的随机延时操作时钟100次 "adb shell monkey -p com.android.deskclock --throttle 200 --randomize-throttle -v 100" //以每次间隔0-200毫秒的随机延时操作时钟100次,每次操作都执行触摸操作(参数后面的100就是百分比数) "adb shell monkey -p com.android.deskclock --pct-touch 100 --throttle 200 --randomize-throttle -v 100" //对时钟操作100次,每次都执行轨动作事件(参数后面的100就是百分比数) "adb shell monkey -p com.android.deskclock --pct-motion 100 -v 100" //以此类推轨迹事件、基本导航事件、主要导航事件、系统按键事件、主要导航事件、等,需要注意的是百分比事件可以连用。按照百分比分配即可,当然所有事件的总和不能超过100
四、Monkey黑白名单
- 黑名单:不测试的应用
- 白名单:要测试的应用
如果要测试的应用太多,使用太多-p太麻烦,这时候就可以用刀黑白名单了,需要注意的是,黑名单和白名单只能设置一个
选项 |
说明 |
–pkg-blacklist-file | apk黑名单,屏蔽掉黑名单中的apk不操作 |
PACKAGE_BLACKLIST_FILE | apk黑名单,屏蔽掉黑名单中的apk不操作 |
–pkg-whitelist-file | apk白名单,只操作包含在白名单红的apk |
PACKAGE_WHITELIST_FILE | apk白名单,只操作包含在白名单红的apk |
- 例如:
//假设设置黑名单:"com.android.contacts","com.android.launcher"首先先将这两个包名存放在一个txt文件中(一行一个包名)。比如说是:d:\blacklist.txt。为了方便记忆,使用参数名作为文件名是一个不错的选择 //先将黑名单push到设备中,需要记住下面push到的路径。 "adb push d:\break.txt /data/local/tmp" //除了黑名单外的整机测试,延时200毫秒操作300次 "adb shell monkey --keg-blacklist-file /data/local/tmp/blacklist.txt --throttle 200 300" //白名单也是同理
五、Monkey结果解读
1.Monkey结果分类
- Mlokey运行log输出,按照以下顺序输出:
1)测试命令信息 随机种子、运行次数、可运行的应用列表、各事件百分比
2)伪随机事件流 11大事件流
3)异常信息 ANR、CRASH等异常
4)测试结果信息 完成事件、旋转情况、案件情况、网络状态
2.Monkey结果详细解读
1)有一个隐藏属性,旋转。命令为:
adb shell monkey –pct-rotation 100 –throttle 2000 10//两秒间隔旋转10次
六、Monkey异常结果
1.Monkey异常结果
1)ANR输出异常格式:
//ANR无响应,哪一个进程没响应,进程号(Pid)是多少 System.err.println("//NOT RESPONDING:"+"(pid"+pdi+")");
- 例:ANR输出log
//NOT RESPONDING:com.android.quicksearchbox(pid 6333) //哪一个进程ANR ANR in com.android.quicksearchbox(com.android.quicksearchbox/.SearchActivity) //CPU使用情况 CPU usage from 8381ms to 2276ms ago: //内存信息 procrank:→通过"adb shell procrank"命令输出 //trace信息 anr traces:→保存于/data/anr/traces.txt //meminfo信息 meminfo:→通过"adb shell dumpsys meminfo"命令输出 //Bugereport信息 Bugreport:→通过"adb bugreport"命令输出,可用--bugreport参数控制
2)Creash输出异常格式:
System.err.println("//CREAAH:"+processName+"(pid"+pdi+")");//creash的进程名+进程号 System.err.println("//Short Msg:"+ShortMsg);//错误类型,短消息 System.err.println("//Long Msg:"+LongMsg);//错误类型,长消息 System.err.println("//Build Lable:"+Build.FINGERPRINT);//系统属性 System.err.println("//Build Changelist:"+Build.VERSION.INCREMENTAL);//系统属性 System.err.println("//Build Time:"+Build.TIME);//系统属性 System.err.pringln("//"+stackTrace.replace("\n","\n//"));//输出
- 例:creash输出log:
//Crash:com.android.quicksearchbox(pid 1699) //Short Msg:java.lang.NullPointerException //Long Msg:java.lang.NUllPointException:Attempt to invoke virtual method'com.android.quicksearbox.SourceResult com.android.quicksearchbox.Suggestions.getResult()' on a null object reference //可以通过"adb shell"进入手机后使用"getprop | grep 86"查看该系统属性 //Build Lable:generic/vbox86p:5.0/LRX21M/buildbot12160004:userdebug/test-keys //可以通过"adb shell"进入手机后使用"getprop | grep bot"查看该系统属性 //Build Changelist:eng.buildbot.20141216.000103 //可以通过"adb shell"进入手机后使用"getprop | grep time"查看该系统属性 //Build Time:1318684697000 //java.lang.RuntimeException:Unable to stop activity{com.android.quicksearchbox/com.android.quicksearchbox.SearchActivity}:java.lang.NullPointerException:Attempt to invoke virtual method 'com.android.quicksearchbox.SourResult com.android.quicksearchbox.Suggestions.getResult()' on a null object reference //at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3344) //creash的一些信息 //at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3344) //at android.app.ActivityThread.handleStopActivity(ActivityThread:java:3390) //at android.app.ActivityThread.access$1100(ActivityThread:java:144) //at android.app.ActivityThread$H.handleMessage(ActivityThread:java:1307) //at android.os.Handler.dispatchMessage(Handler.java:102) //at android.Looper.loop(Looper.java:135)
2.Java常见错误类型
- 例如:
异常与错误类型 说明 java.lang.NullPointerException 空指针异常 java.lang.ArraIndexOutOfBoundsException 数组溢出 java.lang.ClassNotFoundException 类不存在 java.lang.ArithmeticException 数学运算异常 java.lang.IllegalArgumentException 方法参数异常 java.os.FileNotFoundException 文件未找到 java.lang.NumberFormatException 数值转化异常 java.lang.StackOverflowError 堆栈异常错误 java.lang.OutOfMemoryError 内存溢出错误
七、Monkey测试策略介绍
- 策略例子1:整机测试,而不测试拨号盘应用,忽略所有错误,次数100万次
adb shell monkey --ignore-creashes --ignore-timeouts --pke-blacklist-file /data/local/tmp/blacklist.txt -v -v 1000000
- Strategy Example 2: Test Calculator 300,000 times, random seed 100, a random delay of 0-1 seconds, ignoring all errors
adb shell monkey -p com.android.calculator2 -s 100 --throttle 1000 --randomize-throttle --ignore-creashs --ignore-timeouts -v -v 300000
- Strategy Example 3: Test the calculator, touch events by 30%, 50% other keys, error stop, delay 200
adb shell monkey -p com.android.calculator2 --throttle 200 --pct-touch 30 --pct-anyevent 50 -v -v 100000
- Strategy Example 4: The calculator rotation stress test event 2 seconds delay, 10W times
adb shell monkey -p com.android.calculator2 -pct-rotation 100 --throttle 2000 100000
- Strategy Example 5: Only the application of open testing machine, 5 seconds delay event, 10W times
adb shell monkey --pct-appswitch 100 --throttle 5000 100000