性能测试工具 python+monkey+ 监控 crash,性能统计
monkey 压力测试android[*]python3
[*]统计性能信息cpu,men,fps,battery,flow
[*]支持wifi,gprs统计
[*]统计crash信息
[*]查看源码
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
https://testerhome.com/uploads/photo/2017/d008ea38-2719-4079-9699-ec94a03e2ff3.png!large
https://testerhome.com/uploads/photo/2017/5fdc9ec3-597f-44e7-b1cf-ddde850f631f.png!large
https://testerhome.com/uploads/photo/2017/6acdb4e0-3521-4971-b742-34360b8fdbdd.png!large
代码分析主要监控代码def get_cpu(pkg_name):
cmd = "adbshell dumpsys cpuinfo | findstr " + pkg_name
print(cmd)
output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
for info in output:
if info.split().decode().split("/")[:-1] == pkg_name:# 只有包名相等
# print("cpu=" + info.split().decode())
cpu.append(float(info.split().decode().split("%")))
print("----cpu-----")
print(cpu)
return cpu
def get_men(pkg_name):
cmd = "adb shelldumpsysmeminfo %s" % (pkg_name)
print(cmd)
men_s = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
for info in men_s:
if len(info.split()) and info.split().decode() == "TOTAL":
# print("men="+info.split().decode())
men.append(int(info.split().decode()))
print("----men----")
print(men)
return men
# 得到fps
'''
@author fenfenzhong
'''
def get_fps(pkg_name):
_adb = "adb shell dumpsys gfxinfo %s" % pkg_name
print(_adb)
results = os.popen(_adb).read().strip()
frames =
frame_count = len(frames)
jank_count = 0
vsync_overtime = 0
render_time = 0
for frame in frames:
time_block = re.split(r'\s+', frame.strip())
if len(time_block) == 3:
try:
render_time = float(time_block) + float(time_block) + float(time_block)
except Exception as e:
render_time = 0
if render_time > 16.67:
jank_count += 1
if render_time % 16.67 == 0:
vsync_overtime += int(render_time / 16.67) - 1
else:
vsync_overtime += int(render_time / 16.67)
_fps = int(frame_count * 60 / (frame_count + vsync_overtime))
fps.append(_fps)
# return (frame_count, jank_count, fps)
print("-----fps------")
print(fps)
return fps
def get_battery():
_batter = subprocess.Popen("adb shell dumpsys battery", shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).stdout.readlines()
for info in _batter:
if info.split().decode() == "level:":
battery.append(int(info.split().decode()))
print("-----battery------")
print(battery)
return int(info.split().decode())
def get_pid(pkg_name):
pid = subprocess.Popen("adb shell ps | findstr " + pkg_name, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).stdout.readlines()
for item in pid:
if item.split().decode() == pkg_name:
return item.split().decode()
def get_flow(pkg_name, type):
pid = get_pid(pkg_name)
if pid is not None:
_flow = subprocess.Popen("adb shell cat /proc/" + pid + "/net/dev", shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).stdout.readlines()
for item in _flow:
if type == "wifi" and item.split().decode() == "wlan0:":# wifi
# 0 上传流量,1 下载流量
flow.append(int(item.split().decode()))
flow.append(int(item.split().decode()))
print("------flow---------")
print(flow)
return flow
if type == "gprs" and item.split().decode() == "rmnet0:":# gprs
print("--------------")
flow.append(int(item.split().decode()))
flow.append(int(item.split().decode()))
return flow
else:
flow.append(0)
flow.append(0)
return flow
代码入口:
if ba.attached_devices():
mc = BaseMonkeyConfig.monkeyConfig(PATH("monkey.ini"))
# 打开想要的activity
ba.open_app(mc["package_name"], mc["activity"])
temp = ""
# monkey开始测试
start_monkey(mc["cmd"], mc["log"])
time.sleep(1)
starttime = datetime.datetime.now()
while True:
with open(mc["monkey_log"], encoding='utf-8') as monkeylog:
BaseMonitor.get_cpu(mc["package_name"])
BaseMonitor.get_men(mc["package_name"])
BaseMonitor.get_fps(mc["package_name"])
BaseMonitor.get_battery()
BaseMonitor.get_flow(mc["package_name"], mc["net"])
time.sleep(1) # 每1秒采集检查一次
if monkeylog.read().count('Monkey finished') > 0:
endtime = datetime.datetime.now()
print("测试完成咯")
app = {"beforeBattery": BaseMonitor.get_battery(), "net": mc["net"], "monkey_log": mc["monkey_log"]}
report(app, str((endtime - starttime).seconds) + "秒")
bo.close()2.0 版本更新
[*]优化了统计性能数据的代码
[*]支持多设备
[*]用了持久化记录信息,删除全局变量统计性能信息
高大上 虽然看不懂,但觉得很腻害!:lol 不错不错,支持! 厉害! 玩 monkey的高手,人外有人呀
页:
[1]