註釋挺詳細了,直接上全部代碼,歡迎各位大佬批評指正。
# encoding:utf-8
import csv
import os
import time
class App(object):
def __init__(self, package, acticity):
self.content = ""
self.startTime = 0
self.ThisTime = 0
self.TotalTime = 0
self.WaitTime = 0
self.result = []
self.package = package.strip()
self.activity = acticity.strip()
# 啓動App
def LaunchApp(self):
cmd = 'adb shell am start -W -n {}/{}'.format(self.package,self.activity)
self.content = os.popen(cmd)
# 熱啓動停止App
def StopHotApp(self):
cmd = 'adb shell input keyevent 3'
os.popen(cmd)
# 冷啓動停止App
def StopApp(self):
cmd = 'adb shell am force-stop {}'.format(self.package)
os.popen(cmd)
# 獲取啓動時間
def GetLaunchedTime(self):
# print( self.content.readlines())
lines = self.content.readlines()
for line in lines:
if "ThisTime" in line:
self.ThisTime = line.split(":")[1].strip()
elif "TotalTime" in line:
self.TotalTime = line.split(":")[1].strip()
elif "WaitTime" in line:
self.WaitTime = line.split(":")[1].strip()
elif "not started" in line:
break
return self.ThisTime,self.TotalTime,self.WaitTime
# 控制類
class Controller(object):
def __init__(self, count, isHot, package,acticity):
self.app = App(package,acticity)
self.counter = count
self.isHot = isHot
self.alldata = [("timestamp", "ThisTime","TotalTime","WaitTime")]
self.sum_ThisTime = 0
self.sum_TotalTime = 0
self.sum_WaitTime = 0
self.junzhi = "均值"
self.junzhidata = [(self.junzhi, self.sum_ThisTime,self.sum_TotalTime,self.sum_WaitTime)]
# 單次測試過程
def testprocess(self):
self.app.LaunchApp()
# time.sleep(3)
ThisTime, TotalTime, WaitTime = self.app.GetLaunchedTime()
print(ThisTime, TotalTime, WaitTime)
time.sleep(3)
if self.isHot == 'y' or self.isHot == 'Y':
self.app.StopHotApp()
elif self.isHot == 'n' or self.isHot == 'N':
self.app.StopApp()
# time.sleep(3)
currenttime = self.getCurrentTime()
if TotalTime!=0 and ThisTime!=0:
self.alldata.append((currenttime, ThisTime, TotalTime, WaitTime))
self.sum_ThisTime = self.sum_ThisTime + int(ThisTime)
self.sum_TotalTime = self.sum_TotalTime + int(TotalTime)
self.sum_WaitTime = self.sum_WaitTime + int(WaitTime)
return self.sum_ThisTime,self.sum_TotalTime,self.sum_WaitTime
# 多次執行測試過程
def run(self):
c = self.counter
while self.counter > 0:
self.sum_ThisTime, self.sum_TotalTime, self.sum_WaitTime = self.testprocess()
# print(self.sum_ThisTime, self.sum_TotalTime, self.sum_WaitTime)
if self.sum_ThisTime!=0 and self.sum_TotalTime!=0:
self.counter = self.counter - 1
self.junzhidata = [(self.junzhi, int(self.sum_ThisTime) / c, int(self.sum_TotalTime) / c, int(self.sum_WaitTime) / c)]
# 獲取當前的時間戳
def getCurrentTime(self):
currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return currentTime
# 數據的存儲
def SaveDataToCSV(self):
str(int(time.time()))
csvfile = open(f'{self.isHot}{package.split(".")[1]}{str(int(time.time()))}.csv', 'a+', encoding='utf_8_sig', newline='')
writer = csv.writer(csvfile)
writer.writerows(self.alldata)
writer.writerows(self.junzhidata)
csvfile.close()
with open(f'{self.isHot}{package.split(".")[1]}{str(int(time.time()))}.txt', 'a+', encoding='utf_8_sig') as f:
for one in self.alldata:
f.write(str(one)[1:-1].replace(',',' ') + '\n')
for o in self.junzhidata:
f.write(str(o)[1:-1].replace(',', ' ') + '\n')
if __name__ == "__main__":
isHot = input("熱啓動時間請輸入y後按下回車鍵,冷啓動請輸入n後按下回車鍵:").strip()
lines = input("請輸入要執行的包名和主Acticity名,輸入完畢後按下回車鍵。包名和主Acticity名之間輸入英文逗號(,),不同App之間輸入英文分號(;)最後以;結尾:").strip()
apps = lines.split(';')
for one in apps:
if one:
package, acticity = one.split(',')
if package and acticity:
if isHot == 'y' or isHot == 'Y':
cmd = 'adb shell am start -W -n {}/{}'.format(package, acticity)
os.popen(cmd) # 使App處於後臺運行,保證測試的第一次是熱啓動
cmd = 'adb shell input keyevent 3'
os.popen(cmd)
elif isHot == 'n' or isHot == 'N':
cmd = 'adb shell am start -W -n {}/{}'.format(package, acticity)
os.popen(cmd) # 使App處於後臺運行,保證測試的第一次是冷啓動
cmd = 'adb shell am force-stop {}'.format(package)
os.popen(cmd)
controller = Controller(3, isHot, package, acticity)
# controller.testprocess()
controller.run()
controller.SaveDataToCSV()