python automation to get android APP startup speed

python automation to get android APP startup speed

Enter the dos window, adb shell dumpsys SurfaceFlinger -list, open the APP, APP Activity obtain first page
as com.hualv.myliveroomplay of APP is the first Activity: com.hualv.myliveroomplay / com.hualv.myliveroomplay.activity.LoginActivity
Here Insert Picture Description
replacement after running to test Activity APP

Here Insert Picture Description
CSV is output to the averaging
Here Insert Picture Description

#!/user/bin/env python3
# -*- coding: utf-8 -*-
import os,re
import time
import datetime
import subprocess
import numpy as np
from subprocess import Popen, PIPE
import logging
if os.path.exists(os.getcwd() + "/Startspeed_data"):
    pass
else:
    os.makedirs(os.getcwd() + "/Startspeed_data")
csv = logging.getLogger()
csv.setLevel(logging.DEBUG)
fh= logging.FileHandler(os.getcwd() + "/Startspeed_data/"+time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) + '.csv')
fh.setLevel(logging.INFO)
ch= logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter()
ch.setFormatter(formatter)
fh.setFormatter(formatter)
csv.addHandler(ch)
csv.addHandler(fh)

def Get_Start_Cold(Activity):
    try:
        cmd = 'adb shell am start -W -n %s'%Activity  # % apk_file
        cmd_cold='adb shell am force-stop %s'%Activity.split("/")[0]
        (os.popen(cmd_cold).readlines())
        time.sleep(2)
        redcmd =str((os.popen(cmd).readlines())).replace("'", "").replace("\\n", " ").replace("]", " ").replace("[", " ").split(" ,")
        return ({"cold":{redcmd[-3].split(":")[0].replace(" ", ""):int(redcmd[-3].split(":")[-1])}})

    except Exception as e:
        print(str(e),"Get_Start_Cold(Activity),请检查adb是否连接……")

def Get_Start_Hot(Activity):
    try:
        cmd = 'adb shell am start -W -n %s'%Activity  # % apk_file
        cmd_hot='adb shell input keyevent 3'
        (os.popen(cmd_hot).readlines())
        time.sleep(2)
        redcmd =str((os.popen(cmd).readlines())).replace("'", "").replace("\\n", " ").replace("]", " ").replace("[", " ").split(" ,")
        return ({"hot":{redcmd[-3].split(":")[0].replace(" ", ""):int(redcmd[-3].split(":")[-1])}})

    except Exception as e:
        print(str(e),"Get_Start_Hot(Activity),请检查adb是否连接……")


def StartTest(Activity):
    csv.info("ID,HotStart,ColdStart")
    for i in range(100):
        cold=Get_Start_Cold(Activity)['cold']['TotalTime']
        time.sleep(1)
        hot = Get_Start_Hot(Activity)['hot']['TotalTime']
        csv.info("%s,%s,%s"%(i+1,hot,cold))

if __name__ == '__main__':
    Activity='com.hualv.myliveroomplay/com.hualv.myliveroomplay.activity.LoginActivity'#adb shell dumpsys SurfaceFlinger获取APPactivity
    StartTest(Activity)
Published 58 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_42846555/article/details/104309449