Android之自動化壓力測試工具:Monkey的使用

來源:互聯網
上載者:User

monkey 壓力測試
adb -d shell monkey -p your.package.name -v 6000

 一、  什麼是Monkey

    Monkey是Android中的一個命令列工具,可以運行在模擬器裡或實際裝置中。它向系統發送偽隨機的使用者事件流(如按鍵輸入、觸控螢幕輸入、手勢輸入等),實現對正在開發的應用程式進行壓力測試。Monkey測試是一種為了測試軟體的穩定性、健壯性的快速有效方法。

    二、 Monkey的特徵

    1、測試的對象僅為應用程式套件組合,有一定的局限性。

    2、 Monky測試使用的事件流資料流是隨機的,不能進行自訂。

    3、可對MonkeyTest的對象,事件數目量,類型,頻率等進行設定。

    三、Monkey的基本用法

    基本文法如下:

    $ adb shell monkey [options]

    如果不指定options,Monkey將以無反饋模式啟動,並把事件任意發送到安裝在目標環境中的全部包。下面是一個更為典型的命令列樣本,它啟動指定的應用程式,並向其發送1500個偽隨機事件:

    $ adb shell monkey -p your.package.name -v 1500

    $ monkey -p(package的意思)  指定檔案名稱 -v(測試的次數和頻率) number(次數)

    四、Monkey測試的一個執行個體

    通過這個執行個體,我們能理解Monkey測試的步驟以及如何知道哪些應用程式能夠用Monkey進行測試。

    Windows下(註:2—4步是為了查看我們可以測試哪些應用程式套件組合,可省略):

    1、 通過eclipse啟動一個Android的emulator

    2、 在命令列中輸入:adb devices查看裝置串連情況

    C:\Documents and Settings\Administrator>adb devices

    List of devices attached

    emulator-5554   device

    3、 在有裝置串連的前提下,在命令列中輸入:adb shell 進入shell介面

    C:\Documents and Settings\Administrator>adb shell

    #

    4、 查看data/data檔案夾下的應用程式套件組合。註:我們能測試的應用程式套件組合都在這個目錄下面

    C:\Documents and Settings\Administrator>adb shell

    # ls data/data

    ls data/data

    com.google.android.btrouter

    com.android.providers.telephony

    com.android.mms

    com.android.providers.downloads

    com.android.deskclock

    com.android.email

    com.android.providers.media

    com.android.settings

    jp.co.omronsoft.openwnn

    com.android.providers.userdictionary

    com.android.quicksearchbox

    com.android.protips

    com.android.browser

    com.android.launcher

    com.android.term

    com.android.speechrecorder

    com.android.server.vpn

    com.android.defcontainer

    com.svox.pico

    com.android.customlocale

    com.android.development

    com.android.soundrecorder

    com.android.providers.drm

    com.android.spare_parts

    com.android.providers.downloads.ui

    com.android.fallback

    com.android.providers.applications

    com.android.netspeed

    com.android.wallpaper.livepicker

    android.tts

    com.android.htmlviewer

    com.android.music

    com.android.certinstaller

    com.android.inputmethod.pinyin

    com.android.providers.subscribedfeeds

    com.android.inputmethod.latin

    com.android.gallery

    com.android.systemui

    com.android.contacts

    com.android.phone

    com.android.sdksetup

    com.android.calculator2

    com.android.packageinstaller

    com.android.camera

    com.android.providers.settings

    com.thestore.main

    com.android.providers.contacts

    5、 以com.android.camera作為對象進行MonkeyTest

    #monkey -p com.android.camera -v 500

    其中-p表示對象包 –v 表示事件數目量

    運行過程中,Emulator中的應用程式在不斷地切換畫面。

    按照選定的不同層級的反饋資訊,在Monkey中還可以看到其執行過程報告和產生的事件。

    註:具體參數的設定可參考:

    http://developer.android.com/guide/developing/tools/monkey.html

    五、關於Monkey測試的停止條件

    Monkey Test執行過程中在下列三種情況下會自動停止:

    1、如果限定了Monkey運行在一個或幾個特定的包上,那麼它會監測試圖轉到其它包的操作,並對其進行阻止。

    2、如果應用程式崩潰或接收到任何失控異常,Monkey將停止並報錯。

    3、如果應用程式產生了應用程式不響應(application not responding)的錯誤,Monkey將會停止並報錯。

    通過多次並且不同設定下的Monkey測試才算它是一個穩定性足夠的程式。
    
    
下面是開發文檔的原文,對其他的命令也有詳細解釋::   
http://developer.android.com/guide/developing/tools/monkey.html:    
    
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] <event-count>

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

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.