Android程序调用python脚本,Python脚本实现Android App资源监控

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值