1. 程式人生 > >Monkey穩定性測試

Monkey穩定性測試

1 Monkey測試簡介

   Monkey是SDK中自帶的工具,在sdk的tools資料夾下,連線Android裝置時,命令列裡輸入相應命令就能執行;
   使用者可以模擬Application切換、橫豎屏、應用關閉等等,但Monkey對應用的操作是隨機的,如果需要指定操作,可以在專案中引入UI自動化,
如Robotium、Appium等;
   Monkey測試結果裡不能有nullPointException和Crash出現,開發人員結合monkey列印的日誌和系統列印的日誌,解決測試中出現的問題。

2 Monkey 測試命令
2.1 命令引數

adb shell monkey [options]

usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]
              [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]
              [--ignore-crashes][--ignore-timeouts][--ignore-security-exceptions][--monitor-native-crashes][--ignore-native-crashes][--kill-process-after-error][--hprof][--pct-touch
PERCENT][--pct-motion PERCENT][--pct-trackball PERCENT][--pct-syskeys PERCENT][--pct-nav PERCENT][--pct-majornav PERCENT][--pct-appswitch PERCENT][--pct-flip PERCENT][--pct-anyevent PERCENT][--pct-pinchzoom PERCENT][--pct-permission PERCENT][--pkg-blacklist-file PACKAGE_BLACKLIST_FILE][--pkg-whitelist
-file PACKAGE_WHITELIST_FILE][--wait-dbg][--dbg-no-events][--setup scriptfile][-f scriptfile [-f scriptfile] ...] [--port port][-s SEED][-v [-v] ...] [--throttle MILLISEC][--randomize-throttle][--profile-wait MILLISEC][--device-sleep-time MILLISEC][--randomize-script][--script-log][--bugreport][--periodic-bugreport][--permission-target-system] COUNT

2.2 引數詳解

-p:指定有效的package(如不指定,則對系統中所有package有效),一個-p 對應一個有效package, 

-p com.moco.mcplayer -p com.moco.mcplayer.alpha


-c:activity必須至少包含一個指定的category  (Intent.CATEGORY_LAUNCHERIntent.CATEGORY_MONKEY ),才能被啟動,否則啟動不了。( 這裡指直接被monkey程式調起,介面中被點選事件啟動的不包括在內)

-c  android.intent.category.CATEGORY_MONKEY


--ignore-crashes :  忽略崩潰(不中斷測試,之後可以在日誌中查詢資訊)

--ignore-timeouts: 忽略超時ANR (不中斷測試)

--ignore-security-exceptions: 忽略安全異常(不中斷測試)

--monitor-native-crashes: 跟蹤native方法的崩潰問題

--ignore-native-crashes: 忽略native崩潰 (不中斷測試)

--kill-process-after-error:發生錯誤後直接殺掉程序

--hprof: 開始測試前和測試結束後生成trace 報告,會在/data/misc 目錄下產生大檔案

--pct-touch: 觸控事件,佔總事件的百分比

--pct-motion: 滑動事件百分比

--pct-trackball:  軌跡球事件百分比
--pct-syskeys: 系統按鍵事件百分比 (HomeBack 、startCall 、 endCall 、 volumeControl)

--pct-nav: 導航事件百分比(上,下,左,右)

--pct-majornav: 主要導航事件百分比( back key 、 menu key)

--pct-appswitch:  Activity切換事件百分比

--pct-flip: 鍵盤翻轉事件百分比

--pct-anyevent: 其他任意事件百分比 (其他不常用裝置按鈕按鍵等)

--pct-pinchzoom: 縮放事件百分比

--pct-permission:

--pkg-blacklist-file:  指定不執行monkey的包名列表檔案

--pkg-whitelist-file: 指定執行monkey的包名列表檔案

--wait-dbg:   應用偵錯程式 debugger attach 之前 monkey 不會開始傳送事件

--dbg-no-events: 初始化啟動的activity,但是不產生任何事件

-s: 產生隨機數的種子,使用同一個seed, monkey 會確保生成相同的事件

-v: 日誌級別,預設為0,增加 -v 的個數可以增加日誌級別,輸出更詳細日誌(最多3個 -v -v -v)

--throttle: 事件之間的間隔,不設定時,在android系統極限內操作,若手機效能較低,容易出現系統無響應,最佳的時間間隔在300或者500(毫秒)

--randomize-throttle: 設定隨機時間的事件間隔

