基于 (python-monkey 压测监控 app)[https://testerhome.com/topics/5662]
最开始是看到了蛇的文章来学习的 https://testerhome.com/topics/2514 找了好久才找到。。。

代码分析

model 层

用的 schematics,不用自己写 get,set 了


from schematics.models import Model
from schematics.types import StringType,IntType
class monkeyconfig(Model):
    cmd = StringType()
    package_name = StringType()
    logdir = StringType() #本机的log存放地址
    remote_path = StringType()  #远程服务器地址,可以给开发查看
    phone_msg_log = StringType() #临时存放手机日志信息路径
    now = StringType()
    exceptions = StringType() #异常列表监控
    sum = StringType() #事件数量
    activity = StringType()
    monkey_log = StringType()

DAL 层

import configparser
import time
def monkeyConfig(mmonkeyconfig, init_file):
    config = configparser.ConfigParser()
    config.read(init_file)
    mmonkeyconfig.package_name = config['DEFAULT']['package_name']
    mmonkeyconfig.logdir = config['DEFAULT']['logdir']
    mmonkeyconfig.remote_path = config['DEFAULT']['remote_path']
    mmonkeyconfig.phone_msg_log = config['DEFAULT']['phone_msg_log']
    mmonkeyconfig.now = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
    mmonkeyconfig.activity = config['DEFAULT']['activity']
    mmonkeyconfig.sum = int(config['DEFAULT']['sum'])
    mmonkeyconfig.monkey_log = mmonkeyconfig.logdir + "\\" + mmonkeyconfig.now + r"monkey.log"
    mmonkeyconfig.cmd = config['DEFAULT']['cmd'] + " " + str(mmonkeyconfig.sum) + ">>" + mmonkeyconfig.monkey_log
    return mmonkeyconfig

BLL 层

from DAL import DMonkeyConfig
def monkeyConfig(mmonkeyconfig, init_file):
    return DMonkeyConfig.monkeyConfig(mmonkeyconfig, init_file)

UI 层


# 只调用BLL层的东西
from BLL import BMatPlo
from BLL import BAdbCommon
from Common import OperateFile
from BLL import BMonkeyConfig
from Model import MMonkeyConfig
from Model import MMatplo
from BLL import BMenCpu

 ba = BAdbCommon
mconfig = MMonkeyConfig.monkeyconfig()
mc = BMonkeyConfig.monkeyConfig(mconfig, ini_file)

# 下面就可以取实体类的值
mc.cmd, mc.logdir, mc.now, mc.monkey_log

入口代码

import os
import time
import datetime as dt
from BLL import BMatPlo
from BLL import BAdbCommon
from Common import OperateFile
from BLL import BMonkeyConfig
from Model import MMonkeyConfig
from Model import MMatplo
from BLL import BMenCpu

#开始脚本测试
def start_monkey(cmd, logdir, now1, monkey_log):
    print(cmd)
    os.popen(cmd)
    cmd2 = "adb logcat -d >%s" % monkey_log
    os.popen(cmd2)
    # logcatname=logdir+"\\"+now1+r"logcat.log"
    # cmd2="adb logcat -d >%s" %(logcatname)
    # os.popen(cmd2) 用于error的分析
    #print"导出traces文件"
    tracesname = logdir + "\\" + now1 + r"traces.log"
    cmd3 = "adb shell cat /data/anr/traces.txt>%s" % tracesname
    os.popen(cmd3)
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, mc.monkey_log)

            # cpu,men统计
            ml = MMatplo.matplo()
            bm = BMenCpu.get_men_cpu(mc.package_name)
            ml.cpu = [[], []]
            ml.men = [[], []]
            ml.title = ["cpu测试", "内存测试"]
            ml.locator = 2
            for i in range(mc.sum):
                time.sleep(1)
                dn = dt.datetime.now()
                ml.cpu[0].append(dn)
                cpu = bm.top_cpu()
                ml.men[0].append(dn)
                men = bm.get_men()
                with open(mc.monkey_log, encoding='utf-8') as monkeylog:
                    temp = monkeylog.read()
                if temp.count('Monkey finished') > 0:
                    print("测试完成咯")
                    ml.cpu[1].append(cpu)
                    ml.men[1].append(men)
                    BMatPlo.cpu_men_plots(ml)
                    break
        else:
            print("设备不存在")
    else:
        print(u"配置文件不存在"+ini_file)


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