Python脚本实现Android App资源监控

1. 实现原理

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


2. ADB命令实现资源监控

adb查看cpu的资源占用

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

执行结果

注意:第一列的“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)

执行结果

注意:获取到内存信息的单位是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为网卡

执行结果

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

adb查看电量的使用
设置手机为非充电状态

adb shell dumpsys battery set status 1

查看电量的使用情况

adb shell dumpsys battery |findstr level

执行结果

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()
生成曲线图
image.png
到这里想必大家都已经清楚,剩余的内存,电量,流量的监控来交给你们自己来完成
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android开发中我们有时候需要借助一些命令帮助更好的高效率定位解决问题,本文就来介绍一些可能有些隐藏的而却非常...
    passiontim阅读 1,488评论 0 4
  • 一、查看当前显示的Activitylinux:adb shell dumpsys activity | grep ...
    PrivateGO阅读 2,100评论 0 0
  • adb:Android 调试桥接 Android Debug Bridge,是一个 C/S 架构的命令行工具 a...
    LLd_阅读 3,112评论 0 3
  • 本片博文讲详细讲解搭建过程,经历鉴于我搭建托管博客所遇到的错误!为了避免读者重蹈覆辙。我将尽量详细,和一些遇到的错...
    喜欢雨天的我阅读 1,176评论 1 2
  • 该用方块切割回忆 还是任这情绪揉碎在旋律里 仿佛拥有你 小憩在我膝头的人 太阳烧红的耳根 微风倾诉着眷恋 我低下头...
    大写的张十五阅读 265评论 0 1