1.什么是Monkey测试?
Android的Monkey是一个命令行工具,可以在模拟器或实际的设备上运行。它发送用户事件(诸如键输入,触摸屏输入,手势输入等)的伪随机流,实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法。
2. Monkey特征
1)仅测试应用程序包,有一定的局限性;
2)使用随机事件的数据流,并且可以不被定制;
3)可以设置测试对象、事件号、类型、频率
3. Monkey基本用法
基本语法如下:
$ Adb shell monkey [options]
如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:
$ Adb shell monkey -p your.package.name -v 500
4.Monkey测试举例
1)在连接设备的情况下,在命令行输入adb shell进入shell 界面
2)查看 data / data 文件夹应用程序包
注:我们能测试的应用程序包都在这个目录下面
adb shell
# cd data/data
# ls
3) 以com.android.calculator2 作为monkey测试对象即计算器模块压力测试
测试脚本:monkey -p com.android.calculator2 -s 5000 -v 5000>mnt/sdcard/monkey.txt
或monkey -p com.android.calculator2 -s 5000 -v 5000> /sdcard/monkey.txt
-p 后跟包名
-v 打印日志级别,每增加一个-v 意味增加一个日志级别
5000即产生5000个事件
s 种子值生成的随机事件,相同的种子值,以产生相同的事件序列
5000 即产生5000个相同事件序列
mnt/sdcard/monkey.txt实时抓取日志存储平板内存名为monkey.txt
/sdcard/monkey.txt实时抓取日志存储平板SD卡名为monkey.txt
以com.android.camera2 作为monkey测试对象即相机模块压力测试
测试脚本:monkey -p com.android.camera2 -s 5000 -v 5000
以com.android.gallery3d 作为monkey测试对象即图库模块压力测试
测试脚本: monkey -p com.android.gallery3d -s 5000 –v 5000
以com.android.launcher作为monkey测试对象即启动项压力测试
测试脚本:monkey -p com.android.launcher -s 5000 -v 5000
以com.android.music、 com.android.musicfx作为monkey测试对象即音乐模块压力测试
测试脚本:monkey -p com.android.music -p com.android.musicfx -s 5000 -v 5000
4)Monkey测试停止条件
1、如果限定了Monkey运行在一个或几个特定的包上,那么它会监测试图转到其它包的操作,并对其进行阻止。
2、如果应用程序崩溃或接收到任何失控异常,Monkey将停止并报错。
注:下方截取部分crash日志,与拨号压力测试相关
/ CRASH: com.android.dialer (pid 24566)#进程号
…………………..
………………………
** Monkey aborted due to error.# monkey停止归因异常
Events injected: 4024
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=13 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=15718ms (0ms mobile, 0ms wifi, 15718ms not connec
ted)
** System appears to have crashed at event 4024 of 5000 using seed 5000#执行至4024事件出现crash
184|root@anzhen4_mrd8:/ #
3、如果应用程序产生了应用程序不响应(application not responding)的错误,Monkey将会停止并报错。
5)Monkey 日志分析
如下情况产生log文件
1、程序异常退出uncausedexception(简称Fatal)
2、程序强制关闭ForceClosed(简称 Fatal)
如:下方截取部分Fatal日志,与music压力测试相关
12-22 10:44:04.777 3080 3080 E AndroidRuntime: FATAL EXCEPTION: main
12-22 10:44:04.777 3080 3080 E AndroidRuntime: Process: com.android.music, PID: 3080
12-22 10:44:04.777 3080 3080 E AndroidRuntime: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
3、程序无响应ApplicationNo Response (简称ANR)
ANR出现的情况有以下两种
A界面操作按钮的点击等待响应时间超过5秒
B HandleMessage回调函数执行超过10秒,BroadcasterReciver里的onRecive()方法处理超过10秒
如:下方截取部分ANR日志,与拨号压力测试相关
12-05 15:01:01.159 581 597 E ActivityManager: ANR in com.android.dialer (com.android.dialer/.DialtactsActivity)
12-05 15:01:01.159 581 597 E ActivityManager: PID: 1815
12-05 15:01:01.159 581 597 E ActivityManager: Reason: Input dispatching timed out (Waiting because the focused window has not finished processing the input events that were previously delivered to it.)
………………….
12-05 15:01:03.609 581 612 I InputDispatcher: Application is not responding: AppWindowToken{268d6b60 token=Token{267c1ab8 ActivityRecord{267e99b0 u0 com.android.dialer/.DialtactsActivity t6}}} - Window{268e6850 u0 com.android.dialer/com.android.dialer.DialtactsActivity}. It has been 5006.1ms since event, 5003.9ms since wait started. Reason: Waiting because the focused window has not finished processing the input events that were previously delivered to it.
12-05 15:01:03.609 581 612 I WindowManager: Input event dispatching timed out sending to com.android.dialer/com.android.dialer.DialtactsActivity. Reason: Waiting because the focused window has not finished processing the input events that were previously delivered to it.
12-05 15:01:03.609 581 907 W InputManager: Input event injection from pid 2027 timed out.
12-05 15:01:03.609 581 597 I ActivityManager: Skipping duplicate ANR: ProcessRecord{268f9478 1815:com.android.dialer/u0a5} Input dispatching timed out (Waiting because the focused window has not finished processing the inpu
网友评论