Monkey Stability Test

1. What is a stability test?

By randomly clicking the screen for a period of time, see if the app will crash and whether it can maintain normal operation

2. What is Money?

Monkey test is a means of automated testing on the Android platform. The Monkey program simulates the user's operations such as touching the screen, sliding the Trackball, and pressing buttons to test the program on the device to detect how long the program will be abnormal. It is almost completely different from MonkeyRunner. MonkeyRunner is equivalent to automated testing.

Official introduction: 
https://developer.android.com/studio/test/monkey.html

3. Introduction of Monkey

  • The Monkey program comes with the Android system and is written in Java language. The storage path in the Android file system is: /system/framework/monkey.jar
  • The Monkey.jar program is started and executed by a shell script named "monkey". The storage path of the shell script in the Android file system is: /system/bin/monkey
  • Monkey command startup method 
    a. You can execute the Monkey test through the CMD window of the PC: adb shell monkey {+command parameters} 
    b. Enter the Android system with adb shell on the PC, and perform the Monkey test by executing monkey {+command parameters} 
    c. Execute the monkey command directly on the Android machine or emulator, you can install the Android terminal emulator on the Android machine

write picture description here

4. Monkey Architecture

write picture description here

5. Monkey weakness

Although Monkey can send key messages according to a specified command script, it does not support conditional judgment and does not support reading the information of the interface to be tested to perform verification operations

6. Monkey's stress test parameters

write picture description here

Then this option is usually divided into 4 categories: common options, event options, constraint options, debugging options

1. The parameters of common options are usually:

-v is used for feedback information level (one -v means one level, two -v means two levels, usually up to three -v)

Example Level0:

adb shell monkey -p com.swartz.cicada –v 100

The default value is explained, and only a small amount of information such as startup prompt, test completion and final result is provided. The corresponding source code is shown in the figure, which is very helpful for debugging.

Example Level1:

adb shell monkey -p com.swartz.cicada -v-v 100

illustrate 

Provides a more detailed log, including information about each event sent to the Activity

Example Level2:

adb shell monkey -p com.swartz.cicada -v-v-v 100

illustrate 

The most detailed log, including the selected/unselected Activity information in the test

2. The parameters of the time option usually include:

-s represents the seed value generated by the pseudo-random number. If the seed value is the same, the sequence of events generated by the two monkeys is also the same.

Example:

Monkey测试1:adb shell monkey -p com.swartz.cicada –s 10 100

   Monkey 测试2:adb shell monkey -p com.swartz.cicada –s 10 100

   The effect of the two tests is the same, because the simulated user operation sequence (a series of operations composed of each operation in a certain sequence, that is, a sequence) is the same. Although the operation sequence is randomly generated, as long as we specify the same Seed value, the random operation sequence generated by the two tests can be guaranteed to be exactly the same, so this operation sequence is pseudo-random;

--throttle: Interval time after each event ends

3. The parameters of the constraint option are usually:

-p specifies the filename

count specifies the number of events to send

-c Use this parameter to specify one or more categories. The activity must specify a category before it can be started, otherwise it will not start.

4. The parameters of the debug options are usually:

--dbg-no-events: Initialize the started activity, but do not generate any events.
--hprof: After specifying this item, the analysis report will be generated immediately before and after the event sequence is sent
--ignore-crashes: ignore crashes
--ignore-timeouts:忽略超时
--ignore-security-exceptions:忽略安全异常
--kill-process-after-error:发生错误后直接杀掉进程
--monitor-native-crashes:跟踪本地方法的崩溃问题
工作中为了保证测试数量的完整进行,我们一般不会在发生错误时立刻退出压力测试。monkey 测试命令如下:
adb shell monkey -p com.swartz.cicada -s 500 --ignore-crashes --ignore-timeouts --monitor-native-crashes -v -v 10000 > E:\monkey_log\20170831.txt

kill 此参数用来杀掉进程:kill pid  先执行adb shell  再ps |grep monkey 查询结果如下图中15248 即为PID  再kill pid(15248)即可

例子:

adb shell monkey -p com.tpnet.hlquery --throttle 100 --pct-touch 50 --pct-motion 50 -v -v -v 1000 >c:\monkeytest.txt
  • 1

测试com.tpnet.hlquery这个app,间隔100毫秒,50%的触摸事件,50% 
的滑动事件,三个-v输出详细的日志,执行1000个事件,日志输出到c:\monkeytest.txt

ps: 命令运行前手机先解锁,win8 以上要用管理员身份打开cmd

七. Monkey的结果于分析

1. 初步分析方法:

Monkey: seed=20 count=100  --随机种子和运行次数

