- # coding: utf-8
- import os,csv,time
- class App(object):
- def __init__(self):
- self.content=
- self.startTime=0
- #启动 APP
- def launchApp(self):
- cmd=adb shell am start -W -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity
- self.content=os.popen(cmd)
- #停止 APP
- def StopApp(self):
- cmd=adb shell am force-stop com.android.chrome
- # cmd=adb shell keyevent 3
- os.popen(cmd)
- def GetLaunchedTime(self):
- #循环带有 ThisTime 这一行
- for line in self.content.readlines():
- if ThisTimein line:
- self.startTime=line.split(:)[1]
- break
- return self.startTime
- class Controller(object):
- def __init__(self,count):
- self.app=App()
- self.counter=count
- # timestamp: 这次是从几分几秒开始的
- # elapsedtime 耗时
- self.alldata = [("timestamp", "elapsedtime")]
- #单次测试过程
- def testprocess(self):
- self.app.launchApp()
- time.sleep(3)
- elpasedtime =self.app.GetLaunchedTime()
- self.app.StopApp()
- time.sleep(5)
- 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=file(startTime.csv, wb)
- write=csv.writer(csvfile)
- write.writerows(self.alldata) #write.writerow 是向一行写
- csvfile.close()
- if __name__ == __main__:
- controller=Controller(5) #执行次数
- controller.run()
- controller.SaveDataToCSV()
启动时间
第一步: 创建两个类
- APP class: #干活的类
- LaunchApp
- StopApp
- GetlaunchTime
- Controller class:# 调用的类
- run
- collectAllData
- SavaDataToCSV
- -----------------------------------------
第二步:
def launchApp(self): 启动 APP 方法
cmd=adb shell am start -W -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity
self.content=os.popen(cmd) 执行 shell 的模块命令, 获取返回值 content
def StopApp(self): 停止 APP 方法
- cmd=adb shell am force-stop com.android.chrome
- os.popen(cmd)
第三步: 因为 GetlaunchTime 方法需要用到 launchApp(self)的 content 所以需要初始化一个变量
- class App():
- def __init__(self):
- self.content=
第四步:
def GetLaunchedTime(self):
for line in self.content.readlines(): content 内容中循环
if ThisTimein line: 如果这一行带有 ThisTime
self.startTime=line.split(:)[1]则把这一行以: 切分, 赋值给 startTime
break 跳出
return self.startTime 返回 ThisTime 后的时间
第五步: 初始化 self.startTime=0 以待其他方法使用
第六步: Controller()类操作
- 6.1:
- class Controller():
- def __init__(self,count):
- self.app=App() #实例化后就可以在本类中使用其他类 ( app) 的方法了
- self.counter=count #设置运行的次数
- 6.2:
- #单次测试过程
- def testprocess(self):
- self.app.launchApp()
- self.app.GetLaunchedTime()
- self.app.StopApp()
6.3: 多次执行的测试过程
- def run(self):
- while self.counter>0:
- self.testprocess()
- self.counter=self.counter-1
启动时间备注
来源: http://www.bubuko.com/infodetail-2499336.html