安卓APP啓動時間測試

啓動APP分爲兩種冷啓動和熱啓動
冷啓動指的是進程首次創建 第一次啓動 加載資源的過程
熱啓動指的是 APP啓動後 按HOME退到後臺 再次打開APP的時候

冷啓動測試啓動

啓動APP命令

adb shell am start -W -n package/activity

測試返回數據

ThisTime:本次啓動時間(毫秒)
TotalTime:啓動總時間
WaitTime:等待時間
在這裏插入圖片描述
冷啓動需要關閉APP
停止APP命令

adb shell am force-stop package

熱啓動測試啓動時間

熱啓動開啓APP的命令和冷啓動一樣 不同的是退出時不同
熱啓動時退出APP的命令

adb shell input keyevent 3

那麼只需要把啓動和退出APP的命令操作 變成腳本化 就可獲取 啓動時間了


#/usr/bin/python
#encoding:utf-8
import csv
import os
import time

#app類
class App(object):
    def __init__(self):
        self.content = ""
        self.startTime = 0

    #啓動App
    def LaunchApp(self):
        cmd = 'adb shell am start -W -n package/Activity
        self.content=os.popen(cmd)

    #停止App
    def StopApp(self):
        #cmd = 'adb shell am force-stop package'
        cmd = 'adb shell input keyevent 3'
        os.popen(cmd)

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

#控制類
class Controller(object):
    def __init__(self, count):
        self.app = App()
        self.counter = count
        self.alldata = [("timestamp", "elapsedtime")]

    #單次測試過程
    def testprocess(self):
        self.app.LaunchApp()
        print("1")
        time.sleep(5)
        elpasedtime = self.app.GetLaunchedTime()
        self.app.StopApp()
        time.sleep(3)
        currenttime = self.getCurrentTime()
        self.alldata.append((currenttime, elpasedtime))

    #多次執行測試過程
    def run(self):
        while self.counter >0:
            self.testprocess()
            self.counter = self.counter - 1

    #獲取當前的時間戳
    def getCurrentTime(self):
        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        return currentTime

    #數據的存儲
    def SaveDataToCSV(self):
        csvfile = open('startTime2.csv', mode='w')
        writer = csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

if __name__ == "__main__":
    controller = Controller(10)
    controller.run()
    controller.SaveDataToCSV()


執行完畢後 測試數據就寫入到了Excel文件裏了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章