def start(device):
    devices = device["devices"]
    try:
        num = device["match_num"]
        app = {}
        mkdirInit(devices, app, num)
        # mc = BaseMonkeyConfig.monkeyConfig(PATH("./monkey.ini"))

        mc = BaseMonkeyConfig.monkeyConfig("./monkey.ini")
        # 打开想要的activity
        # ba.open_app(mc["package_name"], mc["activity"], devices) 留着备用可以统计每次打开哪个页面的启动时间等
        # monkey开始测试
        if not os.path.exists('./monkey_log'):
            os.mkdir('./monkey_log')

        mc["log"] = PATH("./monkey_log") + "\\" + str(uuid.uuid4())
        mc["monkey_log"] = mc["log"] + "monkey.log"
        mc["cmd"] = mc['cmd'] + mc["monkey_log"]
        start_monkey(devices, "adb -s " + devices + " shell " + mc["cmd"],
                     mc["log"])
        time.sleep(1)
        starttime = datetime.datetime.now()
        pid = BaseMonitor.get_pid(mc["package_name"], devices)
        cpu_kel = BaseMonitor.get_cpu_kel(devices)
        beforeBattery = BaseMonitor.get_battery(devices)
        while True:
            with open(mc["monkey_log"], encoding='utf-8') as monkeylog:
                time.sleep(1)  # 每1秒采集检查一次
                BaseMonitor.cpu_rate(pid, cpu_kel, devices)
                BaseMonitor.get_men(mc["package_name"], devices)
                BaseMonitor.get_fps(mc["package_name"], devices)
                BaseMonitor.get_flow(pid, mc["net"], devices)
                BaseMonitor.get_battery(
                    devices)  # 上方的beforeBattery和下方afterBattery会使电量列多两行,后面再优化
                if monkeylog.read().count('Monkey finished') > 0:
                    endtime = datetime.datetime.now()
                    # print(str(devices) + "测试完成!")
                    writeSum(1, path=PATH("./info/sumInfo.pickle"))
                    app[devices]["header"]["beforeBattery"] = beforeBattery
                    app[devices]["header"][
                        "afterBattery"] = BaseMonitor.get_battery(devices)
                    app[devices]["header"]["net"] = mc["net"]
                    app[devices]["header"]["monkey_log"] = mc["monkey_log"]
                    app[devices]["header"]["time"] = str(
                        (endtime - starttime).seconds) + "秒"
                    writeInfo(app, PATH("./info/info.pickle"))
                    break
                    # go.info[devices]["header"]["sumTime"] = str((endtime - starttime).seconds) + "秒"
                    # report(go.info)
        if readInfo(PATH("./info/sumInfo.pickle")) <= 0:
            print(readInfo(PATH("./info/info.pickle")))
            report(readInfo(PATH("./info/info.pickle")))
            # subprocess.Popen("taskkill /f /t /im adb.exe", shell=True)
            shutil.rmtree((PATH("./info/")))  # 删除持久化目录
            path = os.path.abspath('./')
            print("\n" + '{0:*^79}'.format('测试完成!') +
                  '\n{0:*^79}'.format('测试报告:' + path))
    except:
        print('>>>Stopping monkey process...')
        stop_monkey(devices)
