码迷,mamicode.com
首页 > 移动开发 > 详细

App性能测试之启动时间(安卓)

时间:2018-06-28 16:46:26      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:sel   pack   timestamp   lse   self   let   分享图片   data   tst   

这个测试可以使用adb工具,adb的安装方式

测试策略

  1. 安装后首次启动
  2. 常规冷启动
  3. 热启动(一般这个都很少测试)

    针对1和2的测试方法

    步骤1:在cmd中输入如下命令

    adb logcat * > D:\log.txt

    步骤2:再在手机上打开将要测试的apk,apk启动完成后,cmd上按ctrl+c结束导入日志
    步骤3:在D盘找到log.txt,ctrl+f,输入Displayed(搜索Displayed单词),Displayed后面有显示时间,有些app有多个Displayed,启动时间为所有Displayed时间的总和
    技术分享图片
    技术分享图片
    此处的启动时间为609+738 (ms)

    还有一个方法,可以获取到1,2,3的时间,如下

    步骤一:获取测试apk的包名(可以问开发要),可以通过adb获取,
    1) 先启动apk
    2) 在cmd中输入命令:

    adb shell dumpsys window w | findstr \/ | findstr name = 

    得到如下,其中name后面的则是需要的包名/组件

    mSurface=Surface(name=com.sina.weibo/com.sina.weibo.VisitorMainTabActivity)

    步骤二: 在cmd输入启动apk的命令,里面则有启动时间

    adb shell  am start -W -n com.sina.weibo/.VisitorMainTabActivity

    输出如下,其中,ThisTime则为启动的总时间

    Status: ok
    Activity: com.sina.weibo/.VisitorMainTabActivity
    ThisTime: 1326
    TotalTime: 1326
    WaitTime: 1354
    Complete

毕竟手每次点要等待特别麻烦,那么就用python脚本来实现获得每次的启动时间吧。代码如下

#!/user/bin/python
# _*_ coding: utf-8 _*_
import csv
import re
import os
import time

Package_activity="com.sina.weibo/.VisitorMainTabActivity"
Packagename = "com.sina.weibo"
runnum = 10
class App():
    def __init__(self):
        self.content = ""
        self.startTime = 0
        self.flag = 1
    def LauchApp(self):
        cmd = "adb shell am start -W -n " + Package_activity
        self.content = os.popen(cmd)
    def StopApp(self):
        if self.flag == 1:
            cmd = "adb shell am force-stop " + Packagename
        else:
            cmd = "adb shell input keyevent 3"
        os.popen(cmd)

    def GetLaunchedTime(self):
        for line in self.content.readlines():
            if "ThisTime" in line:
                self.startTime = line.split(":")[1]
                break
        print (self.startTime)
        return self.startTime

class Controller(object):
    def __init__(self):
        self.app = App()
        self.counter = 0
        self.laughtime = [("timestamp","elapsedtime")]

    def testprocess(self):
        self.app.LauchApp()
        elpasedtime = self.app.GetLaunchedTime()
        time.sleep(3)
        self.app.StopApp()
        currenttime = self.getCurrentTime()
        self.laughtime.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):
        if self.app.flag == 1:
            csvfile = file("coldstart_Time.csv","wb")
        else:
            csvfile = file("hotstart_Time.csv", "wb")
        writer = csv.writer(csvfile)
        writer.writerows(self.laughtime)
        csvfile.close()

def coldLaugh_run():
    controller = Controller()
    controller.counter = runnum
    controller.app.flag = 1
    controller.run()
    controller.SaveDataToCSV()

def hotLaugh_run():
    controller = Controller()
    controller.counter = runnum
    controller.app.flag = 0
    controller.run()
    controller.SaveDataToCSV()

if __name__ == "__main__":
    coldLaugh_run()
    hotLaugh_run()

说明下:
其中def SaveDataToCSV(self)这个函数中的file("hotstart_Time.csv", "wb"),python3将file改为open

App性能测试之启动时间(安卓)

标签:sel   pack   timestamp   lse   self   let   分享图片   data   tst   

原文地址:https://www.cnblogs.com/testdora/p/9239280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!