以下运行环境为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()