--device-sleep-time: 裝置休眠時間

--bugreport: 設定Monkey執行時自動記錄發生crash,anr,system not responding的錯誤資訊

2.3 命令例項

adb shell monkey -p com.moco.mcplayer -v 400000 -throttle 10 --pct-touch 30 -–pct-motion 20 -–pct-nav 20 -–pct-majornav 15 -–pct-appswitch 5 -–pct-anyevent 5 -–pct-trackball 5 -–pct-syskeys 0 '%s' --ignore-crashes --ignore-timeouts --ignore-security-exceptions > E:\monkey_log\monkey_log.txt

2.4 命令解釋

-p com.moco.mcplayer

執行程式包名為 com.moco.mcplayer

—ignore-crashes

忽略崩潰問題,遇到問題繼續執行monkey測試

—ignore-timeouts

忽略anr問題,遇到問題繼續執行monkey測試

—ignore-security-exceptions

忽略安全問題,遇到問題繼續執行monkey測試

—pct-trackball 0

軌跡球事件 0%

—pct-nav 0

導航事件 0%

—pct-majornav 0

主要導航事件 0%

—pct-anyevent 0

其他不常用任意事件 0%

-v -v -v

日誌級別最高,記錄所有資訊

—throttle 500

事件之間間隔 500 毫秒

1200000000

總共傳送1200000000個事件(一次500毫秒,1200000000 需要約 6944 天)

> E:\monkey_log\monkey_log.txt

monkey 執行日誌儲存至 E盤 monkey_log 資料夾 monkey_log.txt 檔案

3.日誌分析
3.1 日誌示例

:Monkey: seed=1467305921677 count=120000000
:AllowPackage: com.moco.tcl
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Selecting main activities from category android.intent.category.LAUNCHER
//   - NOT USING main activity com.android.camera.CameraLauncher (from package com.tct.camera3)
//   - NOT USING main activity com.android.settings.SettingsTab (from package com.android.settings)
//   - NOT USING main activity com.android.deskclock.DeskClock (from package com.android.deskclock)
//   - NOT USING main activity com.android.contacts.activities.PeopleActivity (from package com.android.contacts)
//   - NOT USING main activity com.android.dialer.DialtactsActivity (from package com.android.contacts)
//   + Using main activity com.moco.tcl.CSplashScreen (from package com.moco.tcl)
// Selecting main activities from category android.intent.category.MONKEY
// Seeded: 1467305921677
// Event percentages:
//   0: 46.875%
//   1: 31.25%
//   2: 6.25%
//   3: -0.0%
//   4: -0.0%
//   5: -0.0%
//   6: -0.0%
//   7: -0.0%
//   8: 6.25%
//   9: 6.25%
//   10: 3.125%
//   11: -0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.moco.tcl/.CSplashScreen;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.moco.tcl/.CSplashScreen } in package com.moco.tcl
Sleeping for 500 milliseconds
:Sending Touch (ACTION_DOWN): 0:(22.0,1036.0)
:Sending Touch (ACTION_UP): 0:(33.727005,1025.7482)
Sleeping for 500 milliseconds
:Sending Touch (ACTION_DOWN): 0:(840.0,1031.0)
:Sending Touch (ACTION_UP): 0:(823.241,1017.319)
Sleeping for 500 milliseconds
:Sending Touch (ACTION_DOWN): 0:(429.0,1721.0)
:Sending Touch (ACTION_UP): 0:(432.43265,1730.7511)
Sleeping for 500 milliseconds
:Sending Touch (ACTION_DOWN): 0:(917.0,956.0)

:Sending Touch (ACTION_UP): 0:(919.1181,955.37024)
Sleeping for 500 milliseconds
:Sending Touch (ACTION_DOWN): 0:(393.0,537.0)
:Sending Touch (ACTION_UP): 0:(392.4326,534.28046)
Sleeping for 500 milliseconds
:Sending Touch (ACTION_DOWN): 0:(975.0,1496.0)
:Sending Touch (ACTION_UP): 0:(977.1913,1503.4038)
Sleeping for 500 milliseconds

3.2 日誌解析

:Monkey: seed=1467305921677 count=120000000
monkey執行的seed值和隨機事件次數

:AllowPackage: com.moco.tcl
可以執行的包名

