主要内容

  • monkey 基本使用
  • monkey 参数说明

monkey基本使用

  1. adb shell monkey -v -p 包名 --pct-touch 100 100
  1. adb shell monkey -p com.qiku.bbs --ignore-crashes --ignore-timeouts --ignore-security-exceptions --pct-touch 100 -v -v -v --throttle 1000 100

monkey参数说明

-p 包名:指定应用程序。例如:adb shell monkey -p 包名 事件总数
-v:打印log级别,-v越多日志信息越详细,最多支持3个。例如:adb shell monkey -p 包名 -v -v -v 事件总数
-s:伪随机数生成器的 seed 值,通俗的说就是个标记,后面跟数字,例如:执行adb shell monkey -s 1 -p 包名 事件总数,这个我标记了-s 1,命令操作完之后,我发现有日志报错,我想重新执行这个monkey操作,那你就可以继续执行这个命令,排错时常用。
-f:后接测试脚本名,例如:adb shell monkey -f 脚本名 事件总数
—throttle:翻译减速的意思,后面接时间,单位为ms,,表示事件之间的固定延迟,如果不接该项,monkey将不会延迟,例如:adb shell monkey —throttle 500 -p 包名 事件总数

—pct-事件类别 11个事件百分比控制(有的是9种事件,没有—pct-pinchzoom,—pct-rotation事件)由安卓SDK决定
11种事件,按顺序罗列的
—pct-touch {+百分比}:0
翻译触摸,触摸事件泛指发生在某一位置的一个down-up事件,点击
—pct-motion {+百分比}:1
翻译动作,动作事件泛指从某一位置接下(即down事件)后经过一系列伪随机事件后弹出(即up事件)
—pct-pinchzoom {+百分比}:2
翻译二指缩放,智能机上的放大缩小手势操作事件
—pct-trackball {+百分比}: 3
翻译轨迹,轨迹事件包括一系列的随机移动,以及偶尔跟随在移动后面的点击事件
—pct-rotation {+百分比}:4
翻译屏幕旋转,横屏竖屏事件
—pct-nav {+百分比}:5
翻译基本导航,基本导航事件主要来自方向输入设备的上、下、左、右事件
—pct-majornav {+百分比}:6
翻译主要导航,主要导航事件通常指引发图形界面的一些动作,如键盘中间按键、返回按键、菜单按键等
—pct-syskeys {+百分比}:7
翻译系统按键,系统按键事件通常指仅供系统使用的保留按键,如HOME键、BACK键、拨号键、挂断键、音量键等
—pct-appswitch {+百分比}:8
翻译应用启动,应用启动事件(activity launches)即打开应用,通过调用startActivity()方法最大限度地开启该package下的所有应用
—pct-flip {+百分比}:9
翻译翻转,键盘轻弹百分比,如点击输入框,键盘弹起,点击输入框以外区域,键盘收回
—pct-anyevent {+百分比}:10
翻译其他类型,其他类型事件指上文中未涉及的所有其他事件,如keypress、不常用的button等
百分比控制以及使用
首先注意一点,这个Event percentages在不同版本的SDK版本中顺序可能会不一样。
如果在monkey参数中不指定事件参数,这些动作都是随机分配的,11个动作其分配的百分比之和为100%,通过添加命令选项来控制每个事件的百分比。
添加事件百分比之后的情况,举例添加—pct-touch事件百分比为100:

官方文档参考

https://developer.android.com/studio/test/monkey

The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.

Overview

The Monkey is a command-line tool that you can run on any emulator instance or on a device. It sends a pseudo-random stream of user events into the system, which acts as a stress test on the application software you are developing.
The Monkey includes a number of options, but they break down into four primary categories:

  • Basic configuration options, such as setting the number of events to attempt.
  • Operational constraints, such as restricting the test to a single package.
  • Event types and frequencies.
  • Debugging options.

When the Monkey runs, it generates events and sends them to the system. It also watches the system under test and looks for three conditions, which it treats specially:

  • If you have constrained the Monkey to run in one or more specific packages, it watches for attempts to navigate to any other packages, and blocks them.
  • If your application crashes or receives any sort of unhandled exception, the Monkey will stop and report the error.
  • If your application generates an application not responding error, the Monkey will stop and report the error.

Depending on the verbosity level you have selected, you will also see reports on the progress of the Monkey and the events being generated.

Basic use of the Monkey

You can launch the Monkey using a command line on your development machine or from a script. Because the Monkey runs in the emulator/device environment, you must launch it from a shell in that environment. You can do this by prefacing adb shell to each command, or by entering the shell and entering Monkey commands directly.
The basic syntax is:
$ adb shell monkey [options]
With no options specified, the Monkey will launch in a quiet (non-verbose) mode, and will send events to any (and all) packages installed on your target. Here is a more typical command line, which will launch your application and send 500 pseudo-random events to it:
$ adb shell monkey -p your.package.name -v 500

Command options reference

The table below lists all options you can include on the Monkey command line.

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. For information on analyzing profiling reports, see Profile your app performance.
--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.

UI/Application Exerciser Monkey