Esempio n. 2
0
def start(devicess):
    devices = devicess["devices"]
    num = devicess["num"]
    app = {}
    mkdirInit(devices, app, num)
    mc = BaseMonkeyConfig.monkeyConfig(PATH("monkey.ini"))
    # 打开想要的activity
    # ba.open_app(mc["package_name"], mc["activity"], devices) 留着备用可以统计每次打开哪个页面的启动时间等
    # monkey开始测试
    mc["log"] = PATH("./log") + "\\" + str(uuid.uuid4())
    mc["monkey_log"] = mc["log"] + "monkey.log"
    mc["cmd"] = mc['cmd'] + mc["monkey_log"]
    start_monkey("adb -s " + devices + " shell " + mc["cmd"], mc["log"])
    time.sleep(1)
    starttime = datetime.datetime.now()
    pid = BaseMonitor.get_pid(mc["package_name"], devices)
    cpu_kel = BaseMonitor.get_cpu_kel(devices)
    beforeBattery = BaseMonitor.get_battery(devices)
    print 'start------------123'
    while True:
        with open(mc["monkey_log"], 'a+') as monkeylog:
            time.sleep(1)  # 每1秒采集检查一次
            print "star()----1"
            BaseMonitor.cpu_rate(pid, cpu_kel, devices)
            BaseMonitor.get_men(mc["package_name"], devices)
            BaseMonitor.get_fps(mc["package_name"], devices)
            print "mc vm :", mc["vm"]
            BaseMonitor.get_flow(pid, mc["vm"], devices)  # 获取流量
            BaseMonitor.get_battery(devices)
            bflag1 = (monkeylog.read().count('Monkey finished') > 0)
            bflag2 = (monkeylog.read().count('Monkey aborted due to error') >
                      0)
            if bflag1 or bflag2:
                endtime = datetime.datetime.now()
                print(str(devices) + "测试完成咯")
                writeSum(1, path=PATH("./info/sumInfo.pickle"))
                app[devices]["header"]["beforeBattery"] = beforeBattery
                app[devices]["header"][
                    "afterBattery"] = BaseMonitor.get_battery(devices)
                app[devices]["header"]["net"] = mc["net"]
                app[devices]["header"]["monkey_log"] = mc["monkey_log"]
                app[devices]["header"]["time"] = str(
                    (endtime - starttime).seconds) + "秒"
                writeInfo(app, PATH("./info/info.pickle"))
                break
                # go.info[devices]["header"]["sumTime"] = str((endtime - starttime).seconds) + "秒"
                # report(go.info)
    if readInfo(PATH("./info/sumInfo.pickle")) <= 0:
        print(readInfo(PATH("./info/info.pickle")))
        report(readInfo(PATH("./info/info.pickle")))  # 这里有问题
        subprocess.Popen("taskkill /f /t /im adb.exe", shell=True)
        # shutil.rmtree((PATH("./info/"))) # 删除持久化目录
        print("------来吧------")
Esempio n. 3
0
def start(devicess):
    devices = devicess["devices"]
    print(devices)
    num = devicess["num"]
    print(num)
    app = {}
    print(app)
    mkdirInit(devices, app, num)
    mc = BaseMonkeyConfig.monkeyConfig(PATH("monkey.ini"))
    # 打开想要的activity
    # ba.open_app(mc["package_name"], mc["activity"], devices) 留着备用可以统计每次打开哪个页面的启动时间等
    # monkey开始测试
    mc["log"] = PATH("./log") + "\\" + str(uuid.uuid4())
    mc["monkey_log"] = mc["log"] + "monkey.log"
    mc["cmd"] = mc['cmd'] + mc["monkey_log"]
    start_monkey("adb -s " + devices + " shell " + mc["cmd"], mc["log"])
    time.sleep(1)
    starttime = datetime.datetime.now()
    pid = BaseMonitor.get_pid(mc["package_name"], devices)
    cpu_kel = BaseMonitor.get_cpu_kel(devices)
    beforeBattery = BaseMonitor.get_battery(devices)
    while True:
        with open(mc["monkey_log"], encoding='utf-8') as monkeylog:
            time.sleep(1)  # 每1秒采集检查一次
            BaseMonitor.cpu_rate(pid, cpu_kel, devices)
            BaseMonitor.get_men(mc["package_name"], devices)
            BaseMonitor.get_fps(mc["package_name"], devices)
            BaseMonitor.get_flow(pid, mc["net"], devices)
            BaseMonitor.get_battery(devices)
            if monkeylog.read().count('Monkey finished') > 0:
                endtime = datetime.datetime.now()
                print(str(devices) + "测试完成咯")
                writeSum(1, path=PATH("./info/sumInfo.pickle"))
                app[devices]["header"]["beforeBattery"] = beforeBattery
                app[devices]["header"][
                    "afterBattery"] = BaseMonitor.get_battery(devices)
                app[devices]["header"]["net"] = mc["net"]
                app[devices]["header"]["monkey_log"] = mc["monkey_log"]
                app[devices]["header"]["time"] = str(
                    (endtime - starttime).seconds) + "秒"
                writeInfo(app, PATH("./info/info.pickle"))
                break
                # go.info[devices]["header"]["sumTime"] = str((endtime - starttime).seconds) + "秒"
                # report(go.info)
    if readInfo(PATH("./info/sumInfo.pickle")) <= 0:
        print(readInfo(PATH("./info/info.pickle")))
        report(readInfo(PATH("./info/info.pickle")))
        subprocess.Popen("taskkill /f /t /im adb.exe", shell=True)