:AllowPackage: com.swartz.cicada  --指定的测试包
:IncludeCategory: android.intent.category.LAUNCHER  --Category包含LAUNCHER
:IncludeCategory: android.intent.category.MONKEY   --Category包含MONKEY
// Selecting main activities from category android.intent.category.LAUNCHER
//   + Using main activity com.swartz.cicada.LauncherActivity (from package com.swartz.cicada)
// Selecting main activities from category android.intent.category.MONKEY  --查询结果列表
// Seeded: 20 --随机种子
// Event percentages:
//   0: 15.0%  --事件0 为touch事件,就是相当于按下之后弹起来的一个动作
//   1: 10.0%  --事件1 为motion,相当于说从起始点到终点有移动了多少步,就是步骤数量 
//   2: 2.0%   --事件2 为pinchzoom,为两个手指有同时按下去后,都向中间移动后up起来,相当于一个缩放的动作。
//   3: 15.0%  --事件3 为trackball,为轨迹球事件 
//   4: -0.0%  --事件4 为rotation 为屏幕旋转百分比隐藏事件
//   5: 25.0%  --事件5 为nav导航事件,就是上下左右
//   6: 15.0%  --事件6 为majornav主导航事件,会产生一些窗口的事件
//   7: 2.0%   --事件7 为系统按键
//   8: 2.0%   --事件8,app应用的打开就是用的这个事件
//   9: 1.0%   --事件9,键盘的开,关

//   10: 13.0% --事件10,按键按下在弹起等动作

日志结尾:

如果Monkey测试顺利执行完成,在log的最后,会打印出当前执行事件的次数和所花费的时间;Monkey
finished代表执行完成。

(如果Monkey执行中断,在log的最后也能查看到当前已执行的次数。)


2.详细分析:

Monkey测试出现错误之后,一般的分析步骤分为以下几种: 

2.1看Monkey日志(注意第一个switch以及异常信息等) 

2.11程序无响应问题,在日志中搜索 “ANR”

2.12奔溃问题: 在日志中搜索 “Exception”

ANR问题:在日志中搜索“ANR”(什么是ANR:Application Not Responding,即应用无响应,具体有关ANR的知识详见:):https://www.tuicool.com/articles/IfQvY3

2.13ForceClosed和程序异常退出问题:在日志中搜索“致命” 

3.查看Monkey里面错误前的一些事件动作,并手动执行该动作。

如果Monkey测试顺利执行完成,在log的最后,会打印出当前执行事件的次数和所花费的时间; // Monkey finished代表执行完成。 

Monkey执行中断,在log的最后也能查看到当前的已经执行的次数。


八. Monkey测试中内存问题分析

1. 怎么样去确定内存泄漏?

1. 内存泄漏弹出out of memory对话框
2. 对于有内存泄漏但是没有单出out of memory对话框的情况,可以通过logcat文件GC出信息,(GC:java的垃圾回收机制)
    GC_FOR_ALLOC: 因为在分配内存时候内存不够引起的
    GC_EXPLICIT 表明GC被显式请求触发的,如System.gc调用,
    GC_CONCCURRENT: 表明GC在内存使用率达到一定的警戒值时候,自动触发
    GC_BEFORE_OOM 表明在虚拟机抛出内存不够异常oom之前,执行最后一次回收内存垃圾
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

九. –hprof 参数报告

adb shell monkey -p com.tpnet.hlquery --hprof --throttle 100 --pct-touch 50 --pct-motion 50 -v -v -v 1000 >c:\monkeytest.txt
  • 1

如果指定了这个选项,monkey会在发送事件的前后生成app内存快照文件,一般会在设备的/dada/misc目录下生成hprof文件(ps: /data/misc/需要root权限)

拖进AndroidStudio即可查看

(ps: 我测试了没有生成。。不知道什么回事)

十. Monkey Script脚本制作

Monkey script是按照一定语法规则编写有序的用户事件流并且适用于monkey命令工具的脚本,可以用txt写。

脚本编写完毕放入手机的sdcard目录下,cmd中运行命令

adb shell monkey -f/sdcard/脚本名字 运行次数
  • 1

结合monkey的-v和–hhprof输出日志和性能报告

adb shell monkey --hprof -f/sdcard/*.txt -v -v -v 3 > c:\monkey.txt
  • 1
  • 2

一些脚本命令:

  • start data>> 
    将控制 monkey 的一些参数设置和具体的 monkey 命令分隔开来

  • LaunchAcitivity(pkg_name, cl_name) 
    启动任意应用的一个活动(界面) 
    pkg_name, 要启动的应用包名 
    cl_name, 要打开的活动的类名

  • DispatchPointer(downTime,eventTime,action,x,y,pressure,size,metaState,xPrecision,yPrecision,device,edgeFlags) 
    sends a single gesture message to a specified location 
    downTime: The time of sending the message, as long as it is a legal long integer number EventTime 
    : Mainly used to specify the pause 
    action between sending two events: whether the message is pressed or lifted, 0 means pressed, 1 means lifted 
    x:x coordinate 
    y:y coordinate 
    The other 7 parameters can be set is 0

stencil

count = 1  // 下面这个 count 选项, monkey 并没有用到,可以忽略它  
speed = 1.0    //speed  选项是用来调整两次按键的发送濒率的 
start data >>   // “start data >>” 是 有 大小写敏感的,而且单词间的间隔只用有一个空格!
LaunchActivity(com.zhengping.contact, com.zhengping.contact.Main)   //# Activity 值可以通过 aapt 命令查看
DispatchPointer(5109520,5109520,0,261,128,0,0,0,0,0,0,0);
DispatchPointer(5109521,5109521,1,261,128,0,0,0,0,0,0,0);
UserWait(2000)  // 停顿时间

DispatchPress (KEYCODE_Z) // 文字输入 monkey 只识别英文和数字输入

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324504634&siteId=291194637