1. Monkey的使用
Monkey是在模拟器或设备环境中运行,因此必须通过shell来启动,可在每个命令前加adb shell或这是进入shell后然后再使用monkey命令。
1.1 Monkey的典型使用
adb shell monkey -p your.package.name -v 500
以上命令参数说明:
1)-p 参数是指定特定包名的APP。
2) -v 命令行中日志输出级别,可以写多个,级别 0(默认值)只提供启动通知、测试完成和最终结果。 级别 1 提供有关测试在运行时(例如发送到您的 Activity 的各个事件)的更多详细信息。 级别 2 提供更详细的设置信息,例如已选择或未选择用于测试的 Activity。
3)500 这个参数是指定它会向指定启动的APP发送500个伪随机事件(例如点击、轻触或手势)
1.2 Mokey –throttle 参数说明
adb shell monkey -p your.package.name -v --throttle 500 1000
在典型的使用中新增–throttle这个参数,作用是增加事件之间的延时时间,时间单位是毫秒。也就是在加上这个参数后会在执行完一次伪事件后等待指定的时间后才会执行下一个伪事件。前面500是延时时间后面1000是执行次数
1.3 其它参数使用说明
1.4 Monkey日志输出
1.4.1 日志保存到电脑指定文件
adb shell monkey [options] <event-count> > /Users/test/Desktop/a.txt
以上命令是降monkey测试过程中日志输出到指定文件中,供后续分析日志使用。
1.4.2 日志保存到手机指定文件
adb shell monkey [options] <event-count> /mnt/sdcard/monkeylog.txt
1.4.3 日志标准流与错误流分开保存
# monkey [options] <event-count> 1>/mnt/sdcard/monkeylog.txt 2>/mnt/sdcard/monkeyErrorlog.txt
1.5 Monkey日志内容分析
1.5.1 搜索关键字“ANR” 查找ANR的相关日志
通过搜索这个关键字可以查找到哪里有无响应的情况。
1.5.2 搜索关键字“CRASH” 查找Crash的相关日志
通过搜索这个关键字可以查找到哪里有App崩溃的情况。
1.6 停止Monkey
在monkey运行中,断开USB连接,mokey仍可继续运行
1. ps命令查找进程
adb shell ps | grep monkey 返回来的第一个数字,即是monkey的进程号
2.kill 命令结束进程
adb shell kill [刚才查到的进程号]
2. 利用python和shell脚本来获取性能数据
# /usr/bin/python
# encoding:utf-8
import csv
import os
import time
# 监控CPU资源信息
class MonitoringCPUResources(object):
def __init__(self, seconds):
#这里根据传入的时间计算需要执行的获取数据的次数,传入时间的单位为秒
self.counter = seconds / 5
#添加表格头
self.alldata = [("timestamp","pid","totalcpu", "cpustatus","meminfo")]
# 单次执行监控过程
def monitoring(self):
#获取设备整体cpu使用率
totalcpu = os.popen("adb shell dumpsys cpuinfo | grep TOTAL")
totalcpuvalue = totalcpu.readline().split("%")[0].strip()
#获取单个APP的cpu使用率
cpuinfo = os.popen("adb shell dumpsys cpuinfo | grep com.test.app")
cpuvalue = cpuinfo.readline().split("%")[0].strip()
#获取单个APP的内存使用情况
meminfo = os.popen("adb shell dumpsys meminfo | grep com.test.app")
memvalue = meminfo.readline().split("K:")[0].strip()
#获取单个APP的pid,在单个APP每一次打开pid都会改变
pidinfo = os.popen("adb shell ps -ef | grep 'com.test.app' | grep -v grep | awk '{print $2}'")
pidvalue = pidinfo.readlines()[0].rstrip('\n')
#将获取到的数据打印
currenttime = self.getCurrentTime()
print("pid is: " + pidvalue)
print("current time is:"+currenttime)
print("totalcpu used is " + totalcpuvalue)
print("cpu used is:" + cpuvalue)
print("mem used is:" + memvalue)
#将获取到的数据填充到性能统计表数据中,但此时并未写文件
self.alldata.append([currenttime,pidvalue,totalcpuvalue,cpuvalue, memvalue])
# 多次执行监控过程
def run(self):
#这里是一直重复执行获取数据的操作
while self.counter > 0:
self.monitoring()
self.counter = self.counter - 1
#将这个延迟注释掉是因为本身获取设备或APP数据是需要时间的,测试得到这个时间大概就是5秒,所有将这个延迟去除了
# time.sleep(5)
# 获取当前的时间戳
def getCurrentTime(self):
currentTime = time.strftime("%H:%M:%S", time.localtime())
return currentTime
# 数据的存储
def SaveDataToCSV(self):
csvfile = open('cpustatus03161339.csv', 'w',0)
writer = csv.writer(csvfile)
writer.writerows(self.alldata)
csvfile.close()
if __name__ == "__main__":
monitoringCPUResources = MonitoringCPUResources(7200)
monitoringCPUResources.run()
monitoringCPUResources.SaveDataToCSV()