最近在研究Android APP性能测试。所以发现一些有趣的东西,在这里进行分享。我们先讲第一个内容,如何获取APP冷/热启动时间?为什么要做这个测试,道理其实很简单,如果启动APP特别耗时的话,用户反馈百分之99不好。所以在这里我们可以获取APP冷/热启动时间,同竞品进行比较。

环境准备(可参考我写的monkey测试)

  • adb
  • 手机/模拟器
  • cmder
  • python2

获取APK包名及主活动名

adb logcat | grep START //监控指令

具体步骤:

1、cmder下输入  adb logcat | grep START

2、点击想监控的APP,比如这里我点击的是手机自带浏览器,然后会生成一些log,我们找到cmp,如下 com.android.browser 是我们要找的包名,.BrowserActivity 是我们找的主活动名

Windows下获取APP 冷/热启动时间

冷启动

adb shell am start -W -n com.android.browser/.BrowserActivity  

冷启动停止APP

adb shell am force-stop com.android.browser

热启动

adb shell am start -W -n com.android.browser/.BrowserActivity

热启动停止APP

adb shell input keyevent 3

python脚本实现APP 冷/热启动时间

思路:

1. 创建一个APP类,进行APP相关操作,其中包含,冷/热启动APP,冷/热关闭APP,获取冷/热启动时间

2. 创建一个Controller类,主要实现多次启动/关闭APP,获取时间戳,数据的存储

# /usr/bin/python
# encoding:utf-8
import csv
import os
import time class App(object):
def __init__(self):
self.content = ""
self.startTime = 0 # 启动App
def LaunchApp(self):
cmd = 'adb shell am start -W -n com.begoit.studyplan/.ui.act.SplashActivity'
self.content = os.popen(cmd) # 停止App
def StopApp(self):
# cmd = 'adb shell am force-stop com.android.browser'
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
return self.startTime # 控制类
class Controller(object):
def __init__(self, count):
self.app = App()
self.counter = count
self.alldata = [("timestamp", "elapsedtime")] # 单次测试过程
def testprocess(self):
self.app.LaunchApp()
time.sleep(5)
elpasedtime = self.app.GetLaunchedTime()
self.app.StopApp()
time.sleep(3)
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('startTime2.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerows(self.alldata)
csvfile.close() if __name__ == "__main__":
controller = Controller(5)
controller.run()
controller.SaveDataToCSV()

运行结果展示:

总结:

我们通过两种方式实现记录APP冷/热启动时间,进行比较,编写脚本方式相对简单些。也更容易对测试结果进行分析。所以在这里推荐大家学习python基础知识。关于adb shell am 的命令推荐阅读:https://blog.csdn.net/soslinken/article/details/50245865

1、获取APP 冷/热启动时间的更多相关文章

  1. 2、获取APP CPU占用率

    前面已经介绍过如何获取包名和主活动名.这里不再过多赘述.我们依旧采取两种方案实现APP CPU占有率 Windows下获取APP CPU占用率 adb shell "dumpsys cpui ...

  2. Android 查看App冷启动时间/热启动时间/页面打开时间

    Android 查看App冷启动时间/热启动时间/页面打开时间 冷启动时间 热启动时间 页面打开时间 通过adb查看 adb shell am start -W packageName/Activit ...

  3. 获取app启动时间

    启动APP并收集消耗时间的命令: adb shell am  start -W -n package/activity 手动关闭谷歌浏览器APP(也可以使用命令关闭adb shell am force ...

  4. 3、获取APP 内存占用率

    关于APP内存占用,不用多说,应该是APP性能测试中比较重要的一点.试想一下,开个应用把手机内存占满了,其它应用无法打开,那么这个应用还会有人安装吗?我觉得是没有的.下面就通过adb命令获取APP虚存 ...

  5. iOS获取app图标和启动图片名字(AppIcon and LaunchImage's name)

    在某种场景下,可能我们需要获取app的图标名称和启动图片的名称.比如说app在前台时,收到了远程通知但是通知栏是不会有通知提醒的,这时我想做个模拟通知提示,需要用到icon名称:再比如在加载某个控制器 ...

  6. NSDate获取当前时区的时间

    [NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...

  7. python 获取文件大小,创建时间和访问时间

    # -*- coding: UTF8 -*- import timeimport datetime import os 1. '''把时间戳转化为时间: 1479264792 to 2016-11-1 ...

  8. 获取APP应用的包名信息

    语言: python 3.7 需求:获取APP的包名和程序入口信息,以便在 Appium 脚本中配置 appPackage 和 appActivity 参数. 场景一 资源:已有APP应用的apk安装 ...

  9. Appium+Python自动化 3 -获取 app 包名和 activity

    方法一: ①手机通过USB连接电脑 ②打开手机上被测app ③在电脑上 dos命令窗口,输入命令 adb shell dumpsys window w | findstr \/ | findstr n ...

随机推荐

  1. 实验吧关于隐写术的writeUp(二)

    0x01 Black Hole 1.下载文件后,发现打不开,放到kali中.用命令file 分析一下文件 root@trial:~/Documents# file blackhole.img blac ...

  2. Linux命令 touch

    1.简介 改变文件或者目录的时间,可以更新文件的存取时间(atime,文件内容被读取的时候就会更改的时间)和更改时间(mtime,文件内容被更改是会变更的时间) 2.语法和参数 touch [参数] ...

  3. C++——迭代器

    除了每个容器定义的迭代器外,iterator库内还定义了其他的迭代器. 1.插入迭代器:向容器中插入元素 1.1 back_inserter 1.2 front_inserter 1.3 insert ...

  4. css3 盒模型与 伪元素综合应用案例

    先来简略理解下盒模型: 在 css3 之前,盒模型默认为 box-sizing : content-box,这种模式下的盒模型计算大小方式为,width + padding + border : 而 ...

  5. Qt 打开指定网站/系统文件夹

     本文转载自:http://blog.csdn.net/robertkun/article/details/7802977和http://hi.baidu.com/xyhouse/item/ccf ...

  6. WireMock提供Restful接口数据

    1.去官网下载并启动: 2.引入Pom依赖(主要是com.github.tomakehurst:wiremock): <dependency> <groupId>com.git ...

  7. SQL才是世界上最牛逼的语言!

    身处互联网行业,SQL 可能是你需要掌握的核心技能之一. 最早的时候,SQL 作为一门查询数据库的语言,是程序员的必备技能,运维.开发.Web 以及数据等从业人员都需要用到 SQL,毕竟只有查询到正确 ...

  8. SpringCloud-技术专区-Zuul-使用指南

    Zuul作为微服务系统的网关组件,用于构建边界服务,致力于动态路由.过滤.监控.弹性伸缩和安全. Zuul功能 认证 压力测试 金丝雀测试 动态路由 负载削减 安全 静态响应处理 主动/主动交换管理 ...

  9. 利用单选框的单选特性作tab切换

    <RadioGroup v-model="selectType" type="button" @onchange="selectTypeChan ...

  10. JUnit中Assert简单介绍

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...