获取Android系统应用启动时间的Python实现

获取Android系统应用启动时间的Python实现,第1张

获取Android系统应用启动时间的Python实现
def getPID(pkgName):
    pidStr = "ps | grep " + pkgName
    out = d.shell(pidStr)
    print(str(out).split("='")[1].split(" "))
    list = str(out).split("='")[1].split(" ")
    i = 1
    while i < len(list):
        if list[i] != "":
            pid = list[i]
            break
        i += 1
    return pid
def getAppStartTime(pkgName, startTimes, startType):
    coldStartTimeList = []
    warmStartTimeList = []
    i = 0
    j = 0
    if os.path.exists("./pkgInfoFile_AList.txt"):
        AListFile = open("./pkgInfoFile_AList.txt", mode="r+")
        AList1 = AListFile.readlines()
        AListFile.close()
        for item in AList1:
            if pkgName is None:
                pkgName = input("please input your test packageName:")
            if item.find(pkgName) != -1:
                coldStartCMDStr = "am start -S -W " + item.strip("n")
                warmStartCMDStr = "am start -W " + item.strip("n")
                if startType is None:
                    startType = int(
                        input("please input your startType(0-coldStart/1-warmStart/2-coldStart&warmStart):"))
                if startTimes is None:
                    startTimes = int(input("请输入您需要测试启动的次数:"))
                startTimeFilePath = "./startTimeFile_" + pkgName + ".txt"
                startTimeFile = open(startTimeFilePath, mode="w+")
                startTimeFile.seek(0)
                startTimeFile.write("StartTime Collect for APP:" + pkgName + "  Test Times:" + str(startTimes) + "n")
                if startType == 0:
                    print("正在获取冷启动时间...")
                    d.shell(coldStartCMDStr)
                    while i < startTimes:
                        d.shell("kill -9 " + getPID(pkgName))
                        time.sleep(1)
                        coldStartCMDOut = d.shell(coldStartCMDStr)
                        coldStartTime = str(coldStartCMDOut).split("TotalTime:")[1].split("\n")[0]
                        coldStartTimeList.append(int(coldStartTime))
                        coldStartTimeStr = pkgName + " coldStartTime:" + coldStartTime
                        print(coldStartTimeStr)
                        startTimeFile.write(coldStartTime + "n")
                        startTimeFile.flush()
                        time.sleep(4)
                        i += 1
                    print(coldStartTimeList)
                    avg_coldStartTime = np.mean(coldStartTimeList)
                    print(pkgName + " AVG_coldStartTime:" + str('%.1f' % avg_coldStartTime))
                    startTimeFile.write("AVG_coldStartTime:" + str('%.1f' % avg_coldStartTime) + "n")
                    startTimeFile.flush()
                    startTimeFile.close()
                elif startType == 1:
                    print("正在获取暖启动时间...")
                    d.shell(warmStartCMDStr)
                    while i < startTimes:
                        d.press("home")
                        time.sleep(3)
                        warmStartCMDOut = d.shell(warmStartCMDStr)
                        warmStartTime = str(warmStartCMDOut).split("TotalTime:")[1].split("\n")[0]
                        warmStartTimeStr = pkgName + " warmStartTime:" + warmStartTime
                        print(warmStartTimeStr)
                        startTimeFile.write(warmStartTime + "n")
                        startTimeFile.flush()
                        warmStartTimeList.append(int(warmStartTime))
                        time.sleep(2)
                        i += 1
                    print(warmStartTimeList)
                    avg_warmStartTime = np.mean(warmStartTimeList)
                    print(pkgName + " AVG_warmStartTime:" + str('%.1f' % avg_warmStartTime))
                    startTimeFile.write("AVG_warmStartTime:" + str('%.1f' % avg_warmStartTime) + "n")
                    startTimeFile.flush()
                    startTimeFile.close()
                elif startType == 2:
                    print("正在获取冷启动和暖启动时间...")
                    print("start to get coldStartTime for " + pkgName)
                    d.shell(coldStartCMDStr)
                    while i < startTimes:
                        d.shell("kill -9 "+getPID(pkgName))
                        time.sleep(1)
                        coldStartCMDStr = "am start -S -W " + item.strip("n")
                        coldStartCMDOut = d.shell(coldStartCMDStr)
                        coldStartTime = str(coldStartCMDOut).split("TotalTime:")[1].split("\n")[0]
                        coldStartTimeList.append(int(coldStartTime))
                        coldStartTimeStr = pkgName + " coldStartTime:" + coldStartTime
                        startTimeFile.write(coldStartTime + "n")
                        startTimeFile.flush()
                        time.sleep(2)
                        i += 1
                    avg_coldStartTime = np.mean(coldStartTimeList)
                    print(pkgName + " AVG_coldStartTime:" + str('%.1f' % avg_coldStartTime))
                    startTimeFile.write("AVG_coldStartTime:" + str('%.1f' % avg_coldStartTime) + "n")
                    startTimeFile.flush()
                    d.shell(warmStartCMDStr)
                    print("start to get warmStartTime for " + pkgName)
                    while j < startTimes:
                        d.press("home")
                        time.sleep(1)
                        warmStartCMDStr = "am start -W " + item.strip("n")
                        warmStartCMDOut = d.shell(warmStartCMDStr)
                        time.sleep(1)
                        warmStartTime = str(warmStartCMDOut).split("TotalTime:")[1].split("\n")[0]
                        warmStartTimeList.append(int(warmStartTime))
                        warmStartTimeStr = pkgName + " warmStartTime:" + warmStartTime
                        startTimeFile.write(warmStartTime + "n")
                        startTimeFile.flush()
                        j += 1
                    avg_warmStartTime = np.mean(warmStartTimeList)
                    print(pkgName + " AVG_warmStartTime:" + str('%.1f' % avg_warmStartTime))
                    startTimeFile.write("AVG_warmStartTime:" + str('%.1f' % avg_warmStartTime) + "n")
                    startTimeFile.flush()
                    startTimeFile.close()
                else:
                    print("please input 0 or 1 or 2")
                    continue
                break
            else:
                continue
    else:
        print("请先获取系统内App信息!!!")
        getPKGInfoOK()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5652118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存