Android性能测试-应用程序内存测试一、从顶部获取内存值并生成内存趋势
1.如何获取内存的价值,请参阅我之前写的文章
2.在了解如何获取内存值的基础上,我们可以在测试应用程序的整个过程中记录应用程序的内存使用情况,并将其写入文件中,例如使用以下说明:
#1秒输出一次top使用情况,输出使用量排名前5的应用的情况
adb shell top -d 1 > D:\DailyWork\top.log
例如,在测试期间,记录的头文件的内容如下


您可以使用脚本来处理所需的内容,例如,提取com.android.systemui的cpu和RSS用法,以python脚本为例:
def process_log(self):
"""
处理输入的日志
:return: 是否处理成功
"""
try:
process_name = com.android.systemui
with open(D:/DailyWork/top.log, r+, encoding=utf-8, errors=ignore) as f:
file_director = D:/DailyWork/result/
if not os.path.isdir(file_director):
os.makedirs(file_director)
try:
f_memory = open(file_director + / + process_name + .mem.txt, w+)
f_cpu = open(file_director + / + process_name + .cpu.txt, w+)
# 控制应用的
system_mem_cpu_line_number = 0
process_mem_cpu_line_number = 0
# 按行读文件,遇到相应应用的信息,则进行处理
for line in f.readlines():
if (User in line) and (System in line):
system_mem_cpu_line_number = system_mem_cpu_line_number + 1
if process_name in line:
process_mem_cpu_line_number = process_mem_cpu_line_number + 1
while process_mem_cpu_line_number < system_mem_cpu_line_number:
process_mem_cpu_line_number = process_mem_cpu_line_number + 1
f_cpu.write(0\n)
f_memory.write(0\n)
tmp = line.split()
f_cpu.write(self.replace_str(tmp[2]) + \n) # cpu值的位置可以自行确定,不一定是第二列
f_memory.write(self.replace_str(tmp[6]) + \n)
# 如果进程从未启动,也写入0值至文件夹
while process_mem_cpu_line_number < system_mem_cpu_line_number:
process_mem_cpu_line_number = process_mem_cpu_line_number + 1
f_cpu.write(0\n)
f_memory.write(0\n)
f_memory.flush()
f_memory.close()
f_cpu.flush()
f_cpu.close()
except IOError as err:
print("File write error:" + str(err))
return False
except IOError as err:
print(File read error: + str(err))
return False
return True
处理后获得的内存值如下:


如果获得该值,则可以在excel中自己生成趋势图,观察内存使用情况,或使用python脚本生成趋势图
import matplotlib
# 画折线图
def draw_line_img(xlabel, ylabel, xticks, y_tuple_list, img_path, figure_size):
plot.figure(figsize=figure_size, frameon=False)
plot.xlabel(xlabel)
plot.ylabel(ylabel)
for label, y_list, line_color in y_tuple_list:
plot.plot(tuple(range(len(xticks))), y_list, line_color, label=label)
plot.legend(fontsize=10)
plot.margins(0.08)
plot.savefig(img_path)
plot.close()
x_mem = []
y_mem = []
with open(D:/DailyWork/result/com.android.systemui.mem.txt, r+, encoding=utf-8, errors=ignore) as f:
x_index = 0
for line in f.readlines():
x_index = x_index + 1
x_mem.append(x_index)
y_mem.append(float(line) / 1024) # 转换成MB
draw_line_img(u时间轴, uMem使用情况(MB), x_mem, [("MEM", y_mem, lightskyblue)], "D:/DailyWork/result/mem_line.png", (15, 5))
生成的折线图如下:

从折线图中可以初步确定是否存在明显的内存泄漏。
对于可疑的内存泄漏,可以使用leakCanary进行检测。后续文章中将介绍使用leakCanary。
二、通过Android Studio的Android Profiler获取实时内存值
实时内存和CPU测试,您可以通过Android Studio工具获取实时内存使用情况。
打开Android Studio,在导航栏中找到视图,然后找到Android Profiler将其打开。将设备连接到计算机后,即可开始获取实时内存使用情况。
有关使用Android Profiler的信息,请参阅Android Studio开发文档:
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shoujiruanjian/article-369342-1.html
卖假货的商家立案稽查了没有
有利益就有冲突
还有心情在对中国说三道四