实现淘宝小程序内存分析工具
前置条件:
- 安卓手机连接adb(可用adb devices命令调试)
- 启动脚本
操作步骤:
- 首先,通过adb命令获取安卓手机内存数据,存入到列表中
- 然后,通过新开一个子线程实时监控q键
- 最后,按下q键退出,生成测试报告(render.html)
import threading
from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import os
import re
from collections import defaultdict
import keyboard
class analyse:
def __init__(self):
self.datas = defaultdict(list)
self.i=0
self.switch = False
#获取内存数据
def getTotalPss(self):
li = os.popen(u"adb shell \"dumpsys meminfo | grep 'taobao'\" ").readlines()
for line in li:
a = re.search('(\d+,\d+)K:(.*)', line)
if a:
self.datas[a.group(2)].append(int(int(a.group(1).replace(',',''))/1024))
else:
print('未匹配到元素')
def run(self):
while True:
try:
if keyboard.is_pressed('q'):
self.switch =True
break
except:
print('异常')
break
#新开一个子线程,监听q键
def test(self):
t = threading.Thread(target=self.run)
t.start()
while True:
if self.switch:
break
self.getTotalPss()
self.i += 1
print(self.i)
self.report()
#生成分析报告
def report(self):
len1 = [len(value) for key,value in self.datas.items()]
maxlen = int(max(len1)*1.5)
bar = Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
bar.add_xaxis([i for i in range(maxlen)])
for key,value in self.datas.items():
bar.add_yaxis(f"{key}",
value,
label_opts=opts.LabelOpts(is_show=False)
)
bar.set_global_opts(
title_opts=opts.TitleOpts(title="小程序内存分析"),
legend_opts=opts.LegendOpts(type_="plain",
pos_left="right",
orient="vertical",
item_width=10,
item_height=5,
legend_icon='pin',
textstyle_opts=opts.TextStyleOpts(font_size=8)
),
)
bar.render()
if __name__ == '__main__':
c = analyse()
c.test()
实现结果
如果遇到内容无法加载的情况:
<meta charset="UTF-8">
<title>Awesome-pyecharts</title>
//把远程访问的地址修改成本地地址
<script type="text/javascript" src="render.js"></script>