标签:
最近有个需求,需要对比前后两个版本单个activity加载的时间
在android日志中我们可以看到类似INFO/ActivityManager(2486): Displayed activity com.teleca/.ContextMenuActivity: 240 ms (total 41289 ms)的日志,即为Activity的加载时间
首先通过adb logcat > xx.txt获取日志,然后用如下代码分析日志:
#-*-coding:utf-8 -*- # 分析android adb logcat日志 提取activity Displayed日志 import os import re def AndroidDisplayedLog(applicationName, logfile): if os.path.exists(logfile): with open(logfile) as f: log = f.readlines() ActivityLoadTime = [] for line in log: if re.search(r"Displayed %s" % applicationName, line): ActivityManagerLog = line.split(":") activity = ActivityManagerLog[1].split("/")[-1] loadTimeStr = ActivityManagerLog[-1].strip().strip(‘ms‘).strip(‘+‘) # 日志时间转换为ms的整数 try: t = int(loadTimeStr) if t: loadtime = t except ValueError: # 过滤常驻的系统activity 超过分钟的 try: loadtime = int(loadTimeStr.split(‘s‘)[0]) * 1000 + int(loadTimeStr.split(‘s‘)[1]) except ValueError: pass print activity, loadtime ActivityLoadTime.append((activity, loadtime)) return ActivityLoadTime else: print "日志文件不存在" a = AndroidDisplayedLog("com.aaa.bbbb", "D:\\log.txt")
python 分析android日志获取activit加载时间
标签:
原文地址:http://www.cnblogs.com/donjor/p/4660925.html