Android APP 性能测试学习

以下运行环境为windows10系统,python3

1.监控启动时间

adb logcat | findstr START 启动监控 ,启动后启动对应app可以获取包名
冷启动
adb shell am start -W -n (上一条命令获取的包名) 启动app 
adb shell am force-stop 包名 停止app
热启动
adb shell am start -W -n (上一条命令获取的包名) 启动app
adb shell input keyevent 3 将APP退到后台

 1 import os
 2 import time
 3 import csv
 4 
 5 class App(object):
 6     def __init__(self):
 7         self.content = ""
 8         self.startTime = 0
 9 
10     #启动App
11     def LaunchApp(self):
12         cmd = 'adb shell am start -W -n com.android.browser/.BrowserActivity'
13         self.content = os.popen(cmd)
14 
15     #停止app(冷启动)
16     def StopApp(self):
17         cmd = 'adb shell am force-stop com.android.browser'
18         os.popen(cmd)
19 
20     #停止app(热启动)
21     def StopApp2(self):
22         cmd = 'adb shell input keyevent 3'
23         os.popen(cmd)
24 
25     #获取启动时间
26     def GetLaunchedTime(self):
27         for line in self.content.readlines():
28             if "ThisTime" in line:
29                 self.startTime = line.split(":")[1]
30                 break
31         return self.startTime
32 
33 #控制类
34 class Controller(object):
35     def __init__(self,count):
36         self.app = App()
37         self.counter = count
38         self.alldata = [("timestamp","elapsedtime")]
39 
40     #单次测试过程
41     def testprocess(self):
42         self.app.LaunchApp()
43         elapsedtime = self.app.GetLaunchedTime()
44         self.app.StopApp()
45         currenttime = self.getCurrentTime()
46         self.alldata.append((currenttime,elapsedtime))
47 
48     #多次执行测试过程
49     def run(self):
50         while self.counter > 0:
51             self.testprocess()
52             self.counter = self.counter - 1
53 
54     #获取当前时间戳
55     def getCurrentTime(self):
56         currentTime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
57         return currentTime
58 
59     #数据的存储
60     def SaveDataToCSV(self):
61         csvfile = open('startTime.csv','w')
62         writer = csv.writer(csvfile)
63         writer.writerows(self.alldata)
64         csvfile.close()
65 
66 if __name__ == "__main__":
67     controller = Controller(10)
68     controller.run()
69     controller.SaveDataToCSV()
获取启动时间代码

2.CPU利用率数据监控

adb shell dumpsys cpuinfo | findstr packagename

 1 import time
 2 import os
 3 import csv
 4 
 5 class Controller(object):
 6     def __init__(self,count):
 7         self.counter = count
 8         self.alldata = [("timestamp","cpustatus")]
 9 
10     #单次测试过程
11     def testprocess(self):
12         result = os.popen("adb shell dumpsys cpuinfo | findstr com.android.browser")
13         for line in result.readlines():
14             cpuvalue = line.split("%")[0]
15             break
16         currenttime = self.getCurrentTime()
17         self.alldata.append((currenttime,cpuvalue))
18 
19     #多次执行过程
20     def run(self):
21         while self.counter > 0:
22             self.testprocess()
23             self.counter = self.counter - 1
24             time.sleep(5)
25 
26     #获取当前时间戳
27     def getCurrentTime(self):
28         currentTime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
29         return currentTime
30 
31     #数据的存储
32     def SavaDataToCSV(self):
33         csvfile = open('cpustatus.csv','w')
34         writer = csv.writer(csvfile)
35         writer.writerows(self.alldata)
36         csvfile.close()
37 
38 if __name__ == '__main__':
39     controller = Controller(10)
40     controller.run()
41     controller.SavaDataToCSV()
CPU利用率监控代码

3.流量监控

获取进程id

adb shell ps | findstr packagename

获取进程id流量
adb shell cat /proc/pid/net/dev

 1 import csv
 2 import os
 3 import string
 4 import time
 5 
 6 #控制类
 7 class Controller(object):
 8     def __init__(self,count):
 9         #定义测试的次数
