美文网首页
Python脚本实现Android App资源监控

Python脚本实现Android App资源监控

作者: 御剑逍遥 | 来源:发表于2017-05-27 11:14 被阅读0次

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

    相关文章

      网友评论

          本文标题:Python脚本实现Android App资源监控

          本文链接:https://www.haomeiwen.com/subject/krzffxtx.html