其他分享
首页 > 其他分享> > APP性能测试——安装耗时测试

APP性能测试——安装耗时测试

作者:互联网

安装耗时:

    这里我们用pm命令安装app,来截取安装时间(不要使用adb install安装,因为那样多一个push app的耗时)。

示例代码:

import os,time,datetime

def setuptime(package_name):
    '''单次安装耗时'''
    os.popen('adb uninstall {}'.format(package_name)) #卸载app
    time.sleep(3)
    start_time=datetime.datetime.now()
    os.popen("adb shell pm install /data/local/tmp/ghost-09081547.apk") #通过pm命令安装apk
    end_time=datetime.datetime.now()
    setup_time=(end_time-start_time).total_seconds() #计算单次安装耗时
    return setup_time

def installTime(package_name):
    '''测试安装30次平均耗时并写入txt文本'''
    try:
        with open('F:/report/installtime.txt', 'wb+') as file:
            su_time = []
            for i in range(30):
                setup_time = setuptime(package_name)
                if setup_time==0:
                    continue
                else:
                    su_time.append(setup_time)
                    print("第{}次安装耗时为: {}s".format(i + 1,setup_time))
                    file.write(("第{}次安装耗时为: {}s\n".format(i + 1,setup_time)).encode())
            return su_time
    except os.error as error:
        print(error)

def getDev():
    '''获得设备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_setup(package_name):
    '''计算平均耗时并写入txt文本'''
    s = getDev()
    print('设备:{},APP包名:{}\n安装测试开始...'.format(s,package_name))
    time_list = installTime(package_name)
    total_time = 0
    for i in time_list:
        total_time += i
    avg_time = total_time / len(time_list)
    print('安装平均耗时: {}s'.format(avg_time))
    with open('F:/report/installtime.txt', 'ab+')as f:
        f.write(('\n设备{}平均安装耗时:{}s'.format(s,avg_time)).encode())


if __name__ == '__main__':
    os.popen("adb push F:\ghost\ghost-09081547.apk /data/local/tmp/") #通过adb命令将apk从pc本地拷贝到手机内存中,注意不要存入/sdcard/目录,部分手机限制访问
    package_name = 'sg.partying.ghost.android' #app应用包名
    main_setup(package_name)

 

标签:name,package,APP,耗时,测试,time,安装,setup
来源: https://www.cnblogs.com/muxiaomu/p/16689244.html