其他分享
首页 > 其他分享> > APP性能测试——冷启动耗时测试

APP性能测试——冷启动耗时测试

作者:互联网

冷启动耗时:

    即后台没有该应用的进程(我们使用am force-stop命令结束app运行),打开软件,直到进入到首页activity页面,并计算耗时。

示例代码:

import os
import time


# 测试冷启动时间
# 创建App进程, 加载相关资源, 启动Main Thread, 初始化首屏Activity
def startUpTime(pg_name, pga_name):
    """
    :param device: 设备id
    :param pg_name: activity名称
    :param pga_name: 包名
    """
    wait_time = 5
    try:
        with open('F:/report/coldStartUp.txt', 'wb+') as file:
            su_time = []
            for i in range(30):
                os.popen("adb shell am force-stop {}".format(pg_name))  # kill 进程
                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'  # aapt dump badging + apk
    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/coldStartUp.txt', 'ab+')as f:
        f.write(('\n设备{}平均冷启动耗时:{}ms'.format(s,avg_time)).encode())


if __name__ == '__main__':
    main_cold()

 

标签:name,format,APP,冷启动,测试,time,line,data
来源: https://www.cnblogs.com/muxiaomu/p/16689254.html