APP性能测试——首次启动耗时测试
首次启动耗时:
即第一次安装(清除数据也可以),打开软件,直到进入到首页activity页面,并计算耗时。
示例代码:
import os import time # 测试首次启动时间 # 创建App进程, 加载相关资源, 启动Main Thread, 初始化首屏Activity def startUpTime(pg_name, pga_name): """ :param device: 设备id :param pg_name: 包名 :param pga_name: activity名称 """ wait_time = 5 try: with open('F:/report/firstStartUp.txt', 'wb+') as file: su_time = [] for i in range(30): os.popen("adb shell pm clear {}".format(pg_name)) # 清除app缓存 time.sleep(wait_time) # 启动activity start = os.popen("adb shell am start -W {}".format(pga_name)) time.sleep(wait_time) data = start.readlines() for line in data: if "TotalTime:" in line: line = line.strip() print("第{}次TotalTime为: {}ms".format(i+1,line[11:])) if int(line[11:]) == 0: break su_time.append(int(line[11:])) file.write(('第{}次\n'.format(i + 1)).encode()) line += '\n' line = line.encode() file.write(line) return su_time except os.error as error: print(error) def getDev(): """ :return: 获得设备id """ try: devices_info = os.popen('adb devices') data = devices_info.readlines() if len(data) != 0 and data[1].find('device'): s = data[1][:-7] return s return 0 except Exception as error: print(error) def main_cold(): # 取测试机-s s = getDev() pn = 'sg.partying.ghost.android' an = 'sg.partying.ghost.android/com.bb.infinity.InfiActivity' print('设备:{},APP包名:{}, activity:{}\n首次启动测试开始...'.format(s,pn, an)) time_list = startUpTime(pn, an) total_time = 0 for i in time_list: total_time += i avg_time = total_time / len(time_list) print('首次启动平均耗时: {}ms'.format(avg_time)) with open('F:/report/firstStartUp.txt', 'ab+')as f: f.write(('\n设备{}平均首次启动耗时:{}ms'.format(s,avg_time)).encode()) if __name__ == '__main__': main_cold()