獲取app啟動時間


啟動APP並收集消耗時間的命令:

adb shell am  start -W -n package/activity

手動關閉谷歌瀏覽器APP(也可以使用命令關閉adb shell am force-stop 包名),使用啟動命令來自動啟動谷歌APP,見下圖運行結果,可以看到有啟動谷歌瀏覽器APP用時。

 

 

停止APP應用,可以使用命令實現

停止APP應用命令:adb shell am force-stop package

 

測試APP啟動性能,一次測試是不夠的,需要多次測試數據並進行分析做到充分的性能測試才有說服力。但是手動測試多次采集數據太麻煩了,所以我們可以使用自動化腳本幫我們測試和手機多次測試結果的數據。

自動化代碼實現包含備注信息,如下(代碼可以機器運行通過):

獲取app啟動時間自動化代碼實現如下:
#encoding:utf-8
import os
import time
import csv
#定義APP類,用於啟動APP,獲取啟動APP時間和關閉APP
class App(object):
def __init__(self):
self.content=''
self.startTime="0"
#啟動 APPcom.android.chrome/com.google.android.apps.chrome.Main
def LunchApp(self):
cmd='adb shell am start -W -n com.android.chrome/com.google.android.apps.chrome.Main'
self.content=os.popen(cmd)
#停止APP,用於冷啟動的APP
def StopApp(self):
cmd='adb shell am force-stop com.android.chrome'
os.popen(cmd)
#停止APP,用於熱啟動的APP
# def StopApp(self):
# cmd='adb shell input keyevent 3'#觸發手機上的back鍵,實現退出
# os.popen(cmd) #執行cmd

#獲取啟動時間
def GetLunchTime(self):
for line in self.content.readlines():
if "WaitTime" in line:
self.startTime=line.split(":")[1]
break
return self.startTime

#定義運行控制類
class Controller(object):
def __init__(self,counter):
self.app=App()
self.counter=counter
self.allData=[("TimeStamp","elapsTime")]
#單次測試過程
def TestProcess(self):
#調用啟動APP的方法
self.app.LunchApp()
time.sleep(8)
#調用獲取啟動用時方法
elapsTime=self.app.GetLunchTime()
#調用停止APP方法(此處為冷啟動),如果測試熱啟動需要再定義熱啟動的方法
self.app.StopApp()
#time.sleep(5)
#調用獲取當前時間戳
currentTime=self.GetcurrentTime()
#獲取到當前時間戳和啟動app時間追加存儲到allData數組中
self.allData.append((currentTime,elapsTime))
#定義獲取當前的時間戳方法
def GetcurrentTime(self):
currentTime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
return currentTime
def run(self):
while self.counter > 0:
self.TestProcess()
self.counter=self.counter-1

#數據的存儲,將allData數據寫入startTime.csv文件中
def SaveData(self):
csvfile=file('startTime.csv','wb')
write=csv.writer(csvfile)
write.writerows(self.allData)
csvfile.close()

if __name__=="__main__":
#實例化,並設置運行次數
controller=Controller(10)
controller.run()
controller.SaveData()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM