51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3138|回复: 5

[python] 性能测试工具 python+monkey+ 监控 crash,性能统计

[复制链接]
  • TA的每日心情
    奋斗
    2015-8-28 12:55
  • 签到天数: 29 天

    连续签到: 1 天

    [LV.4]测试营长

    发表于 2017-9-13 16:33:16 | 显示全部楼层 |阅读模式
    monkey 压力测试android
    • python3
    • 统计性能信息cpu,men,fps,battery,flow
    • 支持wifi,gprs统计
    • 统计crash信息
    • [color=rgb(0, 105, 214) !important]查看源码
    monkey.ini 配置文件cmd=adb shell monkey -p com.jianshu.haruki --throttle 500 --ignore-timeouts --ignore-crashes   --monitor-native-crashes -v -v -v 200 >package_name=com.jianshu.harukiactivity = com.baiji.jianshu.account.SplashScreenActivitynet = wifi
    • throttle 每次事件等待500毫秒
    • net 支持gprs和wifi

    [color=rgb(0, 105, 214) !important]



    [color=rgb(0, 105, 214) !important]



    [color=rgb(0, 105, 214) !important]


    代码分析

    主要监控代码

    1. def get_cpu(pkg_name):
    2.     cmd = "adb  shell dumpsys cpuinfo | findstr " + pkg_name
    3.     print(cmd)
    4.     output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
    5.     for info in output:
    6.         if info.split()[1].decode().split("/")[1][:-1] == pkg_name:  # 只有包名相等
    7.             # print("cpu=" + info.split()[2].decode())
    8.             cpu.append(float(info.split()[2].decode().split("%")[0]))
    9.             print("----cpu-----")
    10.             print(cpu)
    11.             return cpu


    12. def get_men(pkg_name):
    13.     cmd = "adb shell  dumpsys  meminfo %s" % (pkg_name)
    14.     print(cmd)
    15.     men_s = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
    16.     for info in men_s:
    17.         if len(info.split()) and info.split()[0].decode() == "TOTAL":
    18.             # print("men="+info.split()[1].decode())
    19.             men.append(int(info.split()[1].decode()))
    20.             print("----men----")
    21.             print(men)
    22.             return men


    23. # 得到fps
    24. '''
    25. @author fenfenzhong
    26. '''


    27. def get_fps(pkg_name):
    28.     _adb = "adb shell dumpsys gfxinfo %s" % pkg_name
    29.     print(_adb)
    30.     results = os.popen(_adb).read().strip()
    31.     frames = [x for x in results.split('\n') if validator(x)]
    32.     frame_count = len(frames)
    33.     jank_count = 0
    34.     vsync_overtime = 0
    35.     render_time = 0
    36.     for frame in frames:
    37.         time_block = re.split(r'\s+', frame.strip())
    38.         if len(time_block) == 3:
    39.             try:
    40.                 render_time = float(time_block[0]) + float(time_block[1]) + float(time_block[2])
    41.             except Exception as e:
    42.                 render_time = 0


    43.         if render_time > 16.67:
    44.             jank_count += 1
    45.             if render_time % 16.67 == 0:
    46.                 vsync_overtime += int(render_time / 16.67) - 1
    47.             else:
    48.                 vsync_overtime += int(render_time / 16.67)

    49.     _fps = int(frame_count * 60 / (frame_count + vsync_overtime))
    50.     fps.append(_fps)
    51.     # return (frame_count, jank_count, fps)
    52.     print("-----fps------")
    53.     print(fps)
    54.     return fps


    55. def get_battery():
    56.     _batter = subprocess.Popen("adb shell dumpsys battery", shell=True, stdout=subprocess.PIPE,
    57.                                stderr=subprocess.PIPE).stdout.readlines()
    58.     for info in _batter:
    59.         if info.split()[0].decode() == "level:":
    60.             battery.append(int(info.split()[1].decode()))
    61.             print("-----battery------")
    62.             print(battery)
    63.             return int(info.split()[1].decode())


    64. def get_pid(pkg_name):
    65.     pid = subprocess.Popen("adb shell ps | findstr " + pkg_name, shell=True, stdout=subprocess.PIPE,
    66.                            stderr=subprocess.PIPE).stdout.readlines()
    67.     for item in pid:
    68.         if item.split()[8].decode() == pkg_name:
    69.             return item.split()[1].decode()


    70. def get_flow(pkg_name, type):
    71.     pid = get_pid(pkg_name)
    72.     if pid is not None:
    73.         _flow = subprocess.Popen("adb shell cat /proc/" + pid + "/net/dev", shell=True, stdout=subprocess.PIPE,
    74.                                  stderr=subprocess.PIPE).stdout.readlines()
    75.         for item in _flow:
    76.             if type == "wifi" and item.split()[0].decode() == "wlan0:":  # wifi
    77.                 # 0 上传流量,1 下载流量
    78.                 flow[0].append(int(item.split()[1].decode()))
    79.                 flow[1].append(int(item.split()[9].decode()))
    80.                 print("------flow---------")
    81.                 print(flow)
    82.                 return flow
    83.             if type == "gprs" and item.split()[0].decode() == "rmnet0:":  # gprs
    84.                 print("--------------")
    85.                 flow[0].append(int(item.split()[1].decode()))
    86.                 flow[1].append(int(item.split()[9].decode()))
    87.                 return flow
    88.     else:
    89.         flow[0].append(0)
    90.         flow[1].append(0)
    91.         return flow

    92. 代码入口:
    93. if ba.attached_devices():
    94.        mc = BaseMonkeyConfig.monkeyConfig(PATH("monkey.ini"))
    95.        # 打开想要的activity
    96.        ba.open_app(mc["package_name"], mc["activity"])
    97.        temp = ""
    98.         # monkey开始测试
    99.        start_monkey(mc["cmd"], mc["log"])
    100.        time.sleep(1)
    101.        starttime = datetime.datetime.now()
    102.        while True:
    103.            with open(mc["monkey_log"], encoding='utf-8') as monkeylog:
    104.                BaseMonitor.get_cpu(mc["package_name"])
    105.                BaseMonitor.get_men(mc["package_name"])
    106.                BaseMonitor.get_fps(mc["package_name"])
    107.                BaseMonitor.get_battery()
    108.                BaseMonitor.get_flow(mc["package_name"], mc["net"])
    109.                time.sleep(1) # 每1秒采集检查一次
    110.                if monkeylog.read().count('Monkey finished') > 0:
    111.                    endtime = datetime.datetime.now()
    112.                    print("测试完成咯")
    113.                    app = {"beforeBattery": BaseMonitor.get_battery(), "net": mc["net"], "monkey_log": mc["monkey_log"]}
    114.                    report(app, str((endtime - starttime).seconds) + "秒")
    115.                    bo.close()
    复制代码
    2.0 版本更新

    • 优化了统计性能数据的代码
    • 支持多设备
    • 用了持久化记录信息,删除全局变量统计性能信息

    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2017-11-26 18:46
  • 签到天数: 382 天

    连续签到: 1 天

    [LV.9]测试副司令

    发表于 2017-9-13 17:50:57 | 显示全部楼层
    虽然看不懂,但觉得很腻害!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-2-8 08:40
  • 签到天数: 173 天

    连续签到: 2 天

    [LV.7]测试师长

    发表于 2017-9-14 10:05:40 | 显示全部楼层
    不错不错,支持!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2017-11-7 08:35
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    发表于 2017-11-5 13:47:44 | 显示全部楼层
    玩 monkey的高手,人外有人呀
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-3-29 03:52 , Processed in 0.067376 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表