美文网首页测试开发
Android-APP启动时间-for Python

Android-APP启动时间-for Python

作者: 沈宥 | 来源:发表于2022-05-18 15:43 被阅读0次

    一、获取APP启动时间

    1、adb命令行获取
    adb shell am start -S -W com.xxxx.xxxx/com.xxxx.biz.main.ui.activity.SplashPageActivity

    -S:表示每次启动前先强行停止

    2、python执行adb命令

    import subprocess
    res = subprocess.Popen('adb shell am start -S -W com.xxxx.xxxx/com.xxxx.biz.main.ui.activity.SplashPageActivity', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
    result = res.stdout.readlines()
    

    3、拆解结果中所需要的数据

    # 系统启动APP耗时
    WaitTime = str(result[-2]).split(':')[-1].replace("\\n'", '')
    # APP启动耗时
    TotalTime = str(result[-3]).split(':')[-1].replace("\\n'", '')
    
    66B2EDAD-896B-4C29-9E4E-7EC3EB5DE31A.png

    二、数据写入Excel表格

    1、引入openpyxl处理表格内容

    from openpyxl import Workbook
    
    # 创建一个新的工作簿
    wb = Workbook()
    # 获取默认sheet
    ws = wb.active
    ws.title = "DefaultSheet"
    
    

    2、需要获取每次启动时间和平均值

    ws['A1'] = '序号'
    ws['B1'] = 'WaitTime'
    ws['C1'] = 'TotalTime'
    ws['D1'] = 'WaitTime平均值'
    ws['E1'] = 'TotalTime平均值'
    
    #保存累加结果,计算平均值
    launchlist = []
    

    3、写入数据,假设共启动了10次

        for index in range(0, 10):
            # 写入每次启动时间
            ws['B' + str(index + 2)] = WaitTime
            ws['C' + str(index + 2)] = TotalTime
            averWaitTime += WaitTime
            averTotalTime += TotalTime
    
        # 计算平均值
        ws['D2'] = averWaitTime / 10
        ws['E2'] = averTotalTime / 10
    
        #写完后,一定要保存文件
        wb.save('/Users/username/Desktop/comment.xlsx')
    
    

    三、创建折线图

    1、引入折线图包

    from openpyxl.chart import (
        LineChart,
        Reference,
    )
    lineimage = LineChart()
    

    2、折线图属性

    #折线图标题
    lineimage.title = "启动时间"
    #Y轴标题
    c2.y_axis.title = "耗时(ms)"
    #X轴标题
    c2.x_axis.title = "Date"
    
    

    3、添加数据

    # 折线图原始数据获取,min_col:起始列;min_row:起始行;max_col:结束列;max_row:结束行
    data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=count)
    lineimage.add_data(data, titles_from_data=True)
    # X轴数据显示,min_col:起始列;min_row:起始行;max_row:结束行
    dates = Reference(ws, min_col=1, min_row=2, max_row=count)
    lineimage.set_categories(dates)
    
    

    四、结果示例

    56D394F1-95FC-45B0-A9DF-3106AB15DEF0.png

    五、完整代码

    
    import subprocess
    from openpyxl import Workbook
    from openpyxl.chart import (
        LineChart,
        Reference,
    )
    
    # 创建一个新的工作簿
    wb = Workbook()
    # 获取默认sheet
    ws = wb.active
    ws.title = "DefaultSheet"
    ws['A1'] = '序号'
    ws['B1'] = 'WaitTime'
    ws['C1'] = 'TotalTime'
    ws['D1'] = 'WaitTime平均值'
    ws['E1'] = 'TotalTime平均值'
    
    launchlist = []
    
    
    def adbrun(cmd):
        res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
        result = res.stdout.readlines()
        return result
    
    
    def launch():
        # -S:表示每次启动前先强行停止
        cmd_start = 'adb shell am start -S -W com.xxxx.xxxx/com.xxxx.biz.main.ui.activity.SplashPageActivity'
        result = adbrun(cmd_start)
        # 系统启动APP耗时
        WaitTime = str(result[-2]).split(':')[-1].replace("\\n'", '')
        # APP启动耗时
        TotalTime = str(result[-3]).split(':')[-1].replace("\\n'", '')
    
        return {'WaitTime': int(WaitTime), 'TotalTime': int(TotalTime)}
    
    
    def writeexcel(launchlist):
        averWaitTime = 0
        averTotalTime = 0
        for index in range(0, len(launchlist)):
            # 为X轴添加序列号
            ws['A' + str(index + 2)] = index + 1
    
            # 写入每次启动时间
            WaitTime = launchlist[index]['WaitTime']
            TotalTime = launchlist[index]['TotalTime']
            ws['B' + str(index + 2)] = WaitTime
            ws['C' + str(index + 2)] = TotalTime
            averWaitTime += WaitTime
            averTotalTime += TotalTime
    
        # 计算平均值
        ws['D2'] = averWaitTime / len(launchlist)
        ws['E2'] = averTotalTime / len(launchlist)
        wb.save('/Users/username/Desktop/comment.xlsx')
    
        linechart(len(launchlist))
    
    
    def linechart(count):
        lineimage = LineChart()
        lineimage.title = "启动时间"
    
        # 折线图原始数据获取,min_col:起始列;min_row:起始行;max_col:结束列;max_row:结束行
        data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=count+1)
        lineimage.add_data(data, titles_from_data=True)
        # X轴数据显示,min_col:起始列;min_row:起始行;max_row:结束行
        dates = Reference(ws, min_col=1, min_row=2, max_row=count+1)
        lineimage.set_categories(dates)
    
        # 折线图添加的位置
        ws.add_chart(lineimage, "G2")
    
        wb.save('/Users/username/Desktop/comment.xlsx')
    
    
    if __name__ == '__main__':
        for index in range(0, 10):
            launchlist.append(launch())
    
        writeexcel(launchlist)
    

    相关文章

      网友评论

        本文标题:Android-APP启动时间-for Python

        本文链接:https://www.haomeiwen.com/subject/hijaurtx.html