// Event percentages:
//   0: 46.875%
//   1: 31.25%
//   2: 6.25%
//   3: -0.0%
//   4: -0.0%
//   5: -0.0%
//   6: -0.0%
//   7: -0.0%
//   8: 6.25%
//   9: 6.25%
//   10: 3.125%
//   11: -0.0%

分配事件的百分比
0:觸控事件百分比,即引數–pct-touch
1:滑動事件百分比,即引數–pct-motion
2:縮放事件百分比,即引數–pct-pinchzoom
3:軌跡球事件百分比,即引數–pct-trackball
4:螢幕旋轉事件百分比,即引數–pct-rotation

5:基本導航事件百分比,即引數–pct-nav
6:主要導航事件百分比,即引數–pct-majornav
7:系統事件百分比,即引數–pct-syskeys
8:Activity啟動事件百分比,即引數–pct-appswitch
9:鍵盤翻轉事件百分比,即引數–pct-flip
10:其他事件百分比,即引數–pct-anyevent

事件0:觸控事件

Sending Touch (ACTION_DOWN): 0:(572.0,1105.0)
Sending Touch (ACTION_UP): 0:(576.20734,1105.024)

事件1:滑動事件

  Sending Touch (ACTION_DOWN): 0:(233.0,761.0)
  Sending Touch (ACTION_MOVE): 0:(208.49568,736.34766)
  Sending Touch (ACTION_MOVE): 0:(202.7063,729.8338)
  Sending Touch (ACTION_MOVE): 0:(183.89723,722.677)
  Sending Touch (ACTION_UP): 0:(174.83568,721.8229)

事件2:縮放事件

  Sending Touch (ACTION_DOWN): 0:(107.0,242.0)
  Sending Touch (ACTION_POINTER_DOWN 1): 0:(108.14705,248.53061) 1:(270.0,262.0)
  Sending Touch (ACTION_MOVE): 0:(110.117355,252.96329) 1:(267.9937,262.25485)
  Sending Touch (ACTION_MOVE): 0:(111.30056,261.88846) 1:(261.90106,262.58475)
  Sending Touch (ACTION_MOVE): 0:(113.11743,265.60138) 1:(253.92662,263.13382)
  Sending Touch (ACTION_POINTER_UP 1): 0:(113.29031,267.4419) 1:(248.60628,263.23257)

事件3:軌跡球事件

  Sending Trackball (ACTION_MOVE): 0:(3.0,-2.0)
  Sending Trackball (ACTION_MOVE): 0:(1.0,-1.0)

事件4:螢幕旋轉事件(隱藏事件)

Sending rotation degree=0,persist=true

事件5:導航事件(上下左右)

Sending Key (ACTION_DOWN): 21    // KEYCODE_DPAD_LEF

事件6:主要導航事件(menu等)

 Sending Key (ACTION_DOWN): 23    // KEYCODE_DPAD_CENTER

事件7:系統按鍵事件(音量,home,返回按鍵等)

Sending Key (ACTION_UP): 25    // KEYCODE_VOLUME_DOWN

事件8:啟動應用事件

 :Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.moco.tcl/.CSplashScreen;end

事件9:鍵盤事件(隱藏顯示鍵盤)

Sending Flip keyboardOpen=true

事件10:其他按鍵

 Sending Key (ACTION_DOWN): 66    // KEYCODE_ENTER
 Sending Key (ACTION_UP): 66    // KEYCODE_ENTER

延時

Sleeping for 500 milliseconds

3.3異常分析
ANR 問題,Monkey 日誌中會打印出詳細的ANR Trace,可以通過ANR Trace分析具體問題

