主要内容
热启动
adb shell am start -W org.cnodejs.android.md/.ui.activity.LaunchActivity #打开应用
adb shell input keyevent 3 #Home
冷启动
adb shell am start -W org.cnodejs.android.md/.ui.activity.LaunchActivity
adb shell am force-stop org.cnodejs.android.md
基础功能实现
import subprocess
import time
import csv
def getstartapp_time():
cmd = 'adb shell am start -W -n org.cnodejs.android.md/.ui.activity.LaunchActivity'
p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
thistime = 0
for line in p.stdout.readlines():
line = line.decode('utf8')
if "ThisTime" in line:
newline = line.split(':')
thistime = newline[1]
thistime = thistime.strip()
return thistime
def force_stopapp():
cmd = 'adb shell am force-stop org.cnodejs.android.md'
subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
def run():
with open('thistime.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['title','time'])
for x in range(20):
thistime = getstartapp_time()
with open('thistime.csv', 'a', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(["thistime",thistime])
time.sleep(3)
force_stopapp()
time.sleep(1)
run()
Python脚本
import subprocess
import csv
import time
class App(object):
def __init__(self):
self.startTime = 0
self.content=b''
#启动App
def LaunchApp(self):
cmd = 'adb shell am start -W com.android.browser/.BrowserActivity'
self.content=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#Home
def BackAppHome(self):
cmd = 'adb shell input keyevent 3'
subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
def ForeceStopApp(self):
cmd = 'adb shell am force-stop com.android.browser'
subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#获取冷启动时间
def GetLaunchedTime(self):
for line in self.content.stdout.readlines():
line = line.decode('utf-8')
if "ThisTime" in line:
self.startTime = line.split(":")[1]
self.startTime =self.startTime.strip()
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()
time.sleep(5)
elpasedtime = self.app.GetLaunchedTime()
self.app.ForeceStopApp()
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', 'w', newline='')
writer = csv.writer(csvfile)
# self.alldata.decode('utf-8')
for line in self.alldata:
writer.writerow(line)
csvfile.close()
if __name__ == "__main__":
# 设置运行次数
controller = Controller(10)
controller.run()
controller.SaveDataToCSV()