使用monkey对安卓应用进行压力测试
Android提供一个命令行工具monkey可以对安卓程序进行测试,monkey会对应用生成随机的动作流,包括点击、触摸、手势、文字录入等,模拟人工操作应用,并在发生错误后发送报告给用户。使用方法:首先在avd manager中打开一个模拟器或者使用USB线连上真机,打开命令行窗口,输入下面的命令:
1 |
adb shell monkey -p com.bcoder.testapp 5000 |
其中com.bcoder.testapp是你的包的名字,5000代表要测试的次数
如果你的命令行中不能执行adb命令,请找度娘给你配置一下adb
monkey支持很多参数,下面是一个详细的参数列表:
类别 | 参数 | 描述 |
---|---|---|
常规 | --help |
打印一个简单的使用向导(确实很简单!)。 |
-v |
每增加一个-v就会增加输出的测试信息的详细度,共分为级别0、1、2,也就是最多输入两个-v |
|
事件 | -s <seed> |
此参数后边可以跟随一个数字值,这个值用于生成随机操作的种子,当你下一次再用这个种子值操作时,会重复上一次一样的操作。 比如:你执行 adb shell monkey -p com.bcoder.testapp -s 10 1000后发现了程序的一个报错 可以在修复问题后重新执行上面的命令,monkey还会按和上次一样的顺序生成随机事件 作者注:貌似不是每次的操作完全一样 |
--throttle <milliseconds> |
为每次的事件增加一个延时,用于放慢monkey的执行速度,单位毫秒 | |
--pct-touch <percent> |
设置触屏操作所占的百分比 | |
--pct-motion <percent> |
设置划屏操作所占的百分比 | |
--pct-trackball <percent> |
设置轨迹球操作所占的百分比 | |
--pct-nav <percent> |
设置方向键操作所占的百分比 Adjust percentage of “basic” navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.) |
|
--pct-majornav <percent> |
设置back键、menu键所占的百分比 Adjust percentage of “major” navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.) |
|
--pct-syskeys <percent> |
设置系统键所占的百分比 Adjust percentage of “system” key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.) |
|
--pct-appswitch <percent> |
Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package. |
|
--pct-anyevent <percent> |
Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth. | |
约束 | -p <allowed-package-name> |
指定一个或者多个包进行测试,比如-p com.bcoder.testapp,如果要指定多个包进行测试就要使用多个-p参数,比如adb shell monkey -p com.bcoder.testapp -p com.bcoder.testapp2 1000 If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you’ll need to specify those packages as well. If you don’t specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package. |
-c <main-category> |
If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don’t specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category. | |
调试 | --dbg-no-events |
When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application. |
--hprof |
If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files. | |
--ignore-crashes |
正常情况下,当测试遇到应用崩溃时,monkey就停止测试,如果使用了此参数,monkey将继续给系统发送事件直到运行完count所设定的值。 Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |
|
--ignore-timeouts |
正常情况下,当遇到ANR错误时,monkey就会停止测试,加上此参数后,monkey将会继续进行测试直到运行完所设定的count值。 Normally, the Monkey will stop when the application experiences any type of timeout error such as a “Application Not Responding” dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |
|
-f |
指定测试脚本文件,示例文件见文末尾 如:adb shell monkey -v -v -f /mnt/sdcard/monkey_click.txt 1000 |
|
--ignore-security-exceptions |
同上,忽略安全性异常错误。 Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |
|
--kill-process-after-error |
Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event. | |
--monitor-native-crashes |
Watches for and reports crashes occurring in the Android system native code. If –kill-process-after-error is set, the system will stop. | |
--wait-dbg |
Stops the Monkey from executing until a debugger is attached to it. |
-f参数脚本示例
1 2 3 4 5 6 7 |
# Start of Script type= user count= 49 speed= 1.0 start data >> Tap(215, 482, 100)//点击坐标x=215, y=482,延时100毫秒 |
注意事项:
- count参数一定要放到最后面