:Sending Touch (ACTION_DOWN): 0:(98.0,315.0) 
// NOT RESPONDING: com.moco.mcplayer (pid 1014) 
ANR in com.moco.mcplayer (com.moco.mcplayer/.CSplashScreen) 
Reason: keyDispatchingTimedOut 
Load: 4.55 / 4.38 / 3.69 
CPU usage from 4481ms to -1188ms ago: 
  17% 332/system_server: 12% user + 5.4% kernel / faults: 1240 minor 7 major 
  2.3% 134/surfaceflinger: 1.5% user + 0.7% kernel 
  1.9% 1014/com.moco.mcplayer: 0.7% user + 1.2% kernel / faults: 393 minor 3 major 
  1.4% 2699/com.moco.mcplayer:Picture: 1.4% user + 0% kernel 
  1% 2477/com.wandoujia.player.walkman: 0.7% user + 0.3% kernel / faults: 328 minor 2 major 
  0.7% 2144/kworker/0:2: 0% user + 0.7% kernel 
  0.7% 2511/kworker/1:2: 0% user + 0.7% kernel 

  0.5% 72/irq/379-lis3dh: 0% user + 0.5% kernel 
  0.3% 450/com.android.systemui: 0.3% user + 0% kernel / faults: 100 minor 
  0% 1886/com.tencent.mm:push: 0% user + 0% kernel / faults: 526 minor 2 major 
  0.1% 83/mmcqd/0: 0% user + 0.1% kernel 
  0.1% 481/dhd_dpc: 0% user + 0.1% kernel 
  0% 518/wpa_supplicant: 0% user + 0% kernel 
  0% 554/com.lbe.security.miui: 0% user + 0% kernel / faults: 185 minor 
  0% 569/com.xiaomi.xmsf: 0% user + 0% kernel / faults: 333 minor 3 major 
  0% 587/com.duokan.airkan.phone: 0% user + 0% kernel / faults: 182 minor 
  0% 776/com.tencent.mobileqq:MSF: 0% user + 0% kernel / faults: 4 minor 
  0.1% 2597/kworker/u:1: 0% user + 0.1% kernel 
  0.1% 2659/kworker/u:3: 0% user + 0.1% kernel 
  0% 18774/com.sina.weibo: 0% user + 0% kernel / faults: 6 minor 
  18% TOTAL: 9.1% user + 4.4% kernel + 4.8% iowait + 0% softirq 
  CPU usage from 596ms to 1117ms later with 99% awake: 
  9.4% 332/system_server: 5.6% user + 3.7% kernel / faults: 42 minor 
  5.6% 403/InputDispatcher: 3.7% user + 1.8% kernel 
  3.7% 338/Compiler: 3.7% user + 0% kernel 
  1.8% 402/er$SensorThread: 0% user + 1.8% kernel 
  6.4% 1014/com.moco.mcplayer: 1.2% user + 5.1% kernel / faults: 185 minor 
  6.4% 1258/AsyncTask #2: 2.5% user + 3.8% kernel 
  1.2% 518/wpa_supplicant: 0% user + 1.2% kernel 
  1.3% 2477/com.wandoujia.player.walkman: 1.3% user + 0% kernel 
  1.3% 2511/kworker/1:2: 0% user + 1.3% kernel 
  11% TOTAL: 4.8% user + 6.7% kernel 

