python adb 安卓app性能测试
2022-09-02 18:30 清风软件测试开发 阅读(464) 评论(0) 编辑 收藏 举报主要是进行cpu、内存、冷启动、热启动、流量、电量的监测 可获取到相关数据,同竞类产品对比,或者同版本对比 cpustatus adb命令:adb shell "dumpsys cpuinfo | grep com.person.buddy" # coding=utf-8#/usr/bin/python #encoding:utf-8 import csv import os import time #控制类 class Controller(object): def __init__(self, count): self.counter = count self.alldata = [("timestamp", "cpustatus")] #单次测试过程 def testprocess(self): cmd = 'adb shell "dumpsys cpu|grep com.person.buddy"' # cmd='adb shell "dumpsys cpuinfo | grep com.person.buddy"' result = os.popen(cmd) for line in result.readlines(): cpuvalue = line.split("%")[0] currenttime = self.getCurrentTime() self.alldata.append((currenttime, cpuvalue)) #多次执行测试过程 def run(self): while self.counter >0: self.testprocess() self.counter = self.counter - 1 time.sleep(3) #获取当前的时间戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #数据的存储 def SaveDataToCSV(self): csvfile = file('cpustatus.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(10) controller.run() controller.SaveDataToCSV() launchtime # coding=utf-8 # /usr/bin/python # encoding:utf-8 import csv import os import time # 同QQ、微信等对比 # 同上下版本进行对比 class App(object): def __init__(self): self.content = "" self.startTime = 0 # 启动App def LaunchApp(self): cmd = 'adb shell am start -W -n com.person.buddy/com.person.buddy.ui.app.LogoActivity' self.content = os.popen(cmd) # 热启动停止App def WarmStopApp(self): cmd = 'adb shell am force-stop com.person.buddy' # cmd = 'adb shell input keyevent 3' os.popen(cmd) # 冷启动停止APP def ColdStopApp(self): cmd = 'adb shell am force -stop com.person.buddy' os.open(cmd) # 获取启动时间 def GetLaunchedTime(self): for line in self.content.readlines(): if "ThisTime" in line: self.startTime = line.split(":")[1] break return self.startTime # 控制类 class Controller(object): def __init__(self, count): self.app = App() self.counter = count self.alldata = [("timestamp", "elapsedtime")] # 单次测试过程 def testprocess(self): self.app.LaunchApp() time.sleep(5) elpasedtime = self.app.GetLaunchedTime() self.app.WarmStopApp() time.sleep(3) currenttime = self.getCurrentTime() self.alldata.append((currenttime, elpasedtime)) # 多次执行测试过程 def run(self): while self.counter > 0: self.testprocess() self.counter = self.counter - 1 # 获取当前的时间戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime # 数据的存储 def SaveDataToCSV(self): csvfile = file('startTime2.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(10) controller.run() controller.SaveDataToCSV() men # coding=utf-8 #/usr/bin/python #encoding:utf-8 import csv import os import time #控制类 class Controller(object): def __init__(self): #定义收集数据的数组 self.alldata = [("id", "vss", "rss")] #分析数据 def analyzedata(self): content = self.readfile() i = 0 for line in content: if "com.person.buddy" in line: print line line = "#".join(line.split()) vss = line.split("#")[5].strip("K") rss = line.split("#")[6].strip("K") #将获取到的数据存到数组中 self.alldata.append((i, vss, rss)) i = i + 1 #数据的存储 def SaveDataToCSV(self): csvfile = file('meminfo.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() #读取数据文件 def readfile(self): mfile = file("meminfo", "r") content = mfile.readlines() mfile.close() return content if __name__ == "__main__": controller = Controller() controller.analyzedata() controller.SaveDataToCSV() power # coding=utf-8 #/usr/bin/python #encoding:utf-8 import csv import os import time #控制类 class Controller(object): def __init__(self, count): #定义测试的次数 self.counter = count #定义收集数据的数组 self.alldata = [("timestamp", "power")] #单次测试过程 def testprocess(self): #执行获取电量的命令 result = os.popen("adb shell dumpsys battery") #获取电量的level for line in result: if "level" in line: power = line.split(":")[1] #获取当前时间 currenttime = self.getCurrentTime() #将获取到的数据存到数组中 self.alldata.append((currenttime, power)) #多次测试过程控制 def run(self): #设置手机进入非充电状态 os.popen("adb shell dumpsys battery set status 1") while self.counter >0: self.testprocess() self.counter = self.counter - 1 #每30秒钟采集一次数据 time.sleep(30) #获取当前的时间戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #数据的存储 def SaveDataToCSV(self): csvfile = file('power.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(5) controller.run() controller.SaveDataToCSV() traffic # coding=utf-8 #/usr/bin/python #encoding:utf-8 import csv import os import string import time #控制类 class Controller(object): def __init__(self, count): #定义测试的次数 self.counter = count #定义收集数据的数组 self.alldata = [("timestamp", "traffic")] #单次测试过程 def testprocess(self): #执行获取进程的命令 cmd = 'adb shell "ps|grep com.person.buddy"' result = os.popen(cmd) #获取进程ID pid = result.readlines()[0].split(" ")[5] #获取进程ID使用的流量 traffic = os.popen("adb shell cat /proc/"+pid+"/net/dev") for line in traffic: if "eth0" in line: #将所有空行换成# line = "#".join(line.split()) #按#号拆分,获取收到和发出的流量 receive = line.split("#")[1] transmit = line.split("#")[9] elif "eth1" in line: # 将所有空行换成# line = "#".join(line.split()) # 按#号拆分,获取收到和发出的流量 receive2 = line.split("#")[1] transmit2 = line.split("#")[9] #计算所有流量的之和 alltraffic = string .atoi(receive) + string .atoi(transmit) + string .atoi(receive2) + string .atoi(transmit2) #按KB计算流量值 alltraffic = alltraffic/1024 #获取当前时间 currenttime = self.getCurrentTime() #将获取到的数据存到数组中 self.alldata.append((currenttime, alltraffic)) #多次测试过程控制 def run(self): while self.counter >0: self.testprocess() self.counter = self.counter - 1 #每5秒钟采集一次数据 time.sleep(5) #获取当前的时间戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #数据的存储 def SaveDataToCSV(self): csvfile = file('traffic.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(5) controller.run() controller.SaveDataToCSV()
https://blog.csdn.net/Asaasa1/article/details/108868054
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
2019-09-02 F12找到页面某一元素所绑定的点击事件
2019-09-02 F12修改html进行本地js操作页面元素
2018-09-02 python三步实现人脸识别
2018-09-02 Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数
2018-09-02 python sys.path[0] 的解释
2018-09-02 Python 之 os.walk()
2018-09-02 Java接口多线程并发测试 (二)