android压力测试工具Monkey使用方法

Monkey简介

官方地址: http://developer.android.com/tools/help/monkey.html

1.什么是Monkey?

      Monkey是一个命令行程序,可以运行在模拟器或设备当中,它向系统发送伪随机的用户事件流(如键盘输入、触摸等操作),对程序进行压力测试。

adb shell monkey [options] <event-count>


2.Monkey的特征

(1)测试的对象仅为应用程序包,有一定的局限性;

(2)Monkey使用的事件流是随机的,不能自定义事件;

(3)可对MonkeyTest的对象、事件数量、类型、频率进行设置(对于不同android系统设置不一定生效)。


Monkey参数:


Category Option Description
General --help Prints a simple usage guide.
-v Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing.
Events -s <seed> Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events.
--throttle <milliseconds> Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible.
--pct-touch <percent> Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.)
--pct-motion <percent> Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.)
--pct-trackball <percent> Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.)
--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> 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.
Constraints -p <allowed-package-name> 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.
Debugging --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 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 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.
--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.

一、常规

1、-help

作用:帮助,列出简单的用法。

例:adb shell monkey -help


2、-v

作用:设置日志级别,命令行上的每一个-v都将增加反馈信息的详细级别。

Level0(默认),除了启动、测试完成和最终结果外只提供较少的信息。

Level1,提供了较为详细的测试信息,如逐个发送到Activity的事件信息。

Level2,提供了更多的设置信息,如测试中选中或未选中的Activity信息。

:adb shell monkey -v 10


注:比较常用的是-v -v -v,即最多详细信息,一般会保存到指定文件中供开发人员查找bug原因时使用。


二、事件


1、-s <seed>

作用:伪随机数生成器的seed值。如果用相同的seed值再次运行monkey,将生成相同的事件序列。

例:adb shell monkey -s 12345 -v 10


2、--throttle <milliseconds>

作用:在事件之间插入固定的时间(毫秒)延迟,你可以使用这个设置来减缓Monkey的运行速度,如果你不指定这个参数,则事件之间将没有延迟,事件将以最快的速度生成。

:adb shell monkey --throttle 300 -v 10

注:常用参数,一般设置为300毫秒,原因是实际用户操作的最快300毫秒左右一个动作事件,所以此处一般设置为300毫秒。


3、--pct-touch <percent>

作用:调整点击事件的百分比。(触摸事件是指在屏幕中的一个down-up事件,即在屏幕某处按下并抬起的操作)

:adb shell monkey --pct-touch 100 -v 10

注:常用参数,此参数设置要适应当前被测应用程序的操作,比如一个应用80%的操作都是触摸,那就可以将此参数的百分比设置成相应较高的百分比。


4、--pct-motion <percent>

作用:调整滑动事件百分比。(motion事件是由屏幕上某处一个down事件、一系列伪随机的移动事件和一个up事件组成)

注:常用参数,需注意的是移动事件是直线滑动,下面的trackball移动包含曲线移动。


5、--pct-trackball <percent>

作用:调整滚动球事件百分比。(滚动球事件由一个或多个随机的移动事件组成,有时会伴随着点击事件)

注:现在手机几乎没有滚动球,所以这个参数几乎不用了。但滚动球事件中包含曲线滑动事件,在被测程序需要曲线滑动时可以选用此参数。


6、--pct-nav <percent>

作用:调整基本的导航事件百分比。(导航事件由方向输入设备的上下左右按键所触发的事件组成)

注:不常用操作。


7、--pct-majornav <percent>

作用:调整主要导航事件的百分比。(这些导航事件通常会导致UI界面中的动作事件,如5-way键盘的中间键,回退按键、菜单按键)

注:不常用。


8、--pct-syskeys <percent>

作用:调整系统事件百分比。(这些按键通常由系统保留使用,如Home、Back、Start Call、End Call、音量调节)

注:不常用。


9、--pct-appswitch <percent>

作用:调整Activity启动的百分比。(在随机的时间间隔中,Monkey将执行一个startActivity()调用,作为最大程度覆盖被测包中全部Activity的一种方法)

注:不常用。


10、--pct-anyevent

作用:调整其他事件的百分比。(这包含所有其他事件,如按键、其他在设备上不常用的按钮等)

注:不常使用。


三、约束条件

1、-p <allowed-package-name>

作用:如果你指定一个或多个包,Monkey将只允许访问这些包中的Activity。如果你的应用程序需要访问这些包(如选择联系人)以外的Activity,你需要指定这些包。如果你不指定任何包,Monkey将允许系统启动所有包的Activity。指定多个包,使用多个-p,一个-p后面接一个包名。

注:常用参数。


2、-c <main-category>

作用:如果你指定一个或多个类别,Monkey将只允许系统启动这些指定类别中列出的Activity。如果你不指定任何类别,Monkey将选择谢列类别中列出的Activity,Intent.CATEGORY_LAUNCHER和Intent.CATEGORY_MONKEY。指定多个类别使用多个-c,每个-c指定一个类别。

注:不常用。


3、--dbg-no-events

作用:设置此选项,Monkey将执行初始启动,进入一个测试Activity,并不会在进一步生成事件。为了得到最佳结果,结合参数-v,一个或多个包的约束,以及一个保持Monkey运行30秒或更长时间的非零值,从而提供了一个可以监视应用程序所调用的包之间转换的环境。


4、--hprof

作用:设置此选项,将在Monkey生成事件序列前后生成profilling报告。在data/misc路径下生成大文件(~5Mb),所以要小心使用。


5、--ignore-crashes

作用:通常,应用发生崩溃或异常时Monkey会停止运行。如果设置此项,Monkey将继续发送事件给系统,直到事件计数完成。


6、--ignore-timeouts

作用:通常,应用程序发生任何超时错误(如“Application Not responding”对话框)Monkey将停止运行,设置此项,Monkey将继续发送事件给系统,直到事件计数完成。


7、--ignore-security-exception 

作用:通常,当程序发生许可错误(例如启动一些需要许可的Activity)导致的异常时,Monkey将停止运行。设置此项,Monkey将继续发送事件给系统,直到事件计数完成。


8、--kill-process-after-error

作用:通常,当Monkey由于一个错误而停止时,出错的应用程序将继续处于运行状态。设置此项,将会通知系统停止发生错误的进程。注意,正常(成功)的结束,并没有停止启动的进程,设备只是在结束事件之后简单的保持在最后的状态。


9、--monitor-native-crashes

作用:监视并报告Andorid系统中本地代码的崩溃事件。如果设置--kill-process-after-error,系统将停止运行。


10、--wait-dbg

作用:停止执行中的Monkey,直到有调试器和它相连接。


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值