zoukankan      html  css  js  c++  java
  • 获取app启动时间

    启动APP并收集消耗时间的命令:

    adb shell am  start -W -n package/activity

    手动关闭谷歌浏览器APP(也可以使用命令关闭adb shell am force-stop 包名),使用启动命令来自动启动谷歌APP,见下图运行结果,可以看到有启动谷歌浏览器APP用时。

     

    停止APP应用,可以使用命令实现

    停止APP应用命令:adb shell am force-stop package

    测试APP启动性能,一次测试是不够的,需要多次测试数据并进行分析做到充分的性能测试才有说服力。但是手动测试多次采集数据太麻烦了,所以我们可以使用自动化脚本帮我们测试和手机多次测试结果的数据。

    自动化代码实现包含备注信息,如下(代码可以机器运行通过):

    获取app启动时间自动化代码实现如下:
    #encoding:utf-8
    import os
    import time
    import csv
    #定义APP类,用于启动APP,获取启动APP时间和关闭APP
    class App(object):
    def __init__(self):
    self.content=''
    self.startTime="0"
    #启动 APPcom.android.chrome/com.google.android.apps.chrome.Main
    def LunchApp(self):
    cmd='adb shell am start -W -n com.android.chrome/com.google.android.apps.chrome.Main'
    self.content=os.popen(cmd)
    #停止APP,用于冷启动的APP
    def StopApp(self):
    cmd='adb shell am force-stop com.android.chrome'
    os.popen(cmd)
    #停止APP,用于热启动的APP
    # def StopApp(self):
    # cmd='adb shell input keyevent 3'#触发手机上的back键,实现退出
    # os.popen(cmd) #执行cmd

    #获取启动时间
    def GetLunchTime(self):
    for line in self.content.readlines():
    if "WaitTime" in line:
    self.startTime=line.split(":")[1]
    break
    return self.startTime

    #定义运行控制类
    class Controller(object):
    def __init__(self,counter):
    self.app=App()
    self.counter=counter
    self.allData=[("TimeStamp","elapsTime")]
    #单次测试过程
    def TestProcess(self):
    #调用启动APP的方法
    self.app.LunchApp()
    time.sleep(8)
    #调用获取启动用时方法
    elapsTime=self.app.GetLunchTime()
    #调用停止APP方法(此处为冷启动),如果测试热启动需要再定义热启动的方法
    self.app.StopApp()
    #time.sleep(5)
    #调用获取当前时间戳
    currentTime=self.GetcurrentTime()
    #获取到当前时间戳和启动app时间追加存储到allData数组中
    self.allData.append((currentTime,elapsTime))
    #定义获取当前的时间戳方法
    def GetcurrentTime(self):
    currentTime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    return currentTime
    def run(self):
    while self.counter > 0:
    self.TestProcess()
    self.counter=self.counter-1

    #数据的存储,将allData数据写入startTime.csv文件中
    def SaveData(self):
    csvfile=file('startTime.csv','wb')
    write=csv.writer(csvfile)
    write.writerows(self.allData)
    csvfile.close()

    if __name__=="__main__":
    #实例化,并设置运行次数
    controller=Controller(10)
    controller.run()
    controller.SaveData()
  • 相关阅读:
    Sublime Text 最佳插件列表(转)
    MyBatis入门(六)---mybatis与spring的整合
    MyBatis入门(五)---延时加载、缓存
    MyBatis入门(三)---多个参数
    MyBatis入门(二)---一对一,一对多
    AccessRandomFile多线程下载文件
    MyBatis入门(一)---基本使用
    JAVA基础学习day27--反射机制
    简明 Vim 练级攻略(转)
    JAVA基础学习day26--正则表达式
  • 原文地址:https://www.cnblogs.com/Ladylittleleaf/p/9545686.html
Copyright © 2011-2022 走看看