前段时间分享的monkey 压测 app 监控性能

重构后

def get_error(log):
    with open(log, encoding="utf-8") as monkey_log:
        lines = monkey_log.readlines()
        for line in lines:
            if re.findall(go.ANR, line):
                print('\033[1;31;42m')
                print("存在anr错误:", line)
                go.I_ANR += 1
            if re.findall(go.CRASH, line):
                print('\033[1;31;42m')
                print("存在crash错误:", line)
                go.I_CRASH += 1
            if re.findall(go.EXCEPTION, line):
                print('\033[1;31;42m')
                print("存在exception异常:", line)
                go.I_EXCEPTION += 1
        if go.I_ANR == 0 and go.I_CRASH == 0 and go.I_EXCEPTION == 0:
            print("恭喜,没有任何错误")

 # 存手机信息
def get_phome(phonelog):
    bg = BphomeMsg.getPhone("log.txt").get_phone_Kernel()
    logname = phonelog + "_" + bg[0]["phone_model"] + bg[0]["phone_name"] + bg[0]["release"] + ".txt"
    of = OperateFile.base_file(logname, "w+")
    if of.mkdir_file():
        result = "手机型号:" + bg[0]["phone_name"] + "\n"
        result += "手机名字:" + bg[0]["phone_model"] + "\n"
        result += "系统版本:" + bg[0]["release"] + "\n"
        result += "手机分辨率:" + bg[3] + "\n"
        result += "手机运行内存:" + bg[1] + "\n"
        result += "CPU核数:" + bg[2] + "\n"
        of.write_txt(result)

#开始脚本测试
def start_monkey(cmd, logdir, now1):

    # Monkey测试结果日志:monkey_log
    os.popen(cmd )
    print(cmd)

    # Monkey时手机日志,logcat
    logcatname = logdir+"\\"+now1+r"logcat.log"
    cmd2 = "adb logcat -d >%s" %(logcatname)
    os.popen(cmd2)

if __name__ == '__main__':
    ini_file = 'monkey.ini'
    ba = BAdbCommon
    if OperateFile.base_file(ini_file, "r").check_file():
        if ba.attached_devices():
            mconfig = MMonkeyConfig.monkeyconfig()
            mc = BMonkeyConfig.monkeyConfig(mconfig, ini_file)
            # 打开想要的activity
            ba.open_app(mc.package_name, mc.activity)
            temp = ""
             # monkey开始测试
            start_monkey(mc.cmd, mc.logdir, mc.now)
            while True:
                with open(mc.monkey_log, encoding='utf-8') as monkeylog:
                    if monkeylog.read().count('Monkey finished') > 0:
                        print("测试完成咯")
                        get_error(mc.monkey_log)
                        get_phome(mc.phone_msg_log)
                        break
        else:
            print("设备不存在")
    else:
        print(u"配置文件不存在"+ini_file)


def getModel(self):
    os.system('adb shell cat /system/build.prop >'+self.cmd_log)
    l_list = {}
    with open(self.cmd_log, "r") as f:
        lines = f.readlines()
        for line in lines:
            line = line.split('=')
            #Android 系统,如anroid 4.0
            if (line[0] == 'ro.build.version.release'):
                l_list["release"] = line[1].replace("\n", " ")
                #手机名字
            if (line[0]=='ro.product.model'):
                print(line[1])
                l_list["phone_name"] = line[1].replace("\n", " ")
                #手机品牌
            if (line[0]=='ro.product.brand'):
                 l_list["phone_model"] = line[1].replace("\n", " ")

    # 删除本地存储的手机信息文件
    if os.path.exists(self.cmd_log):
        os.remove(self.cmd_log)
    return l_list

def get_men_total(self):
    os.system("adb shell cat /proc/meminfo >" + self.cmd_log)
    men_total = ""
    with open(self.cmd_log, "r") as f:
            lines = f.readlines()
            for line in lines:
                line = line.split('=')
                if line[0]:
                    men_total = re.findall(r"\d+", line[0])[0]
                    break
    if os.path.exists(self.cmd_log):
        os.remove(self.cmd_log)
    return men_total
# 得到几核cpu
def get_cpu_kel(self):
    os.system("adb shell cat /proc/cpuinfo >" + self.cmd_log)
    cpu_kel = 0
    with open(self.cmd_log, "r") as f:
            lines = f.readlines()
            for line in lines:
                line = line.split(':')
                if line[0].find("processor") >= 0:
                   cpu_kel += 1
    if os.path.exists(self.cmd_log):
        os.remove(self.cmd_log)
    return str(cpu_kel) + "核"
# print(get_cpu_kel("d:\\men.txt"))

# 得到手机分辨率
def get_app_pix(self):
    result = os.popen("adb shell wm size", "r")
    return result.readline().split("Physical size:")[1]

测试结果



↙↙↙阅读原文可查看相关链接,并与作者交流