1. 实现原理

实现android App资源监控,我们是通过python脚本调用adb命令,然后把adb返回的数据保存到csv文件中,然后按时间生成cpu,内存,电量,流量的曲线图。

141b84f14505?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weixin

2. ADB命令实现资源监控

adb查看cpu的资源占用

adb shell dumpsys cpuinfo | findstr com.xxx.abc(App的pcakageName)

执行结果

141b84f14505?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weixin

注意:第一列的“26%”即为当前App所占用cpu的

adb查看内存的资源占用

获取app进程ID

adb shell ps | findstr com.xxx.abc(App的pcakageName)

查看App内存的占用

adb shell top -n 1 -d 0.5 | findstr pid(进程ID)

执行结果

141b84f14505?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weixin

注意:获取到内存信息的单位是K,其中"2348108K"是虚存,“188564K”是实存,通常我们会分析实存。

adb查看流量的使用

获取app进程ID

adb shell ps | findstr com.xxx.abc(App的pcakageName)

查看Android 设备的网卡信息

adb shell netcfg

查看流量

adb shell cat /proc/ pid /net/dev | findstr wlan0

注意:pid为app的进程ID,wlan0为网卡

执行结果

141b84f14505?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weixin

注意:流量获取的单位为b,其中“62259050”是下行流量,“1153642”为上行流量

adb查看电量的使用

设置手机为非充电状态

adb shell dumpsys battery set status 1

查看电量的使用情况

adb shell dumpsys battery |findstr level

执行结果

141b84f14505?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weixin

3. Python代码实现

python执行adb命令

执行adb命令

result = os.popen("adb shell dumpsys cpuinfo | findstr com.xxx.abc")

获取结果

cpuLine =result.readline().split("%")[0].strip()

python写CSV文件

def saveDataToCSV(self):

csvfile = open(self.filePath, 'a',encoding='utf8',newline='')

writer = csv.writer(csvfile)

writer.writerows(self.alldata)

csvfile.close()

完整的监控CPU的实例

# /usr/bin/python

# encoding:utf-8

import csv

import os

import time

# 监控CPU资源信息

class MonitoringCPUResources(object):

def __init__(self, count):

self.counter = count

self.alldata = [("timestamp", "cpustatus")]

# 单次执行监控过程

def monitoring(self):

result = os.popen("adb shell dumpsys cpuinfo | findstr com.tencent.mm")

cpuvalue = result.readline().split("%")[0].strip()

currenttime = self.getCurrentTime()

print("current time is:"+currenttime)

print("cpu used is:" + cpuvalue)

self.alldata.append([currenttime, cpuvalue])

# 多次执行监控过程

def run(self):

while self.counter > 0:

self.monitoring()

self.counter = self.counter - 1

time.sleep(3)

# 获取当前的时间戳

def getCurrentTime(self):

currentTime = time.strftime("%H:%M:%S", time.localtime())

return currentTime

# 数据的存储

def SaveDataToCSV(self):

csvfile = open('cpustatus.csv', 'w',encoding='utf8',newline='')

writer = csv.writer(csvfile)

writer.writerows(self.alldata)

csvfile.close()

if __name__ == "__main__":

monitoringCPUResources = MonitoringCPUResources(20)

monitoringCPUResources.run()

monitoringCPUResources.SaveDataToCSV()

生成曲线图

141b84f14505?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weixin

image.png

到这里想必大家都已经清楚,剩余的内存,电量,流量的监控来交给你们自己来完成

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