10         self.counter = count
11         #定义收集数据的数组
12         self.alldata = [("timestamp","traffic")]
13 
14     #单次测试过程
15     def testprocess(self):
16         #执行获取进程命令
17         result = os.popen("adb shell ps | findstr com.android.browser ")
18         #获取进程id
19         pid = result.readlines()[0].split(" ")[5]
20 
21         #获取进程id使用的流量
22         traffic = os.popen("adb shell cat /proc/"+pid+"/net/dev")
23         receive = 0
24         transmit = 0
25         receive2 = 0
26         transmit2 = 0
27         for line in traffic:
28             if "eth0" in line:
29                 #将所有空行换成#
30                 line = "#".join(line.split())
31                 #按#号拆分,获取收到和发出的流量
32                 receive = line.split("#")[1]
33                 transmit = line.split("#")[9]
34             elif "eth1" in line:
35                 #将所有空行换成#
36                 line = "#".join(line.split())
37                 #按#号拆分,获取收到和发出的流量
38                 receive2 = line.split("#")[1]
39                 transmit2 = line.split("#")[9]
40 
41         #计算所有流量的之和
42         alltraffic = int(receive) + int(transmit) + int(receive2) + int(transmit2)
43         #按KB计算流量值
44         alltraffic = alltraffic/1024
45         #获取当前时间
46         currenttime = self.getCurrentTime()
47         #将获取到的数据存到数组中
48         self.alldata.append((currenttime,alltraffic))
49 
50     #多次测试过程控制
51     def run(self):
52         while self.counter >0:
53             self.testprocess()
54             self.counter = self.counter - 1
55             #每5秒钟采集一次数据
56             time.sleep(5)
57 
58     #获取当前时间戳
59     def getCurrentTime(self):
60         currentTime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
61         return currentTime
62 
63     #数据的存储
64     def SaveDataToCSV(self):
65         csvfile = open('traffic.csv','w')
66         writer = csv.writer(csvfile)
67         writer.writerows(self.alldata)
68         csvfile.close()
69 
70 if __name__ == "__main__":
71     controller = Controller(5)
72     controller.run()
73     controller.SaveDataToCSV()
流量监控代码

4.电量测试

获取电量
adb shell dumpsys battery
切换非充电状态
adb shell dumpsys battery set status 1

 1 import csv
 2 import os
 3 import time
 4 
 5 #控制类
 6 class Controller(object):
 7     def __init__(self,count):
 8         # 定义测试的次数
 9         self.counter = count
10         # 定义收集数据的数组
11         self.alldata = [("timestamp", "power")]
12 
13     def testprocess(self):
14         #执行获取电量的命令
15         result = os.popen("adb shell dumpsys battery")
16         #获取电量的level
17         for line in result:
18             if "level" in line:
19                 power = line.split(":")[1]
20 
21         #获取当前时间
22         currenttime = self.getCurrentTime()
23         #将获取到的数据存到数组中
24         self.alldata.append((currenttime,power))
25 
26     def run(self):
27         #设置手机进入非充电状态
28         os.popen("adb shell dumpsys battery set status 1")
29         while self.counter > 0:
30             self.testprocess()
31             self.counter = self.counter - 1
32             #每5秒钟采集一次数据
33             time.sleep(5)
34 
35     def getCurrentTime(self):
36         currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
37         return currentTime
38 
39     #数据的存储
40     def SaveDataToCSV(self):
41         csvfile = open('power.csv','w')
42         writer = csv.writer(csvfile)
43         writer.writerows(self.alldata)
44         csvfile.close()
45 
46 if __name__ == "__main__":
47     controller = Controller(5)
48     controller.run()
49     controller.SaveDataToCSV()
电量监控代码

5.内存监控

获取内存数据采集(1秒钟刷新1次)
adb shell top -d 1 > meminfo
type meminfo | findstr packagename

 1 import csv
 2 import os
 3 import time
 4 
 5 class Controller(object):
 6     def __init__(self):
 7         self.alldata = [("id", "vss","rss")]
 8 
 9     #分析数据
10     def analyzedata(self):
11         content = self.readfile()
12         i = 0
13         for line in content:
14             if "com.android.browser" in line:
15                 print(line)
16                 line = "#".join(line.split())
17                 vss = line.split("#")[5].strip("K")
18                 rss = line.split("#")[6].strip("K")
19 
20                 #将获取到的数据存到数组中
21                 self.alldata.append((i,vss,rss))
22                 i = i+1
23      #数据的存储
24     def SaveDataToCSV(self):
25         csvfile = open('meminfo.csv','w')
26         writer = csv.writer(csvfile)
27         writer.writerows(self.alldata)
28         csv.close()
29 
30     #读取数据文件
31     def readfile(self):
32         mfile = open("meminfo","r")
33         content = mfile.readlines()
34         mfile.close()
35         return content
36 
37 if __name__ == "__main__":
38     controller = Controller()
39     controller.analyzedata()
40     controller.SaveDataToCSV()
数据分析代码

6.

FPS:每秒的帧数 通过设置-系统-开发人员选项-GPU呈现模式分析进行查看
过渡渲染:描述的是屏幕上的某个像素在同一帧的时间内被绘制了多次
通过设置-系统-开发人员选项-调试GPU过渡绘制

posted @ 2019-09-03 11:16  Maggie2019  阅读(291)  评论(0编辑  收藏  举报