android 開機時間檢測

基於SPRD平臺,根據關鍵字檢查開機耗時

#coding=utf-8
__author__="ao.deng"
import ctypes, sys,os
import re
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12

# 字體顏色定義 text colors
FOREGROUND_BLUE = 0x09  # blue.
FOREGROUND_GREEN = 0x0a  # green.
FOREGROUND_RED = 0x0c  # red.
FOREGROUND_YELLOW = 0x0e  # yellow.

# 背景顏色定義 background colors
BACKGROUND_YELLOW = 0xe0  # yellow.

# get handle
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)


def set_cmd_text_color(color, handle=std_out_handle):
    Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
    return Bool


# reset white
def resetColor():
    set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)


# green
def printGreen(mess):
    set_cmd_text_color(FOREGROUND_GREEN)
    sys.stdout.write(mess + '\n')
    resetColor()


# red
def printRed(mess):
    set_cmd_text_color(FOREGROUND_RED)
    sys.stdout.write(mess + '\n')
    resetColor()


# yellow
def printYellow(mess):
    set_cmd_text_color(FOREGROUND_YELLOW)
    sys.stdout.write(mess + '\n')
    resetColor()


# white bkground and black text
def printYellowRed(mess):
    set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED)
    sys.stdout.write(mess + '\n')
    resetColor()

BOOT_PROGRESS_KEYWORD =r".*? (\d{2}:\d{2}:\d{2}.\d{3}).*?(boot_progress_\S+): (\d+)"
AM_ON_RESUME_CALLED=r".*? (\d{2}:\d{2}:\d{2}.\d{3}).*?am_on_resume_called:.*?Launcher,LAUNCH_ACTIVITY]"
DESC_DICT={
    "boot_progress_start":u"進入zygote",
    "boot_progress_preload_start":u"開始preload class&resource",
    "boot_progress_preload_end":u"preload結束",
    "boot_progress_system_run":u"systemserver起來了",
    "boot_progress_pms_start":u"pms服務起來",
    "boot_progress_pms_system_scan_start":u"開始掃描system分區",
    "boot_progress_pms_data_scan_start":u"開始掃描data分區",
    "boot_progress_pms_scan_end":u"pms掃描結束",
    "boot_progress_pms_ready":u"pms ready",
    "boot_progress_ams_ready":u"pms到ams間會起一堆服務,然後ams 服務ready",
    "boot_progress_enable_screen":u"進入屏幕",
    "FallbackHome":u"進入fallback界面,等待user_unlocked廣播",
    "Launcher":u"進入桌面"
}
CONSUME_TIME_STANDARD={
    "boot_progress_start":10512,
    "boot_progress_preload_start":2294,
    "boot_progress_preload_end":2570,
    "boot_progress_system_run":453,
    "boot_progress_pms_start":1082,
    "boot_progress_pms_system_scan_start":535,
    "boot_progress_pms_data_scan_start":779,
    "boot_progress_pms_scan_end":335,
    "boot_progress_pms_ready":759,
    "boot_progress_ams_ready":2995,
    "boot_progress_enable_screen":4848,
    "boot_up":56091
}


def read_log_file(logFile):
    with open(logFile,"r") as f:
        content =f.read()
        f.close()
    return content

def filter_keyword_time(keyword,logFile):
    boot_progress_keyword_time_array = re.findall(keyword,logFile)
    #print (boot_progress_keyword_time_array)
    return boot_progress_keyword_time_array

def format_time(stacktime):
    t =stacktime.split(":")
    s=t[-1].split(".")
    return int(s[1])/1000.0+int(s[0])+int(t[-2])*60+int(t[-3])*3600
def analysisData(boot_progress_keyword_time_array,launcher_array):
    timeStart=boot_progress_keyword_time_array[0][2]
    printYellow(DESC_DICT['boot_progress_start'])

    if int(timeStart)<CONSUME_TIME_STANDARD["boot_progress_start"]:
        printGreen(u"{} boot_progress_start {}ms".format(u"內核之前時間",timeStart))
    else:
        printRed(u"{} boot_progress_start {}ms".format(u"內核之前時間", timeStart))
    boot_progress_ams_ready_current_time =0
    boot_progress_ams_ready_current_stacktime=0
    for stacktime,name,time in boot_progress_keyword_time_array[1:]:
        if name=="boot_progress_ams_ready":
            boot_progress_ams_ready_current_time =int(time)
            boot_progress_ams_ready_current_stacktime = stacktime
        if name in DESC_DICT.keys():
            desc=DESC_DICT[name]
        else:
            desc=""
        printYellow(u"{}".format(desc))
        consume_time=int(time)-int(timeStart)

        if consume_time<CONSUME_TIME_STANDARD[name]:
            printGreen(u"{} consume time: {}ms".format(name, consume_time))
        else:
            printRed(u"{} consume time: {}ms".format(name,consume_time))
        timeStart=time

    # 本次開機時長可以用:
    # 002A7F 12-28 16:08:33.652   650   650 I boot_progress_ams_ready: 43367
    # 00476D 12-28 16:08:48.448  1436  1436 I am_on_resume_called: [0,com.android.launcher3.Launcher,LAUNCH_ACTIVITY]
    # 計算:  43.367+(48.448-33.652)= 58.163s
    boot_up_time =boot_progress_ams_ready_current_time/1000.0+(format_time(launcher_array[0])-format_time(boot_progress_ams_ready_current_stacktime))

    printGreen(u"{} boot up consume time: {}s".format(u"本次開機時長",boot_up_time))


if __name__=="__main__":
    logFile=r"events.log"
    content = read_log_file(logFile)
    boot_progress_keyword_time_array=filter_keyword_time(BOOT_PROGRESS_KEYWORD,content)
    launcher_array=filter_keyword_time(AM_ON_RESUME_CALLED,content)
    analysisData(boot_progress_keyword_time_array,launcher_array)
    os.system("pause")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章