Esempio n. 4
0
def start(devicess):
    devices = devicess["devices"]
    num = devicess["num"]
    app = {}
    mkdirInit(devices, app, num)
    mc = BaseMonkeyConfig.monkeyConfig(PATH("monkey.ini"))
    # 打开想要的activity
    # ba.open_app(mc["package_name"], mc["activity"], devices) 留着备用可以统计每次打开哪个页面的启动时间等
    # monkey开始测试
    mc["log"] = PATH("./log") + "\\" + str(uuid.uuid4())
    mc["monkey_log"] = mc["log"] + "monkey.log"
    mc["cmd"] = mc['cmd'] + mc["monkey_log"]
    start_monkey("adb -s " + devices + " shell " + mc["cmd"], mc["log"])
    time.sleep(1)
    starttime = datetime.datetime.now()
    pid = BaseMonitor.get_pid(mc["package_name"], devices)
    cpu_kel = BaseMonitor.get_cpu_kel(devices)
    beforeBattery = BaseMonitor.get_battery(devices)
    while True:
        with open(mc["monkey_log"], encoding='utf-8') as monkeylog:
            time.sleep(1)  # 每1秒采集检查一次
            BaseMonitor.cpu_rate(pid, cpu_kel, devices)
            BaseMonitor.get_men(mc["package_name"], devices)
            BaseMonitor.get_fps(mc["package_name"], devices)
            BaseMonitor.get_flow(pid, mc["net"], devices)
            BaseMonitor.get_battery(devices)
            if monkeylog.read().count('Monkey finished') > 0:
                endtime = datetime.datetime.now()
                print(str(devices)+"测试完成咯")
                writeSum(1, path=PATH("./info/sumInfo.pickle"))
                app[devices] ["header"]["beforeBattery"] = beforeBattery
                app[devices]["header"]["afterBattery"] = BaseMonitor.get_battery(devices)
                app[devices]["header"]["net"] = mc["net"]
                app[devices]["header"]["monkey_log"] = mc["monkey_log"]
                app[devices]["header"]["time"] = str((endtime - starttime).seconds) + "秒"
                writeInfo(app, PATH("./info/info.pickle"))
                break
                    # go.info[devices]["header"]["sumTime"] = str((endtime - starttime).seconds) + "秒"
                    # report(go.info)
    if readInfo(PATH("./info/sumInfo.pickle")) <= 0:
        print(readInfo(PATH("./info/info.pickle")))
        report(readInfo(PATH("./info/info.pickle")))
        subprocess.Popen("taskkill /f /t /im adb.exe", shell=True)
Esempio n. 5
0
def start(devicess):
    devices = devicess["devices"]
    num = devicess["num"]
    app = {}
    mkdirInit(devices, app, num)
    mc = BaseMonkeyConfig.monkeyConfig(PATH("monkey.ini"))
    now = datetime.datetime.now().strftime('%b-%d-%Y-%H-%M-%S')
    mc["log"] = PATH("./log") + "\\" + now
    mc["monkey_log"] = mc["log"] + "monkey.log"
    mc["cmd"] = mc['cmd'] + mc["monkey_log"]
    start_monkey("adb -s " + devices + " shell " + mc["cmd"], mc["log"])
    time.sleep(1)
    starttime = datetime.datetime.now()
    pid = BaseMonitor.get_pid(mc["package_name"], devices)
    cpu_kel = BaseMonitor.get_cpu_kel(devices)
    beforeBattery = BaseMonitor.get_battery(devices)
    while True:
        with open(mc["monkey_log"], encoding='utf-8') as monkeylog:
            time.sleep(1)  # 每1秒采集检查一次
            BaseMonitor.cpu_rate(pid, cpu_kel, devices)
            BaseMonitor.get_men(mc["package_name"], devices)
            BaseMonitor.get_fps(mc["package_name"], devices)
            BaseMonitor.get_battery(devices)
            if monkeylog.read().count('Monkey finished') > 0:
                endtime = datetime.datetime.now()
                print(str(devices) + "测试完成")
                writeSum(1, path=PATH("./info/sumInfo.pickle"))
                app[devices]["header"]["beforeBattery"] = beforeBattery
                app[devices]["header"][
                    "afterBattery"] = BaseMonitor.get_battery(devices)
                app[devices]["header"]["net"] = mc["net"]
                app[devices]["header"]["monkey_log"] = mc["monkey_log"]
                app[devices]["header"]["time"] = str(
                    (endtime - starttime).seconds) + "秒"
                writeInfo(app, PATH("./info/info.pickle"))
                break
    if readInfo(PATH("./info/sumInfo.pickle")) <= 0:
        print(readInfo(PATH("./info/info.pickle")))
        report(readInfo(PATH("./info/info.pickle")))
        subprocess.Popen("taskkill /f /t /im adb.exe", shell=True)