Monkey 是一个在模拟器或设备上运行的程序,可生成伪随机用户事件(例如点击、轻触或手势)流以及很多系统级事件。您可以使用 Monkey 以随机且可重复的方式对正在开发的应用进行压力测试。

概览

Monkey 是一个命令行工具,可以在任何模拟器实例或设备上运行。它会将伪随机用户事件流发送到系统中,从而在您正在开发的应用软件上进行压力测试。
Monkey 包含许多选项,主要分为以下四个类别:

  • 基本配置选项,例如设置要尝试的事件数。
  • 操作限制条件,例如将测试对象限制为单个软件包。
  • 事件类型和频率。
  • 调试选项。

Monkey 在运行时会生成事件并将其发送到系统。它还会监视被测系统并查找三种特殊情况:

  • 如果您已将 Monkey 限制为在一个或多个特定软件包中运行,它会监视转到任何其他软件包的尝试并阻止它们。
  • 如果应用崩溃或收到任何未处理的异常,Monkey 会停止并报告错误。
  • 如果应用生成“应用无响应”错误,Monkey 会停止并报告错误。

根据您选择的详细程度级别,您还将看到有关 Monkey 进度和所生成事件的报告。

Monkey 的基本用法

您可以使用开发计算机上的命令行启动 Monkey,也可以通过脚本启动。由于 Monkey 在模拟器/设备环境中运行,因此您必须从该环境中通过 shell 启动它。为此,您可以在每个命令前面加上 adb shell,或者直接进入 shell 并输入 Monkey 命令。
基本语法如下:

  1. $ adb shell monkey [options] <event-count>

如果未指定任何选项,Monkey 将以静默(非详细)模式启动,并将事件发送到目标上安装的任何(及所有)软件包。下面是一个更典型的命令行,它会启动您的应用并向其发送 500 个伪随机事件:

  1. $ adb shell monkey -p your.package.name -v 500

命令选项参考

下表列出了您可以在 Monkey 命令行中添加的所有选项。

类别 选项 说明
常规 --help 输出简单的使用指南。
-v 命令行上的每个 -v 都会增加详细程度级别。级别 0(默认值)只提供启动通知、测试完成和最终结果。级别 1 提供有关测试在运行时(例如发送到您的 Activity 的各个事件)的更多详细信息。级别 2 提供更详细的设置信息,例如已选择或未选择用于测试的 Activity。
事件 -s <seed> 伪随机数生成器的种子值。如果您使用相同的种子值重新运行 Monkey,它将会生成相同的事件序列。
--throttle <milliseconds> 在事件之间插入固定的延迟时间。您可以使用此选项减慢 Monkey 速度。如果未指定,则没有延迟,系统会尽快地生成事件。
--pct-touch <percent> 调整轻触事件所占百分比。(轻触事件是指屏幕上的单个位置上的按下/释放事件。)
--pct-motion <percent> 调整动作事件所占百分比。(动作事件包括屏幕上某个位置的按下事件,一系列伪随机动作和一个释放事件。)
--pct-trackball <percent> 调整轨迹球事件所占百分比。(轨迹球事件包括一个或多个随机动作,有时后跟点击。)
--pct-nav <percent> 调整“基本”导航事件所占百分比。(导航事件包括向上/向下/向左/向右,作为方向输入设备的输入。)
--pct-majornav <percent> 调整“主要”导航事件所占百分比。(这些导航事件通常会导致界面中的操作,例如 5 方向键的中间按钮、返回键或菜单键。)
--pct-syskeys <percent> 调整“系统”按键事件所占百分比。(这些按键通常预留供系统使用,例如“主屏幕”、“返回”、“发起通话”、“结束通话”或“音量控件”。)
--pct-appswitch <percent> 调整 Activity 启动次数所占百分比。Monkey 会以随机间隔发起 startActivity() 调用,以最大限度地覆盖软件包中的所有 Activity。
--pct-anyevent <percent> 调整其他类型事件所占百分比。这包括所有其他类型的事件,例如按键、设备上的其他不太常用的按钮等等。
约束 -p <allowed-package-name> 如果您通过这种方式指定一个或多个软件包,Monkey 将仅允许系统访问这些软件包内的 Activity。如果应用需要访问其他软件包中的 Activity(例如选择联系人),您还需要指定这些软件包。如果您未指定任何软件包,Monkey 将允许系统启动所有软件包中的 Activity。要指定多个软件包,请多次使用 -p 选项 - 每个软件包对应一个 -p 选项。
-c <main-category> 如果您通过这种方式指定一个或多个类别,Monkey 将仅允许系统访问其中一个指定类别中所列的 Activity。如果您没有指定任何类别,Monkey 会选择 Intent.CATEGORY_LAUNCHER 或 Intent.CATEGORY_MONKEY 类别所列的 Activity。要指定多个类别,请多次使用 -c 选项 - 每个类别对应一个 -c 选项。
调试 --dbg-no-events 指定后,Monkey 将初始启动到测试 Activity,但不会生成任何其他事件。为了获得最佳结果,请结合使用 -v、一个或多个软件包约束条件以及非零限制,以使 Monkey 运行 30 秒或更长时间。这提供了一个环境,您可以在其中监控应用调用的软件包转换操作。
--hprof 如果设置此选项,此选项将在 Monkey 事件序列之前和之后立即生成分析报告。这将在 data/misc 下生成大型(约为 5Mb)文件,因此请谨慎使用。要了解如何分析性能分析报告,请参阅分析应用性能
--ignore-crashes 通常,当应用崩溃或遇到任何类型的未处理异常时,Monkey 将会停止。如果您指定此选项,Monkey 会继续向系统发送事件,直到计数完成为止。
--ignore-timeouts 通常情况下,如果应用遇到任何类型的超时错误(例如“应用无响应”对话框),Monkey 将会停止。如果您指定此选项,Monkey 会继续向系统发送事件,直到计数完成为止。
--ignore-security-exceptions 通常情况下,如果应用遇到任何类型的权限错误(例如,如果它尝试启动需要特定权限的 Activity),Monkey 将会停止。如果您指定此选项,Monkey 会继续向系统发送事件,直到计数完成为止。
--kill-process-after-error 通常情况下,当 Monkey 因出错而停止运行时,出现故障的应用将保持运行状态。设置此选项后,它将会指示系统停止发生错误的进程。注意,在正常(成功)完成情况下,已启动的进程不会停止,并且设备仅会处于最终事件之后的最后状态。
--monitor-native-crashes 监视并报告 Android 系统原生代码中发生的崩溃。如果设置了 —kill-process-after-error,系统将会停止。
--wait-dbg 阻止 Monkey 执行,直到为其连接了调试程序。

