工作笔记 ---- Android客户端获取PSS、CPU、启动时间、流量、电量的Python代码

把工作中用到的用来测试、收集Android客户端性能数据的Python代码整理了一下,涉及到PSS、CPU、启动时间、流量、电量,用os.popen()方法执行adb命令并获取执行结果,用csv文件来存储数据。得到数据后可以做成可视化的图标,方便得出测试结论。

获取CPU的代码:

 1 #/usr/bin/python
 2 #encoding:utf-8
 3 import csv
 4 import os
 5 import time
 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()
 

猜你喜欢

转载自www.cnblogs.com/ailiailan/p/7920410.html