adbパフォーマンスデータ取得の一般的な方法


パッケージ名の取得
def get_package_name(udid):
    pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
    #shell dumpsys window w |findstr \/ |findstr name
    getPackNameCmd = "adb -s" + " " + udid + " " + "shell dumpsys window w |findstr \/ |findstr name="
    print(getPackNameCmd)
    packageText = subprocess.Popen(getPackNameCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.read()
    packageInfo = (str(packageText)).split('=')[2]
    packageName = pattern.findall(packageInfo)[0].split('/')[0]
    return packageName

アクティビティ名の取得

def get_activity_name(udid):
    pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
    getActivityCmd = "adb -s" + " " + udid + " " + "shell dumpsys window w |findstr \/ |findstr name="
    packageText = subprocess.Popen(getActivityCmd, shell=True, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE).stdout.read()
    packageInfo = (str(packageText)).split('=')[2]
    print("     ", packageInfo)
    activityName = pattern.findall(packageInfo)[0].split('/')[1]
    print("     ", activityName)
    return activityName

apkパッケージのメモリ使用量の取得
#   apk       
def mem(udid):
    memCmd = 'adb -s '+ udid + ' shell dumpsys meminfo ' + get_package_name(udid)
    # print("    ",memCmd)
    menTextList = subprocess.Popen(memCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
    # print("    ",menTextList)
    for memInfo in menTextList:
        if len(memInfo.split())>0 and memInfo.split()[0].decode() == "TOTAL":
            mem = int(memInfo.split()[1].decode())//1024
            print("    ",mem, "M")
            # men_list = str(info.split()[1].decode())
    return mem

cpu占有率の取得
#   cpu   
def cpu(udid):
    cpuCmd = 'adb -s ' + udid + ' shell top -n 1| findstr ' + get_package_name(udid)
    print ("cpu    ",cpuCmd)
    cpuText = subprocess.Popen(cpuCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
    print("cpu  ", cpuText)
    if len(cpuText) >= 1:
        cpu = str(cpuText[0].split()[4])
        cpu = cpu.split("'")[1]
        print("cpu",cpu)
        # cpu_list.append(int(top_info[0].split()[2][0:-1]))

バッテリ温度、残電量、およびリアルタイム温度の取得
def gettem(udid):
    #            
    batteryCmd = 'adb -s ' + udid + ' shell "dumpsys  battery | grep "level:""'
    batteryText = subprocess.Popen(batteryCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.read()

    batteryTemperatureCmd = 'adb -s ' + udid + ' shell "dumpsys  battery | grep "temperature:""'
    batteryTemperatureText = subprocess.Popen(batteryTemperatureCmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.read()
    # print("    ",batteryText)
    # print("      ", batteryTemperatureText)
    battery = int(batteryText.split()[1])
    print("    ", battery)

    batteryTemp = int(batteryTemperatureText.split()[1])/10
    print("    ",batteryTemp)

    #  cpu  
    cpuTemperatureText = subprocess.Popen('adb -s ' + udid + ' shell cat /sys/class/thermal/thermal_zone9/temp',
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.read()
    cpuTemp = int(cpuTemperatureText.split()[0])/1000
    print("cpu  ",cpuTemp)