monkey日志查看

一、初步分析方法:Monkey测试出现错误后,一般的差错步骤为以下几步:
1、找到是monkey里面的哪个地方出错
2、查看Monkey里面出错前的一些事件动作,并手动执行该动作
3、若以上步骤还不能找出,可以使用之前执行的monkey命令再执行一遍,注意seed值要一样一般的测试结果分析:1、ANR问题:在日志中搜索“ANR”2、崩溃问题:在日志中搜索“Exception”
(ARN就是Application Not Responding。
-在Android中,活动管理器和窗口管理器这两个系统服务负责监视应用程序的响应,当用户操作的在5s内应用程序没能做出反应,BroadcastReceiver在10秒内没有执行完毕,就会出现应用程序无响应对话框,这既是ANR。)

二、详细分析monkey日志:首先我们需要查看Monkey测试中是否出现了ANR或者异常,具体方法如上述。将执行Monkey生成的log,从手机中导出并打开查看该log;在log的最开始都会显示Monkey执行的seed值、执行次数和测试的包名。具体方法如上述。
然后我们要分析log中的具体信息,方法如下:查看log中第一个Switch,主要是查看Monkey执行的是那一个Activity,譬如下面的log中,执行的是com.tencent.smtt.SplashActivity,在下一个swtich之间的,如果出现了崩溃或其他异常,可以在该Activity中查找问题的所在。:Switch:#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10000000;component=com.tencent.smtt/.SplashActivity;end
// Allowing start of Intent {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]cmp=com.tencent.smtt/.SplashActivity } in package com.tencent.smtt
在下面的log中,Sending Pointer ACTION_DOWN和Sending Pointer ACTION_UP代表当前执行了一个单击的操作;Sleeping for 500 milliseconds这句log是执行Monkey测试时,throttle设定的间隔时间,每出现一次,就代表一个事件。SendKey(ACTION_DOWN) //KEYCODE_DPAD_DOWN 代表当前执行了一个点击下导航键的操作;Sending Pointer ACTION_MOVE 代表当前执行了一个滑动界面的操作。:Sending Pointer ACTION_DOWN x=47.0 y=438.0
:Sending Pointer ACTION_UP x=47.0 y=438.0
Sleeping for 500 milliseconds
:SendKey (ACTION_DOWN): 20 //KEYCODE_DPAD_DOWN
:SendKey (ACTION_UP): 20 //KEYCODE_DPAD_DOWN
Sleeping for 500 milliseconds
:Sending Pointer ACTION_MOVE x=-2.0 y=3.0
:Sending Pointer ACTION_MOVE x=4.0 y=-3.0
:Sending Pointer ACTION_MOVE x=-5.0 y=-3.0
:Sending Pointer ACTION_MOVE x=3.0 y=4.0
:Sending Pointer ACTION_MOVE x=-4.0 y=1.0
:Sending Pointer ACTION_MOVE x=-1.0 y=-1.0
:Sending Pointer ACTION_MOVE x=-2.0 y=-4.0
如果Monkey测试顺利执行完成,在log的最后,会打印出当前执行事件的次数和所花费的时间;// Monkey finished代表执行完成。Monkey执行中断,在log的最后也能查看到当前已执行的次数。Monkey执行完成的log具体如下:Events injected: 6000
:Dropped: keys=0 pointers=9 trackballs=0 flips=0
## Network stats: elapsed time=808384ms (0ms mobile, 808384ms wifi, 0msnot connected)
// Monkey finished