App性能测试之启动时间(安卓)手动+脚本

2023-06-12,,

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

测试策略

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

针对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获取,

    先启动apk
    在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()

说明下:

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

b. adb安装apk

前提:需要把手机给root

命令:adb install 路径名/包名.apk

数据分析:

    在得到数据后,我们一般将第一行数据去掉,取后几次数据分析
    得到数据,我们要先算出平均值,再者就是要画出取线图查看波动情况
    可与竞品对比此数据,做参考用以判断好坏
    版本之间对比数据
    一般冷启动的时间都在3-5s内
    热启动启动的时间比冷启动要少

App性能测试之启动时间(安卓)手动+脚本的相关教程结束。

《App性能测试之启动时间(安卓)手动+脚本.doc》

下载本文的Word格式文档,以方便收藏与打印。