目录
前言:
monkey 压力测试 android
monkey.ini 配置文件
代码分析
前言:
在软件开发中,测试和监控是非常重要的一个环节,它可以帮助我们更加全面地检测软件中的安全漏洞和风险。Python 是一种常用的脚本语言,可以帮助我们更加方便地进行测试和监控工作。Monkey 是 Python 中的一个模块,可以帮助我们进行自动化测试和压力测试。
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.haruki
activity = com.baiji.jianshu.account.SplashScreenActivity
net = wifi
- throttle 每次事件等待 500 毫秒
- net 支持 gprs 和 wifi
代码分析
主要监控代码
def get_cpu(pkg_name):
cmd = "adb shell 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()[1].decode().split("/")[1][:-1] == pkg_name: # 只有包名相等
# print("cpu=" + info.split()[2].decode())
cpu.append(float(info.split()[2].decode().split("%")[0]))
print("----cpu-----")
print(cpu)
return cpu
def get_men(pkg_name):
cmd = "adb shell dumpsys meminfo %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()[0].decode() == "TOTAL":
# print("men="+info.split()[1].decode())
men.append(int(info.split()[1].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 = [x for x in results.split('\n') if validator(x)]
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[0]) + float(time_block[1]) + float(time_block[2])
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()[0].decode() == "level:":
battery.append(int(info.split()[1].decode()))
print("-----battery------")
print(battery)
return int(info.split()[1].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()[8].decode() == pkg_name:
return item.split()[1].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()[0].decode() == "wlan0:": # wifi
# 0 上传流量,1 下载流量
flow[0].append(int(item.split()[1].decode()))
flow[1].append(int(item.split()[9].decode()))
print("------flow---------")
print(flow)
return flow
if type == "gprs" and item.split()[0].decode() == "rmnet0:": # gprs
print("--------------")
flow[0].append(int(item.split()[1].decode()))
flow[1].append(int(item.split()[9].decode()))
return flow
else:
flow[0].append(0)
flow[1].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()
作为一位过来人也是希望大家少走一些弯路
在这里我给大家分享一些自动化测试前进之路的必须品,希望能对你带来帮助。
(软件测试相关资料,自动化测试相关资料,技术问题答疑等等)
相信能使你更好的进步!
点击下方小卡片