話不多說(shuō),直接上腳本:
#/usr/bin/python #encoding:utf-8 import csv import os import time class App(object): def __init__(self): self.content = "" self.startTime = 0 #啟動(dòng)App def LaunchApp(self): print("啟動(dòng)程序.....") cmd = 'adb shell am start -W -n com.android.browser/.BrowserActivity' self.content=os.popen(cmd) #停止App def StopApp(self): #冷啟動(dòng) #cmd = 'adb shell am force-stop com.android.browser' #熱啟動(dòng) cmd = 'adb shell input keyevent 3' print("關(guān)閉程序......") os.popen(cmd) #獲取啟動(dòng)時(shí)間 def GetLaunchedTime(self): for line in self.content.readlines(): if "ThisTime" in line: self.startTime = line.split(":")[1].strip() break return self.startTime #控制類(lèi) pasedtime:?jiǎn)?dòng)時(shí)間 class Controller(object): def __init__(self, count): self.app = App() self.counter = count self.alldata = [("currenttime", "pasedtime")] #單次測(cè)試過(guò)程 def testprocess(self): self.app.LaunchApp() time.sleep(5) elpasedtime = self.app.GetLaunchedTime() self.app.StopApp() time.sleep(3) currenttime = self.getCurrentTime() self.alldata.append((currenttime, elpasedtime)) #多次執(zhí)行測(cè)試過(guò)程 def run(self): while self.counter >0: self.testprocess() self.counter = self.counter - 1 #獲取當(dāng)前的時(shí)間戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #數(shù)據(jù)的存儲(chǔ) def SaveDataToCSV(self): csvfile = open('starttime.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(2) controller.run() controller.SaveDataToCSV() 2.內(nèi)存: #/usr/bin/python #encoding:utf-8 import csv import os import time """ 請(qǐng)先用命令行運(yùn)行 adb shell top -d num--->(數(shù)字是收取數(shù)據(jù)間隔時(shí)間) 收取數(shù)據(jù),并且另存為meminfo """ #控制類(lèi) class Controller(object): def __init__(self): #定義收集數(shù)據(jù)的數(shù)組 vss:虛擬耗用內(nèi)存(包含共享庫(kù)占用的內(nèi)存) rss:實(shí)際使用物理內(nèi)存(包含共享庫(kù)占用的內(nèi)存) self.alldata = [("id", "vss", "rss")] #分析數(shù)據(jù) def analyzedata(self): content = self.readfile() i = 0 for line in content: if "com.android.browser" in line: print (line) line = "#".join(line.split()) vss = line.split("#")[5].strip("K") rss = line.split("#")[6].strip("K") #將獲取到的數(shù)據(jù)存到數(shù)組中 self.alldata.append((i, vss, rss)) i = i + 1 #數(shù)據(jù)的存儲(chǔ) def SaveDataToCSV(self): csvfile = open('meminfo.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() #讀取數(shù)據(jù)文件 def readfile(self): mfile = open("meminfo", "r") content = mfile.readlines() mfile.close() return content if __name__ == "__main__": controller = Controller() controller.analyzedata() controller.SaveDataToCSV() 3.流量 #/usr/bin/python #encoding:utf-8 import csv import os import string import time #控制類(lèi) class Controller(object): def __init__(self, count): #定義測(cè)試的次數(shù) self.counter = count #定義收集數(shù)據(jù)的數(shù)組 traffic:總流量 self.alldata = [("timestamp", "traffic")] #單次測(cè)試過(guò)程 def testprocess(self): #執(zhí)行獲取進(jìn)程的命令 result = os.popen("y_adb shell ps | findstr com.android.browser") #獲取進(jìn)程ID pid = result.readlines()[0].split(" ")[4] #獲取進(jìn)程ID使用的流量 traffic = os.popen("y_adb shell cat /proc/"+pid+"/net/dev") for line in traffic: if "lo" in line: #將所有空行換成# line = "#".join(line.split()) #按#號(hào)拆分,獲取收到和發(fā)出的流量 receive = line.split("#")[1] transmit = line.split("#")[9] elif "eth1" in line: # 將所有空行換成# line = "#".join(line.split()) # 按#號(hào)拆分,獲取收到和發(fā)出的流量 receive2 = line.split("#")[1] transmit2 = line.split("#")[9] #計(jì)算所有流量的之和 alltraffic = string .atoi(receive) + string .atoi(transmit) + string .atoi(receive2) + string .atoi(transmit2) #按KB計(jì)算流量值 alltraffic = alltraffic/1024 #獲取當(dāng)前時(shí)間 currenttime = self.getCurrentTime() #將獲取到的數(shù)據(jù)存到數(shù)組中 self.alldata.append((currenttime, alltraffic)) #多次測(cè)試過(guò)程控制 def run(self): while self.counter >0: self.testprocess() self.counter = self.counter - 1 #每5秒鐘采集一次數(shù)據(jù) time.sleep(5) #獲取當(dāng)前的時(shí)間戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #數(shù)據(jù)的存儲(chǔ) def SaveDataToCSV(self): csvfile = open('traffic.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(2) controller.run() controller.SaveDataToCSV() 4.電量 #/usr/bin/python #encoding:utf-8 import csv import os import time #控制類(lèi) class Controller(object): def __init__(self, count): #定義測(cè)試的次數(shù) self.counter = count #定義收集數(shù)據(jù)的數(shù)組 power:電量,timestamp:時(shí)間 self.alldata = [("timestamp", "power")] #單次測(cè)試過(guò)程 def testprocess(self): #執(zhí)行獲取電量的命令 result = os.popen("adb shell dumpsys battery") #獲取電量的level for line in result: if "level" in line: power = line.split(":")[1].strip() #獲取當(dāng)前時(shí)間 currenttime = self.getCurrentTime() #將獲取到的數(shù)據(jù)存到數(shù)組中 self.alldata.append((currenttime, power)) #多次測(cè)試過(guò)程控制 def run(self): #設(shè)置手機(jī)進(jìn)入非充電狀態(tài) os.popen("adb shell dumpsys battery set status 1") while self.counter >0: self.testprocess() self.counter = self.counter - 1 #每5秒鐘采集一次數(shù)據(jù) time.sleep(5) #獲取當(dāng)前的時(shí)間戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #數(shù)據(jù)的存儲(chǔ) def SaveDataToCSV(self): csvfile = open('dianliang.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(5) controller.run() controller.SaveDataToCSV() 5.cpu #/usr/bin/python #encoding:utf-8 import csv import os import time #控制類(lèi) cpustatus:cpu利用率 class Controller(object): def __init__(self, count): self.counter = count self.alldata = [("timestamp", "cpustatus")] #單次測(cè)試過(guò)程 def testprocess(self): result = os.popen("adb shell dumpsys cpuinfo | findstr com.android.browser") for line in result.readlines(): cpuvalue = line.split("%")[0] currenttime = self.getCurrentTime() self.alldata.append((currenttime,cpuvalue)) print(self.alldata) #多次執(zhí)行測(cè)試過(guò)程 def run(self): while self.counter >0: self.testprocess() self.counter = self.counter - 1 time.sleep(3) #獲取當(dāng)前的時(shí)間戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #數(shù)據(jù)的存儲(chǔ) def SaveDataToCSV(self): csvfile = open('cpcinfo.csv', 'wb') writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(2) controller.run() controller.SaveDataToCSV() |
|
來(lái)自: 小豬窩969 > 《性能測(cè)試》