前言:
启动分为冷启动和热启动,简单区分:冷启动即进程发生了重启,热启动进程在后台(即常见的back 出去,home出去)
使用adb命令行查看冷启动热启动时间
1、冷启动
首先手动或使用命令关闭应用运行
adb shell am force-stop package
启动应用
adb shell am start -W -n package/activity
其中“package/activity”即为当前要测试的应用的“报名/activity名称”
查看一个应用的包名和activity名称的方法如下:
adb logcat |grep START
打开logcat输出,点击要获取的应用时,会输出一段日志,取日志中的“cmp”部分即为当前的应用报名+activity名称。
2、热启动
adb shell input keyevent 3
说明一下,上面的命令时模拟点击的3==home键来完成一次退出(完成对应操作含义见:http://www.cnblogs.com/dirt2/p/6928510.html)
推出后再次启动应用即可
adb shell am start -W -n package/activity
命令已经熟悉,需要对命令进行简单的脚本处理,就可以实现一次简单的自动化监控
# /usr/bin/python
# encoding:utf-8
import os
import time
import csv
# 启动app
class App(object):
def _init_(self):
self.content = ""
self.StartTime = 0
def LaunchApp(self):
cmd = 'adb shell am start -W -n com.huajiao.camera/ '
self.content = os.popen(cmd)
# 停止app
def StopApp(self):
#冷启动
cmd = 'adb shell am force-stop com.huajiao.camera'
#热启动
#cmd = 'adb shell inpt 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= 0
self.data = [("timesstart","elapsedtime")]
self.elapsedtime = ""
self.timestart = ""
#单次执行过程
def TestProcess(self):
self.app.LaunchApp()
time.sleep(5)
self.elapsedtime = self.app.GetLaunchedTime()
self.app.StopApp()
time.sleep(3)
self.timestart = self.getCurrentTime()
self.collectAllDate()
#多次执行
def run(self):
while(self.counter >0 ):
self.TestProcess()
#获取当前时间戳
def getCurrentTime(self):
currentTime = time.strftime("%Y-%m-%d %H:%M:%s",time.localtime())
return currentTime
#写入时间
def collectAllDate(self):
self.data.append((self.timestart, self.elapsedtime))
#数据存储
def SaveDateToCSV(self):
csvfile = file('startTime.csv','wb')
writer = csv.writer(csvfile)
writer.writerows(self.data)
csvfile.close()
if __name__ == "__main__":
controller = Controller(10)
controller.run()
controller.SaveDateToCSV()
输出的是一个csv文件,再用三方的数据处理工具处理一下即可,自己用的是number,可以用曲线图更直观的分析数据