Android App性能测试之2:启动时间

获取启动时间步骤:

1、设置sdk+python环境

 

2、为了获取App的包(package)名,在cmd输入adb logcat | grep START启动监控指令

 

3、打开App,在cmd查看新增的记录中寻找cmp="com.xxxxx"/.xxxx.xxxActivity,引号里是包名

 

4、输入启动App命令adb shell am start -W -n package/activity,ThisTime的值就是启动时间

 

5、输入停止App命令adb shell am force-stop package(注意:没有activity),这时App退出——冷启动

#5、输入退到后台命令adb shell input keyevent 3,相当于Back或Home键暂停App(退到后台)——热启动

 

6、重复10次,取除了第1个的时间之外的9个值取平均值做图表分析

 

脚本如下:

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

 

posted @ 2017-04-26 23:31  _HLP_huang  阅读(308)  评论(0编辑  收藏  举报