1、获取APP 冷/热启动时间
最近在研究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 冷/热启动时间的更多相关文章
- 2、获取APP CPU占用率
前面已经介绍过如何获取包名和主活动名.这里不再过多赘述.我们依旧采取两种方案实现APP CPU占有率 Windows下获取APP CPU占用率 adb shell "dumpsys cpui ...
- Android 查看App冷启动时间/热启动时间/页面打开时间
Android 查看App冷启动时间/热启动时间/页面打开时间 冷启动时间 热启动时间 页面打开时间 通过adb查看 adb shell am start -W packageName/Activit ...
- 获取app启动时间
启动APP并收集消耗时间的命令: adb shell am start -W -n package/activity 手动关闭谷歌浏览器APP(也可以使用命令关闭adb shell am force ...
- 3、获取APP 内存占用率
关于APP内存占用,不用多说,应该是APP性能测试中比较重要的一点.试想一下,开个应用把手机内存占满了,其它应用无法打开,那么这个应用还会有人安装吗?我觉得是没有的.下面就通过adb命令获取APP虚存 ...
- iOS获取app图标和启动图片名字(AppIcon and LaunchImage's name)
在某种场景下,可能我们需要获取app的图标名称和启动图片的名称.比如说app在前台时,收到了远程通知但是通知栏是不会有通知提醒的,这时我想做个模拟通知提示,需要用到icon名称:再比如在加载某个控制器 ...
- NSDate获取当前时区的时间
[NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...
- python 获取文件大小,创建时间和访问时间
# -*- coding: UTF8 -*- import timeimport datetime import os 1. '''把时间戳转化为时间: 1479264792 to 2016-11-1 ...
- 获取APP应用的包名信息
语言: python 3.7 需求:获取APP的包名和程序入口信息,以便在 Appium 脚本中配置 appPackage 和 appActivity 参数. 场景一 资源:已有APP应用的apk安装 ...
- Appium+Python自动化 3 -获取 app 包名和 activity
方法一: ①手机通过USB连接电脑 ②打开手机上被测app ③在电脑上 dos命令窗口,输入命令 adb shell dumpsys window w | findstr \/ | findstr n ...
随机推荐
- PHP定时任务实现(计划任务 vs node.js)
PHP自动任务(单线程) 一.计划任务实现 :最终需要在服务器(windows)上 设置计划任务 1.写好php任务文件 auto.php:链接数据库 判断条件操作数据库 2.创建bat文件 例:ru ...
- upc组队赛6 Progressive Scramble【模拟】
Progressive Scramble 题目描述 You are a member of a naive spy agency. For secure communication,members o ...
- multiple-cursors做代码对齐
multiple-cursors做代码对齐 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839 ...
- iView的表单table
// html<div class="exam-list"> <Table :columns="columns7" :data="d ...
- Spring学习笔记(8)——依赖注入
spring依赖注入使用构造器注入使用属性setter方法注入使用Field注入(用于注解方式) 注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发 ...
- CSS | 字体系列
CSS字体处理中最复杂的部分是字体系列(font-family)匹配和字体加粗(font-weight)匹配,其次是字体大小(font-size)的计算. 一. 字体系列 相同的字体可能有很多不同的称 ...
- Ansible的roles标准化与Jenkins持续集成(三)
Ansible的roles标准化与Jenkins持续集成(三) 链接:https://pan.baidu.com/s/1A3Iq3gGkGS27L_Gt37_I0g 提取码:ncy2 复制这段内容后打 ...
- day08 python文件操作
day08 python 一.文件操作 1.文件操作的函数 open(文件名, mode=模式, encoding=字符集) 2.模式: r, w, a, r+ ...
- 【转】前后端分离架构:web实现前后端分离,前后端解耦
一.前言 ”前后端分离“已经成为互联网项目开发的业界标杆,通过Tomcat+Ngnix(也可以中间有个Node.js),有效地进行解耦.并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构 ...
- vue 本地环境API代理设置和解决跨域
写一个config.js文件,作为项目地址的配置. //项目域名地址 const url = 'https://exaple.com'; let ROOT; //由于封装的axios请求中,会将ROO ...