procrank: 
  PID Vss Rss Pss Uss cmdline 
 1014 177420K 172008K 86485K 43464K com.moco.mcplayer 
 2699 100016K 99544K 58213K 54608K com.moco.mcplayer:Picture 
 3408 125964K 120620K 49817K 9688K com.moco.mcplayer 
 31167 76560K 76220K 41223K 36704K com.moco.mcplayer 
 450 74516K 70188K 34378K 32332K com.android.systemui 
 332 66160K 65556K 30428K 28492K system_server 
 597 60840K 60708K 25572K 24020K com.miui.home 
 893 59216K 59136K 23628K 18808K com.wandoujia.phoenix2 
 1905 55396K 55300K 23217K 20772K com.tencent.mm 
 2936 61992K 61880K 21637K 18444K com.moco.mcplayer:pictureShow 
 21282 53380K 53204K 20787K 18472K com.tencent.mobileqq 
 709 54480K 54420K 19631K 18040K com.android.settings

 2477 55944K 55880K 18983K 13872K com.wandoujia.player.walkman 
 3060 46808K 46668K 17198K 14796K com.moco.mcplayer 
 18774 51104K 51016K 16956K 13488K com.sina.weibo 
 1757 49840K 49720K 16300K 12908K com.sina.weibo:remote 
 776 46696K 46620K 13768K 11392K com.tencent.mobileqq:MSF 
 1886 43956K 43868K 12386K 10036K com.tencent.mm:push 
 21368 40408K 40264K 9799K 8868K android.process.acore 
 21399 40700K 40620K 9647K 8692K com.google.process.gapps 
 521 41356K 41276K 9603K 8560K com.android.inputmethod.latin 
 1848 41700K 41612K 9433K 8084K com.moco.mcplayer:pushservice 
 540 38476K 38400K 7420K 6436K com.android.phone 
 21311 37664K 37536K 7001K 6056K com.miui.cloudservice 
 569 37656K 37568K 6943K 6036K com.xiaomi.xmsf 
 2673 18100K 18056K 6882K 6172K com.android.commands.monkey 
 1489 37540K 37464K 6792K 5912K com.kingroot.kinguser:service 
 615 37832K 37708K 6381K 5292K com.lbe.security.miui:service 
 2785 37220K 37152K 6224K 5264K com.miui.player 
 134 28840K 11244K 6151K 5816K /system/bin/surfaceflinger 
 750 37504K 37368K 5962K 4872K com.xiaomi.market 
 2819 35816K 35676K 5376K 4304K com.android.mms 
  749 35044K 34956K 5216K 4356K com.baidu.map.location:remote 
  137 11128K 10956K 4699K 4184K /system/bin/mediaserver 
  790 34716K 34576K 4684K 3852K android.process.media 
  700 33596K 33448K 3734K 2768K com.android.calendar 
  133 3836K 3832K 3619K 3616K /system/bin/debuggerd 
  27079 33244K 33100K 3534K 2620K com.android.providers.calendar 
  724 32972K 32828K 3492K 2540K com.baidu.map.location 
  135 39644K 39480K 3416K 1284K zygote 
  554 32368K 32224K 3108K 2176K com.lbe.security.miui 
 1210 31956K 31808K 2965K 2112K com.qualcomm.wiper 
  587 31712K 31568K 2880K 1956K com.duokan.airkan.phone 
  651 31428K 31276K 2646K 1636K com.android.smspush 
  174 1856K 1856K 1250K 1204K /system/bin/rild 
  129 4252K 4252K 1119K 1032K /system/bin/servicemanager 
  143 4228K 4224K 1108K 1036K /system/bin/fmradioserver 
  136 4264K 4264K 1101K 1028K /system/bin/drmserver 
  261 4212K 4208K 1096K 1020K /system/xbin/shelld 
 3411 1308K 1300K 1091K 1088K procrank 
  132 1704K 1700K 877K 836K /system/bin/netd 
  847 1236K 1224K 789K 748K /system/bin/thermald
156 1248K 1236K 774K 732K /system/bin/time_daemon 
  518 1244K 1240K 596K 584K /system/bin/wpa_supplicant 
  179 824K 820K 530K 492K /system/bin/netmgrd 
  139 988K 988K 387K 316K /system/bin/installd 
  176 640K 636K 380K 372K /system/bin/qmuxd 
  130 996K 992K 376K 360K /system/bin/vold 
  145 716K 712K 329K 292K /system/bin/wiperiface 
  158 284K 284K 256K 256K /sbin/adbd 
15991 472K 468K 202K 196K /system/bin/dhcpcd 
    1 276K 276K 198K 156K /init 
 2671 392K 384K 190K 188K /system/bin/sh 
 1534 600K 592K 175K 168K k_worker/31:0 
 1732 600K 592K 175K 168K /system/xbin/ku.sud 
  141 560K 552K 170K 164K /system/bin/keystore 
  138 472K 464K 143K 132K /system/bin/dbus-daemon 
 1269 356K 348K 139K 136K uuids_sys 
   92 176K 176K 134K 92K /sbin/ueventd 
  183 120K 120K 120K 120K /system/xbin/ku.sud 
  148 336K 332K 119K 116K /system/bin/mm-pp-daemon 
  155 3416K 340K 115K 112K /system/bin/rmt_storage 
 2149 312K 304K 95K 92K /data/data/com.moco.mcplayer/files/gdaemon 
  516 288K 280K 78K 76K /system/bin/logwrapper 
                          ------ ------ ------ 
                         692365K 537112K TOTAL 

RAM: 815636K total, 11384K free, 132K buffers, 105608K cached, 1748K shmem, 16708K slab 
// procrank status was 0 
anr traces: 


----- pid 1014 at 2015-06-03 03:36:40 ----- 
Cmd line: com.moco.mcplayer 
DALVIK THREADS: 
(mutexes: tll=0 tsl=0 tscl=0 ghl=0) 
"main" prio=5 tid=1 NATIVE 
  | group="main" sCount=1 dsCount=0 obj=0x410aac40 self=0x145fb90 
  | sysTid=1014 nice=0 sched=0/0 cgrp=default handle=1074267384 
  | schedstat=( 0 0 0 ) utm=22926 stm=3906 core=1 
  at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209) 
  at java