monkey 壓力測(cè)試android
Monkey的特征
- 測(cè)試的對(duì)象僅為應(yīng)用程序包竟稳,有一定的局限性置吓。
- Monkey測(cè)試使用的事件流數(shù)據(jù)流是隨機(jī)的匠题,不能進(jìn)行自定義。
- 可對(duì)MonkeyTest的對(duì)象眶诈,事件數(shù)量涨醋,類型,頻率等進(jìn)行設(shè)置逝撬。
Monkey的基本用法
- 基本語(yǔ)法如下:
- $ adb shell monkey [options]
- 如果不指定options浴骂,Monkey將以無(wú)反饋模式啟動(dòng),并把事件任意發(fā)送到安裝在目標(biāo)環(huán)境中的全部包宪潮。下面是一個(gè)更為典型的命令行示例溯警,它啟動(dòng)指定的應(yīng)用程序,并向其發(fā)送500個(gè)偽隨機(jī)事件:
- $ adb shell monkey -p your.package.name -v 500
分析日志
- 通過(guò)Android trace文件分析死鎖ANR實(shí)例過(guò)程
- system/build.prop 日志文件主要記錄手機(jī)系統(tǒng)信息狡相,如版本梯轻,型號(hào),品牌
- adb logcat 導(dǎo)出日志文件
monkey.ini 配置文件
cmd=adb shell monkey -p com.dgm.user --throttle 500 --ignore-timeouts --ignore-crashes --monitor-native-crashes -v -v
package_name=com.dgm.user
logdir=d:\android
remote_path=d:\android_server
phone_msg_log=d:\android_temp\phone.txt
sum = 100 -
activity = com.dgm.user.SplashActivity
exceptions=['NullPointer','IllegalState','IllegalArgument','ArrayIndexOutOfBounds','RuntimeException','SecurityException']
- throttle 每次事件等待500毫秒
- sum 定義隨機(jī)事件數(shù)
- exceptions 異常定義尽棕,用于后面擴(kuò)展
結(jié)果生成為可視化圖片 使用的是matplotlib
Paste_Image.png
- 當(dāng)然可以看日志文件
代碼分析
- 獲得cpu-men
# -*- coding: utf-8 -*-
import subprocess
pkg_name = "com.dgm.user"
cpu = []
men = []
def top_cpu(pkg_name):
cmd = "adb shell dumpsys cpuinfo | grep " + pkg_name
temp = []
# cmd = "adb shell top -n %s -s cpu | grep %s$" %(str(times), pkg_name)
top_info = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
for info in top_info:
temp.append(info.split()[2].decode()) # bytes轉(zhuǎn)換為string
# print("cpu占用:%s" %cpu)
for i in temp:
if i != "0%":
cpu.append(i.split("%")[0])
return cpu
def get_men(pkg_name):
cmd = "adb shell dumpsys meminfo %s" %(pkg_name)
print(cmd)
temp = []
m = []
men_s = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
for info in men_s:
temp.append(info.split())
# print("內(nèi)存占用:%s" %men[19][1].decode()+"K")
m.append(temp)
for t in m:
men.append(t[19][1].decode())
return men
- 入口代碼
import monkeyConfig
from adb_common import AndroidDebugBridge as ai
import matplotlibBase as mt
import MenCpu as m
import datetime as dt
CPU = [[],[]] # time,使用情況
MEN = [[],[]] #當(dāng)前時(shí)間喳挑,和內(nèi)存使用情況
# 得到手機(jī)信息
def getPhoneMsg(cmd_log):
l_list = []
f = open(cmd_log, "r")
lines = f.readlines()
for line in lines:
line = line.split('=')
#Android 系統(tǒng),如anroid 4.0
if (line[0] == 'ro.build.version.release'):
l_list.append(line[1])
#手機(jī)名字
if (line[0]=='ro.product.model'):
l_list.append(line[1])
#手機(jī)品牌
if (line[0]=='ro.product.brand'):
l_list.append(line[1])
f.close()
return l_list
#開(kāi)始腳本測(cè)試
def start_monkey(cmd, logdir, now1, logcatname):
print(cmd)
os.popen(cmd)
# os.kill()
#print"使用Logcat導(dǎo)出日志"
cmd2 = "adb logcat -d >%s" % logcatname
os.popen(cmd2)
#print"導(dǎo)出traces文件"
tracesname = logdir + "\\" + now1 + r"traces.log"
cmd3 = "adb shell cat /data/anr/traces.txt>%s" % tracesname
os.popen(cmd3)
if __name__ == '__main__':
ini_file = 'monkey.ini'
if os.path.isfile(ini_file):
if ai().attached_devices():
mc = monkeyConfig.baseReadnin(ini_file)
ai().open_app(mc.get_package_name(), mc.get_activity())
os.system('adb shell cat /system/build.prop >'+mc.get_phone_msg_log()) #存放的手機(jī)信息
ll_list = getPhoneMsg(mc.get_phone_msg_log())
# monkey開(kāi)始測(cè)試
sum = mc.get_sum()
temp = ""
monkeylog = ""
start_monkey(mc.get_cmd(), mc.get_logdir(), mc.get_now(), mc.get_logcatname())
for i in range(sum):
time.sleep(1)
print(i)
dn = dt.datetime.now()
CPU[0].append(dn)
m.top_cpu(mc.get_package_name())
MEN[0].append(dn)
m.get_men(mc.get_package_name())
monkeylog = open(mc.get_logdir() + "\\" + mc.get_now()+"monkey.log")
temp = monkeylog.read()
monkeylog.close()
if temp.count('Monkey finished')>0:
print("測(cè)試完成咯")
CPU[1].append(m.cpu)
MEN[1].append(m.men)
# geterror(ll_list, mc.get_log(), mc.get_remote_path(), mc.now) 錯(cuò)誤顯示
mt.cpu_men_plots(CPU, MEN)
break
else:
print("設(shè)備不存在")
else:
print(u"配置文件不存在"+ini_file)
- 結(jié)果以曲線圖展示
def cpu_men_plots(cpu, men):
import matplotlib.pyplot as pl
import matplotlib.dates as mdates
import datetime
# 處理異常數(shù)據(jù)滔悉,有時(shí)候得到數(shù)據(jù)(占用情況)會(huì)比時(shí)間多一次循環(huán)的數(shù)據(jù)蟀悦,造成xy的數(shù)據(jù)不一致,而引起報(bào)錯(cuò)
if len(cpu[0]) != len(cpu[1][0]):
cpu[1][0]= cpu[1][0][0:len(cpu[0])]
if len(men[0]) != len(men[1][0]):
men[1][0]= men[1][0][0:len(men[0])]
print(men[0])
print(men[1][0])
a1 = pl.subplot(311)
a1.set_title("CPU")
a1.set_ylabel("占用情況%")
a1.plot(cpu[0], cpu[1][0])
a1.xaxis.set_major_locator(mdates.SecondLocator(interval=1))
a1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
a2 = pl.subplot(312)
a2.set_title("內(nèi)存")
a2.set_ylabel("使用情況 K")
a2.plot(men[0], men[1][0])
a2.xaxis.set_major_locator(mdates.SecondLocator(interval=2))
a2.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
# a3 = pl.subplot(313)
# a3.set_title("流量")
# a3.set_ylabel("使用情況 K")
# a3.plot(x,list2)
# a3.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
# a1.margins(x=0.2)
pl.tight_layout()
pl.show()