工作筆記 ---- Android客戶端獲取PSS、CPU、啟動時間、流量、電量的Python代碼
阿新 • • 發佈:2018-06-02
int ESS 電量 nbsp tro activit gin 計算 IT
把工作中用到的用來測試、收集Android客戶端性能數據的Python代碼整理了一下,涉及到PSS、CPU、啟動時間、流量、電量,用os.popen()方法執行adb命令並獲取執行結果,用csv文件來存儲數據。得到數據後可以做成可視化的圖標,方便得出測試結論。
獲取CPU的代碼:
1 #/usr/bin/python 2 #encoding:utf-8 3 import csv 4 import os 5 importtime 6 7 #控制類 8 class Controller(object): 9 def __init__(self, count): 10 self.counter = count 11 self.alldata = [("timestamp", "cpustatus")] 12 13 #單次測試過程 14 def testprocess(self): 15 result = os.popen("adb shell dumpsys cpuinfo | grep com.android.browser")16 for line in result.readlines(): 17 cpuvalue = line.split("%")[0] 18 19 currenttime = self.getCurrentTime() 20 self.alldata.append((currenttime, cpuvalue)) 21 22 #多次執行測試過程 23 def run(self): 24 while self.counter >0: 25 self.testprocess()26 self.counter = self.counter - 1 27 time.sleep(3) 28 29 #獲取當前的時間戳 30 def getCurrentTime(self): 31 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 32 return currentTime 33 34 #數據的存儲 35 def SaveDataToCSV(self): 36 csvfile = file(‘cpustatus.csv‘, ‘wb‘) 37 writer = csv.writer(csvfile) 38 writer.writerows(self.alldata) 39 csvfile.close() 40 41 if __name__ == "__main__": 42 controller = Controller(10) 43 controller.run() 44 controller.SaveDataToCSV()
獲取PSS的代碼:
1 #控制類 2 class Controller(object): 3 def __init__(self): 4 #定義收集數據的數組 5 self.alldata = [("id", "vss", "rss")] 6 7 #分析數據 8 def analyzedata(self): 9 content = self.readfile() 10 i = 0 11 for line in content: 12 if "com.android.browser" in line: 13 print line 14 line = "#".join(line.split()) 15 vss = line.split("#")[5].strip("K") 16 rss = line.split("#")[6].strip("K") 17 18 #將獲取到的數據存到數組中 19 self.alldata.append((i, vss, rss)) 20 i = i + 1 21 22 #數據的存儲 23 def SaveDataToCSV(self): 24 csvfile = file(‘meminfo.csv‘, ‘wb‘) 25 writer = csv.writer(csvfile) 26 writer.writerows(self.alldata) 27 csvfile.close() 28 29 #讀取數據文件 30 def readfile(self): 31 mfile = file("meminfo", "r") 32 content = mfile.readlines() 33 mfile.close() 34 return content
獲取啟動時間的代碼:
1 class App(object): 2 def __init__(self): 3 self.content = "" 4 self.startTime = 0 5 6 #啟動App 7 def LaunchApp(self): 8 cmd = ‘adb shell am start -W -n com.android.browser/.BrowserActivity‘ 9 self.content=os.popen(cmd) 10 11 #停止App 12 def StopApp(self): 13 #cmd = ‘adb shell am force-stop com.android.browser‘ 14 cmd = ‘adb shell input keyevent 3‘ 15 os.popen(cmd) 16 17 #獲取啟動時間 18 def GetLaunchedTime(self): 19 for line in self.content.readlines(): 20 if "ThisTime" in line: 21 self.startTime = line.split(":")[1] 22 break 23 return self.startTime 24 25 #控制類 26 class Controller(object): 27 def __init__(self, count): 28 self.app = App() 29 self.counter = count 30 self.alldata = [("timestamp", "elapsedtime")] 31 32 #單次測試過程 33 def testprocess(self): 34 self.app.LaunchApp() 35 time.sleep(5) 36 elpasedtime = self.app.GetLaunchedTime() 37 self.app.StopApp() 38 time.sleep(3) 39 currenttime = self.getCurrentTime() 40 self.alldata.append((currenttime, elpasedtime)) 41 42 #多次執行測試過程 43 def run(self): 44 while self.counter >0: 45 self.testprocess() 46 self.counter = self.counter - 1 47 48 #獲取當前的時間戳 49 def getCurrentTime(self): 50 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 51 return currentTime 52 53 #數據的存儲 54 def SaveDataToCSV(self): 55 csvfile = file(‘startTime2.csv‘, ‘wb‘) 56 writer = csv.writer(csvfile) 57 writer.writerows(self.alldata) 58 csvfile.close()
獲取電量的代碼:
1 #控制類 2 class Controller(object): 3 def __init__(self, count): 4 #定義測試的次數 5 self.counter = count 6 #定義收集數據的數組 7 self.alldata = [("timestamp", "power")] 8 9 #單次測試過程 10 def testprocess(self): 11 #執行獲取電量的命令 12 result = os.popen("adb shell dumpsys battery") 13 #獲取電量的level 14 for line in result: 15 if "level" in line: 16 power = line.split(":")[1] 17 18 #獲取當前時間 19 currenttime = self.getCurrentTime() 20 #將獲取到的數據存到數組中 21 self.alldata.append((currenttime, power)) 22 23 #多次測試過程控制 24 def run(self): 25 #設置手機進入非充電狀態 26 os.popen("adb shell dumpsys battery set status 1") 27 while self.counter >0: 28 self.testprocess() 29 self.counter = self.counter - 1 30 #每5秒鐘采集一次數據 31 time.sleep(5) 32 33 #獲取當前的時間戳 34 def getCurrentTime(self): 35 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 36 return currentTime 37 38 #數據的存儲 39 def SaveDataToCSV(self): 40 csvfile = file(‘meminfo.csv‘, ‘wb‘) 41 writer = csv.writer(csvfile) 42 writer.writerows(self.alldata) 43 csvfile.close()
獲取流量的代碼:
1 #/usr/bin/python 2 #encoding:utf-8 3 import csv 4 import os 5 import string 6 import time 7 8 #控制類 9 class Controller(object): 10 def __init__(self, count): 11 #定義測試的次數 12 self.counter = count 13 #定義收集數據的數組 14 self.alldata = [("timestamp", "traffic")] 15 16 #單次測試過程 17 def testprocess(self): 18 #執行獲取進程的命令 19 result = os.popen("adb shell ps | grep com.android.browser") 20 #獲取進程ID 21 pid = result.readlines()[0].split(" ")[5] 22 23 #獲取進程ID使用的流量 24 traffic = os.popen("adb shell cat /proc/"+pid+"/net/dev") 25 for line in traffic: 26 if "eth0" in line: 27 #將所有空行換成# 28 line = "#".join(line.split()) 29 #按#號拆分,獲取收到和發出的流量 30 receive = line.split("#")[1] 31 transmit = line.split("#")[9] 32 elif "eth1" in line: 33 # 將所有空行換成# 34 line = "#".join(line.split()) 35 # 按#號拆分,獲取收到和發出的流量 36 receive2 = line.split("#")[1] 37 transmit2 = line.split("#")[9] 38 39 #計算所有流量的之和 40 alltraffic = string .atoi(receive) + string .atoi(transmit) + string .atoi(receive2) + string .atoi(transmit2) 41 #按KB計算流量值 42 alltraffic = alltraffic/1024 43 #獲取當前時間 44 currenttime = self.getCurrentTime() 45 #將獲取到的數據存到數組中 46 self.alldata.append((currenttime, alltraffic)) 47 48 #多次測試過程控制 49 def run(self): 50 while self.counter >0: 51 self.testprocess() 52 self.counter = self.counter - 1 53 #每5秒鐘采集一次數據 54 time.sleep(5) 55 56 #獲取當前的時間戳 57 def getCurrentTime(self): 58 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 59 return currentTime 60 61 #數據的存儲 62 def SaveDataToCSV(self): 63 csvfile = file(‘traffic.csv‘, ‘wb‘) 64 writer = csv.writer(csvfile) 65 writer.writerows(self.alldata) 66 csvfile.close() 67 68 if __name__ == "__main__": 69 controller = Controller(5) 70 controller.run() 71 controller.SaveDataToCSV()
工作筆記 ---- Android客戶端獲取PSS、CPU、啟動時間、流量、電量的Python代碼