一. throttle 参数
问题:当我们执行adb shell monkey 1000这个压力测试的时候,这个动作是非常快的,瞬间就完成了。如果我们需要在每一个执行动作中间加一个延时的话,可以使用throttle参数。
参数解释:指定事件之间的间隔。
命令:
adb shell monkey --throttle <millisecond>#毫秒(延时时间)
示例:如果我们要执行计算器10个事件,每个事件中间间隔1s,命令如下:
adb shell monkey -p com.android.calculator2 --throttle 1000 10
动态演示不好截图,大家自己试一下哈。
二. seed参数
问题:我们在做monkey做测试的时候都知道,monkey做的操作都是随机的,那如果我们发现了一个bug,当我们提交给开发后,开发要求我们复现这个bug,那么问题来了,monkey执行的是随机事件,你要我复现,我怎么能让monkey重新按照上次的顺序执行一边呢?
参数解释:执行随机生成数的seed值。
也就是说,monkey所谓的随机操作,是基于seed产生的。每一个seed值都会产生唯一的一个操作序列。只要两次操作的seed值是一样的,那么这两次的操作序列也是一样的,这样我们就能用monkey来复现随机发生的问题了。
命令:
adb shell monkey -s<seed> <event-count>
示例:仍然以计算器压测10次为例,seed值我们设置为100。第一次执行
C:\Windows\System32>adb shell monkey -p com.android.calculator2 -s 100 10
Events injected: 10
## Network stats: elapsed time=461ms (0ms mobile, 0ms wifi, 461ms not connected)
第二次执行(先把计算器历史记录清空并且推出)
C:\Windows\System32>adb shell monkey -p com.android.calculator2 -s 100 10
Events injected: 10
## Network stats: elapsed time=2996ms (0ms mobile, 0ms wifi, 2996ms not connected)
大家注意模拟器两次输出的数字,会发现两次执行动作是一致的,这样我们就能复现随机事件出现的bug了。
三,触摸事件
问题:如果我们只要求monkey触发点击事件10次,或者只触发滑屏操作,怎么办呢?
参数解释:设定触摸事件半分比。
命令:
adb shell monkey --pct-touch <percent>
示例:让monkey帮我们完成100%的10个点击事件。
adb shell monkey -p com.android.calculator2 --pct-touch 100 10
发现看不出来有什么。这里我们加个参数 -v,列出所有事件的详情。
adb shell monkey -v -p com.android.calculator2 --pct-touch 10 100
执行结果:列出了所有的touch事件。
Event percentages就是完成了100%的点击事件。其余事件占比是0%。
C:\Windows\System32>adb shell monkey -v -p com.android.calculator2 --pct-touch 100 10
:Monkey: seed=1511772164945 count=10
:AllowPackage: com.android.calculator2
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:
// 0: 100.0%
// 1: 0.0%
// 2: 0.0%
// 3: 0.0%
// 4: -0.0%
// 5: 0.0%
// 6: 0.0%
// 7: 0.0%
// 8: 0.0%
// 9: 0.0%
// 10: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;co
onent=com.android.calculator2/.Calculator;end
// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.androi
calculator2/.Calculator } in package com.android.calculator2
:Sending Touch (ACTION_DOWN): 0:(177.0,18.0)
:Sending Touch (ACTION_UP): 0:(184.94064,1.9921265)
:Sending Touch (ACTION_DOWN): 0:(343.0,678.0)
:Sending Touch (ACTION_UP): 0:(359.80368,667.6056)
:Sending Touch (ACTION_DOWN): 0:(34.0,664.0)
:Sending Touch (ACTION_UP): 0:(53.356007,662.314)
:Sending Touch (ACTION_DOWN): 0:(175.0,302.0)
:Sending Touch (ACTION_UP): 0:(179.52809,295.46835)
:Sending Touch (ACTION_DOWN): 0:(308.0,308.0)
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=507ms (0ms mobile, 0ms wifi, 507ms not connected)
// Monkey finished
四,其他事件
1.动作事件
参数解释:设置动作事件的百分比
命令:
adb shell monkey --pct-motion<percent>
示例:如果我们执行100次操作,其中指定touch事件50%,motion事件30%,则命令如下:
adb shell monkey --pct-touch 50 --pct-motion 30 100
那剩下的20%会随机进行操作。如果我们不想monkey执行其他事件,只执行touch和motion事件,我们touch和motion的百分比加起来要等于100%才可以。
即
adb shell monkey --pct-touch 50 --pct-motion 50 100
这里我们看到只执行了touch和motion事件。
2.轨迹球事件
参数解释 :设定轨迹球事件百分比。
命令:
adb shell monkey --pct-trackball <percent>
3.基本导航事件
参数解释:设定基本导航事件的百分比,输入设备的上、下、左、右。
命令:
adb shell monkey --pct-nav <percent>
4.主要导航事件
参数解释:设定主要导航事件百分比,兼容中间键,返回键,菜单按键。
命令:
adb shell monkey --pct-majornav <percent>
5.系统导航事件
参数解释:设定系统导航事件百分比,HOME、BACK及拨号音量键。
命令:
adb shell monkey --pct-syskeys <percent>
6.启动Activity事件
设定启动Activity的事件百分比。
命令:
adb shell monkey --pct-appswitch <percent>
7.不常用事件
设定不常用事件百分比。
命令:
adb shell monkey --pct-anyevent <percnet>
8.崩溃事件(CRASH)
当我们执行monkey操作的时候,如果遇到系统应用崩溃怎么办呢?monkey这时候就会停止,后面的操作不会继续执行。所以我们需要一个参数来让monkey忽略在压力测试遇到的崩溃事件,继续执行后面的操作。
命令:
adb shell monkey --ignore-crashes <event-count>
9.超时事件(ANR)
与崩溃事件一样,遇到超时时,忽略,继续进行操作,直到完成指定的操作数。
命令:
adb shell monkey --ignore-timeouts <event-count>
示例:我要执行计算器100次压测,忽略超时和崩溃事件。
adb shell monkey -p com.android.calculator2 --ignore-crashes --ignore-timeouts 100