Esempio n. 6
0
def start(dev):
    rt = os.popen('adb devices').readlines()  # os.popen()执行系统命令并返回执行后的结果
    num = len(rt) - 2
    print(num)
    app = {}
    Create_pickle(dev, app, num)
    """os.popen("adb kill-server")
    os.popen("adb start-server")
    time.sleep(5)"""

    #手动测试部分(手动测试性能数据统计),直接杀掉app进程即可结束测试统计(备注:操作过程中请不要杀掉app进程,否则测试终止)
    """signal = input("现在是手动测试部分,是否要开始你的测试,请输入(y or n): ")
    if signal == 'y':
        print("测试即将开始,请打开需要测试的app并准备执行您的操作....")
        time.sleep(5)
        path_log = Config.log_location + dev
        device_dir = os.path.exists(path_log)
        if device_dir:
            print("日志文件目录log已存在,继续执行测试!")
        else:
            os.mkdir(path_log)  # 按设备ID生成日志目录文件夹
        run_time = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))

        # logcat日志
        logcat_log = path_log + "\\" + run_time + "logcat.log"
        cmd_logcat = "adb -s " + dev + " logcat > %s" % (logcat_log)
        os.popen(cmd_logcat)
        # print("logcat 命令:", cmd_logcat)

        # "导出traces文件"
        traces_log = path_log + "\\" + run_time + "traces.log"
        cmd_traces = "adb -s " + dev + " shell cat /data/anr/traces.txt > %s" % traces_log
        os.popen(cmd_traces)
        # print("traces 命令:", cmd_traces)

        time.sleep(2)
        start_time = datetime.datetime.now()
        pid = BaseMonitor.get_pid(Config.package_name, dev)
        cpu_kel = BaseMonitor.get_cpu_kel(dev)
        beforeBattery = BaseMonitor.get_battery(dev)
        print("测试前电量", beforeBattery)
        try:
            while True:
                time.sleep(1)  # 每1秒采集一次
                print("----------------数据采集-----------------")
                BaseMonitor.cpu_rate(pid, cpu_kel, dev)
                BaseMonitor.get_men(Config.package_name, dev)
                BaseMonitor.get_fps(Config.package_name, dev)
                BaseMonitor.get_flow(pid, Config.net, dev)
                BaseMonitor.get_battery(dev)
        except:
            end_time = datetime.datetime.now()
            print(str(dev) + ":测试完成!")
            afterBattery = BaseMonitor.get_battery(dev)
            writeSum(1, path=PATH("./info/sumInfo.pickle"))
            app[dev]["header"]["beforeBattery"] = beforeBattery
            app[dev]["header"]["afterBattery"] = afterBattery
            app[dev]["header"]["net"] = Config.net
            app[dev]["header"]["time"] = str((end_time - start_time).seconds) + "秒"
            writeInfo(app, PATH("./info/info.pickle"))
    if readInfo(PATH("./info/sumInfo.pickle")) <= 0:
        report(readInfo(PATH("./info/info.pickle")))
        print("Kill adb server,test finished!")
        os.popen("taskkill /f /t /im adb.exe")
        shutil.rmtree((PATH("./info/"))) # 删除持久化目录

    elif signal == 'n':
        print('用户主动放弃测试,测试结束!')
    else:
        print("测试结束,输入非法,请重新输入y or n!")"""

    #Monkey测试部分,如果是进行monkey测试,去除该部分注释(line134~line191)并注掉手动测试部分(line72~line131);另外BaseReport.py中line85,line120~line132注释也要去除
    print("--------------开始执行Monkey----------------")
    path_log = Config.log_location + dev
    device_dir = os.path.exists(path_log)
    if device_dir:
        print("日志文件目录log已存在,继续执行测试!")
    else:
        os.mkdir(path_log)  # 按设备ID生成日志目录文件夹
    run_time = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))
    # Monkey测试结果日志:monkey_log
    adb_monkey = "shell monkey -p %s -s %s %s" % (
        Config.package_name, Config.monkey_seed, Config.monkey_parameters)
    monkey_log = path_log + "\\" + run_time + "monkey.log"
    cmd_monkey = "adb -s %s %s > %s" % (dev, adb_monkey, monkey_log)
    os.popen(cmd_monkey)
    print("monkey 命令:", cmd_monkey)

    # logcat日志
    logcat_log = path_log + "\\" + run_time + "logcat.log"
    cmd_logcat = "adb -s " + dev + " logcat > %s" % (logcat_log)
    os.popen(cmd_logcat)
    # print("logcat 命令:", cmd_logcat)

    # "导出traces文件"
    traces_log = path_log + "\\" + run_time + "traces.log"
    cmd_traces = "adb -s " + dev + " shell cat /data/anr/traces.txt > %s" % traces_log
    os.popen(cmd_traces)
    # print("traces 命令:", cmd_traces)
    time.sleep(2)
    start_time = datetime.datetime.now()
    pid = BaseMonitor.get_pid(Config.package_name, dev)
    cpu_kel = BaseMonitor.get_cpu_kel(dev)
    beforeBattery = BaseMonitor.get_battery(dev)
    print("测试前电量", beforeBattery)
    while True:
        try:
            with open(monkey_log, encoding='utf-8') as monkeylog:
                time.sleep(2)  # 每2秒采集一次
                print("----------------数据采集-----------------")
                BaseMonitor.cpu_rate(pid, cpu_kel, dev)
                BaseMonitor.get_men(Config.package_name, dev)
                BaseMonitor.get_fps(Config.package_name, dev)
                BaseMonitor.get_flow(pid, Config.net, dev)
                BaseMonitor.get_battery(dev)
                if monkeylog.read().count('Monkey finished') > 0:
                    end_time = datetime.datetime.now()
                    print(str(dev) + ":测试完成!")
                    afterBattery = BaseMonitor.get_battery(dev)
                    writeSum(1, path=PATH("./info/sumInfo.pickle"))
                    app[dev]["header"]["beforeBattery"] = beforeBattery
                    app[dev]["header"]["afterBattery"] = afterBattery
                    app[dev]["header"]["net"] = Config.net
                    app[dev]["header"]["monkey_log"] = monkey_log
                    app[dev]["header"]["time"] = str(
                        (end_time - start_time).seconds) + "秒"
                    writeInfo(app, PATH("./info/info.pickle"))
                    break
        except:
            end_time = datetime.datetime.now()
            print(str(dev) + ":测试完成!")
            afterBattery = BaseMonitor.get_battery(dev)
            writeSum(1, path=PATH("./info/sumInfo.pickle"))
            app[dev]["header"]["beforeBattery"] = beforeBattery
            app[dev]["header"]["afterBattery"] = afterBattery
            app[dev]["header"]["net"] = Config.net
            app[dev]["header"]["monkey_log"] = monkey_log
            app[dev]["header"]["time"] = str(
                (end_time - start_time).seconds) + "秒"
            writeInfo(app, PATH("./info/info.pickle"))
            break
    if readInfo(PATH("./info/sumInfo.pickle")) <= 0:
        report(readInfo(PATH("./info/info.pickle")))
        print("Kill adb server,test finished!")
        os.popen("taskkill /f /t /im adb.exe")
        shutil.rmtree((PATH("./info/")))  # 删除持久化目录