Python获取android设备cpu和内存占用情况


Posted in Python onNovember 15, 2020

功能:获取android设备中某一个app的cpu和内存

环境:python和adb

使用方法:使用adb连接android设备,打开将要测试的app,执行cpu/内存代码

cpu获取代码如下:(输入参数为脚本执行时间)

# coding:utf-8
'''
获取系统total cpu
'''
import os, csv
import time
import csv
import numpy as np
from matplotlib import pyplot as plt

cpu_list = []
time_list = []
app_list = []
lines = []
package_name = []


# 读取进程名称(包名)
def get_applist():
  global package_name
  with open('config/director.txt', encoding='utf-8', mode='r') as f:
    lines_all = f.readlines()
    for appname in lines_all:
      package_name1 = appname
      appname_new = appname[0:15]
      package_name.append(package_name1)
      lines.append(appname_new)
    for line in lines:
      app_list.append(line.strip())


# 获取cpu数值
def get_cpu():
  global filename
  with open(filename, encoding="utf-8", mode="r") as f:
    lines = f.readlines()
    for appname in app_list:
      for lis in lines:
        # 适配低版本手机
        if appname in lis and '%' in lis:
          now = time.strftime("%H:%M:%S", time.localtime())
          time_list.append(now)
          cpu_1 = lis.split('%')[0]
          cpu_2 = cpu_1.split(' ')
          # print(cpu_2)
          cpu = cpu_2[len(cpu_2) - 1]
          print(cpu, now)
          cpu_list.append(cpu)
          break
        # 适配高版本手机
        elif appname in lis:
          now = time.strftime("%H:%M:%S", time.localtime())
          time_list.append(now)
          cpu1 = lis.split(' ')
          # print(cpu1)
          cpu2 = list(set(cpu1))
          cpu2.sort(key=cpu1.index)
          cpu_h = cpu2[len(cpu2) - 4]
          print(cpu_h, now)
          cpu_list.append(cpu_h)
          break
        else:
          pass


# csv头部
def write_head():
  headers = ['name:']
  headers.append(app_list[0])
  headers.append('init_cpu')
  with open('log_su/cpuinfo.csv', 'w+', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=headers)
    writer.writeheader()


# 将数值写入csv,用于绘图时读取
def write_report():
  # headers = ['name', 'aaa', 'init_cpu']
  with open('log_su/cpuinfo.csv', 'a+', newline='') as csvfile:
    writer = csv.writer(csvfile)
    for key in cpu_list:
      writer.writerow([' ', ' ', key])


# 绘制折线图,生成测试报告
def mapping():
  filename = 'log_su/cpuinfo.csv'
  with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    highs = []
    for row in reader:
      high = row[2]
      highs.append(high)
    # print(highs)

  wights = time_list
  highs_float = list(map(float, highs))
  # print(f"****{highs}")
  print(f"CPU值:{highs_float}")
  # 输出平均值
  total = 0
  for value in highs_float:
    total += value
  average = round(total/len(highs_float), 2)
  print(f"CPU平均值:{average}")

  #输出最低值和最高值
  highs_hl = sorted(highs_float)
  print(f"CPU最低值:{highs_hl[0]}")
  print(f"CPU最高值:{highs_hl[len(highs_hl)-1]}")

  # 根据数据绘制图形
  plt.figure(figsize=(11, 4), dpi=600)
  # 生成网格
  # plt.grid()
  plt.grid(axis="y")
  # 折线图
  if package_name[0] == 'com.oneapp.max.security.pro.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="PPP")
  elif package_name[0] == 'com.oneapp.max.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Opt1.6.1")
  elif package_name[0] == 'com.boost.clean.coin.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Fastclear")
  elif package_name[0] == 'com.walk.sports.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Walk")
  elif package_name[0] == 'com.diamond.coin.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Amber")
  elif package_name[0] == 'com.oneapp.max.cleaner.booster.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Space")
  else:
    plt.plot(wights, highs_float, "c-", linewidth=1, label=package_name[0])
  # 坐标轴范围
  # plt.ylim(300, 400)
  # plt.xlim(0, 10)

  plt.xlabel('time(H:Min:S)', fontsize=16)
  plt.ylabel("cpu_realtime(%)", fontsize=16)
  plt.title("cpu real time line chart", fontsize=24)
  plt.legend()

  # 横坐标显示间隔
  if len(wights) <= 15:
    pass
  else:
    t = int(len(wights) / 15)
    plt.xticks(range(0, len(wights), t))

  # 纵坐标显示间隔
  # plt.yticks(range(100, 300, 10))

  # 旋转日期
  plt.gcf().autofmt_xdate()

  # 展示每个坐标
  # for a, b in zip(wights, highs_float):
  #   plt.text(a, b, (a, b), ha='center', va='bottom', fontsize=8)

  # plt.show()

  time_now = time.strftime("%m%d-%H:%M:%S", time.localtime())
  path = "report/" + time_now
  plt.savefig(path)


# 自动识别当前需检测的
def name_app():
  cmd = 'adb shell dumpsys window | grep mCurrentFocus > log_su/name_info.csv'
  os.system(cmd)
  with open('log_su/name_info.csv', encoding='utf-8', mode='r') as f:
    lines = f.readlines()
    for line in lines:
      if 'mCurrentFocus' in line:
        name1 = line.split('/')[0].split(' ')
        name = name1[len(name1) - 1]

  with open('config/director.txt', encoding='utf-8', mode='w') as f_name:
    text = name
    f_name.write(text)
  print(f"将要监测的包名为:{text}")

#控制监测时间
def time_control():
  global filename
  while True:
    end_time = time.time()
    if (end_time - start_time)/60 >= tol_time:  #分钟
    # if end_time - start_time >= tol_time: # 秒
      break

    time.sleep(1)
    adb = "adb shell top -n 1 > log_su/adb_info.csv"
    d = os.system(adb)
    filename = "log_su/adb_info.csv"
    get_cpu()


if __name__ == "__main__":
  name_app()
  tol_time = int(input("请输入脚本执行时间(分钟):"))
  start_time = time.time()
  get_applist()
  write_head()
  time_control()
  write_report()
  mapping()

会在.py文件同级目录下生成3个文件夹,config、log_su、report,其中运行结果在report中

结果以是生成折线图,看起来直观,如下:

Python获取android设备cpu和内存占用情况

这里我解释下,cpu占比是adb获取的实时占比,但是满值并不一定是100%,比如这张图,用的是一个八核的手机,所以CPU满值是800%

内存获取代码如下:(输入参数为脚本执行时间)

# coding:utf-8
'''
获取系统total memory
'''
import os, csv
import time
import csv
import numpy as np
from matplotlib import pyplot as plt

mem_dict = {}
time_list = []
app_list = []
package_name = []
t = 0
def get_applist():
  global package_name
  with open('config/director.txt', encoding='utf-8', mode='r') as f:
    lines = f.readlines()
    for line in lines:
      package_name1 = line
      package_name.append(package_name1)
      app_list.append(line.strip())


def get_mem():
  global filename
  with open(filename, encoding="utf-8", mode="r") as f:
    lines = f.readlines()
    start_flag = False
    for appname in app_list:
      for line in lines:
        if "Total PSS by OOM adjustment" in line:
          break
        if appname in line and 'pid' in line and 'kB' in line:
          mem_v = line.strip().split(':')[0].replace('kB', '').replace(',', '')
          line_name = line.split(':')[1].split('(')[0].strip()
          if line_name in appname:
            mem_v = round(float(mem_v) / 1024, 2)
            mem_dict[appname] = mem_v
            now_v = time.strftime("%H:%M:%S", time.localtime())
            # now_int = int(now_v)
            time_list.append(now_v)
            print(mem_v, now_v)
            break
        elif appname in line and 'pid' in line and 'K' in line:
          mem_v = line.strip().split(':')[0].replace('K', '').replace(',', '')
          line_name = line.split(':')[1].split('(')[0].strip()
          if line_name in appname:
            mem_v = round(float(mem_v) / 1024, 2)
            mem_dict[appname] = mem_v
            now_v = time.strftime("%H:%M:%S", time.localtime())
            # now_int = int(now_v)
            time_list.append(now_v)
            print(mem_v, now_v)
            break

def write_head():
  headers = ['name:']
  headers.append(app_list[0])
  headers.append('init_mem')
  with open('log_su/meminfo.csv', 'w+', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=headers)
    writer.writeheader()

def write_report():
  headers = ['name','aaa', 'init_mem']
  with open('log_su/meminfo.csv', 'a+', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=headers)
    for key in mem_dict:
      writer.writerow({'init_mem': mem_dict[key]})


def mapping():
  filename = 'log_su/meminfo.csv'
  with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    highs = []
    for row in reader:
      high = row[2]
      highs.append(high)
    # print(highs)

  wights = time_list
  highs_float = list(map(float, highs))

  print(f"内存值:{highs_float}")

  # 输出平均值
  total = 0
  for value in highs_float:
    total += value
  average = round(total / len(highs_float), 2)
  print(f"内存平均值:{average}")

  # 输出最低值和最高值
  highs_hl = sorted(highs_float)
  print(f"内存最低值:{highs_hl[0]}")
  print(f"内存最高值:{highs_hl[len(highs_hl) - 1]}")

  # 根据数据绘制图形

  plt.figure(figsize=(11, 4), dpi=600)

  # 生成网格
  # plt.grid()
  plt.grid(axis="y")

  if package_name[0] == 'com.oneapp.max.security.pro.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="PPP")
  elif package_name[0] == 'com.oneapp.max.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Opt")
  elif package_name[0] == 'com.boost.clean.coin.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="fastclear")
  elif package_name[0] == 'com.walk.sports.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Walk")
  elif package_name[0] == 'com.diamond.coin.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Amber")
  elif package_name[0] == 'com.oneapp.max.cleaner.booster.cn':
    plt.plot(wights, highs_float, "c-", linewidth=1, label="Space")
  else:
    plt.plot(wights, highs_float, "c-", linewidth=1, label=package_name[0])
  # 坐标轴范围
  # plt.ylim(300, 400)
  # plt.xlim(0, 10)

  plt.xlabel('time(H:Min:S)', fontsize=16)
  plt.ylabel("Number (Mb)", fontsize=16)
  plt.title("meminfo", fontsize=24)
  plt.legend()

  # 横坐标显示间隔
  if len(wights) <= 15:
    pass
  else:
    t = int(len(wights) / 15)
    plt.xticks(range(0, len(wights), t))

  # 坐标刻度
  # my_y_ticks = np.arange(300, 400, 10)
  # my_x_ticks = np.arange(1, 10, 1)
  # plt.xticks(my_x_ticks)
  # plt.yticks(my_y_ticks)
  # plt.yticks(range(100, 300, 10))

  #旋转日期
  plt.gcf().autofmt_xdate()

  # 展示每个坐标
  # for a, b in zip(wights, highs_float):
  #   plt.text(a, b, (a, b), ha='center', va='bottom', fontsize=8)

  # plt.show()

  time_now = time.strftime("%m%d-%H:%M:%S", time.localtime())
  path = "report/" + time_now
  plt.savefig(path)

def name_app():
  cmd = 'adb shell dumpsys window | grep mCurrentFocus > log_su/name_info.csv'
  os.system(cmd)
  with open('log_su/name_info.csv', encoding='utf-8', mode='r') as f:
    lines = f.readlines()
    for line in lines:
      if 'mCurrentFocus' in line:
        name1 = line.split('/')[0].split(' ')
        name = name1[len(name1) - 1]

  with open('config/director.txt', encoding='utf-8', mode='w') as f_name:
    text = name
    f_name.write(text)
  print(f"将要监测的包名为:{text}")

def time_control():
  global filename
  while True:
    end_time = time.time()
    if (end_time - start_time)/60 >= tol_time:  #分钟
    # if end_time - start_time >= tol_time:  #秒
      break
    # time.sleep(2)
    # filename = str(input("请输入文件名:"))
    adb = "adb shell dumpsys meminfo > log_su/adb_info.csv"
    d = os.system(adb)
    filename = "log_su/adb_info.csv"
    get_mem()
    write_report()

if __name__ == "__main__":
  name_app()
  tol_time = int(input("请输入脚本执行时间(分钟):"))
  start_time = time.time()
  get_applist()
  write_head()
  time_control()
  mapping()

会在.py文件同级目录下生成3个文件夹,config、log_su、report,其中运行结果在report中

生成的内存结果图如下:

Python获取android设备cpu和内存占用情况

到此这篇关于Python获取android设备cpu和内存占用情况的文章就介绍到这了,更多相关Python获取android设备内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
在Python上基于Markov链生成伪随机文本的教程
Apr 17 Python
Python实现好友全头像的拼接实例(推荐)
Jun 24 Python
pandas修改DataFrame列名的方法
Apr 08 Python
详解TensorFlow查看ckpt中变量的几种方法
Jun 19 Python
python实现将读入的多维list转为一维list的方法
Jun 28 Python
一文带你了解Python中的字符串是什么
Nov 20 Python
Python中的类与类型示例详解
Jul 10 Python
python使用正则来处理各种匹配问题
Dec 22 Python
python Canny边缘检测算法的实现
Apr 24 Python
python读取hdfs并返回dataframe教程
Jun 05 Python
python如何编写类似nmap的扫描工具
Nov 06 Python
python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)
Dec 03 Python
Python __slots__的使用方法
Nov 15 #Python
Python descriptor(描述符)的实现
Nov 15 #Python
基于OpenCV的网络实时视频流传输的实现
Nov 15 #Python
彻底解决Python包下载慢问题
Nov 15 #Python
Python eval函数原理及用法解析
Nov 14 #Python
Django怎么在admin后台注册数据库表
Nov 14 #Python
通过实例解析python and和or使用方法
Nov 14 #Python
You might like
英雄试炼之肉山谷—引领RPG新潮流
2020/04/20 DOTA
PHP 处理TXT文件(打开/关闭/检查/读取)
2013/05/13 PHP
解决file_get_contents无法请求https连接的方法
2013/12/17 PHP
php实现Linux服务器木马排查及加固功能
2014/12/29 PHP
javascript 密码强弱度检测万能插件
2009/02/25 Javascript
jquery 扑捉回车键事件代码
2014/04/24 Javascript
使用jQuery监听DOM元素大小变化
2016/02/24 Javascript
jQuery判断是否存在滚动条的简单方法
2016/09/17 Javascript
jQuery+json实现动态创建复杂表格table的方法
2016/10/25 Javascript
JS简单判断函数是否存在的方法
2017/02/13 Javascript
jQuery实现字符串全部替换的方法【推荐】
2017/03/09 Javascript
angularjs中使用ng-bind-html和ng-include的实例
2017/04/28 Javascript
安装vue-cli的简易过程
2018/05/22 Javascript
小程序外卖订单界面的示例代码
2019/12/30 Javascript
React.js组件实现拖拽排序组件功能过程解析
2020/04/27 Javascript
[44:51]2018DOTA2亚洲邀请赛 4.4 淘汰赛 VP vs Liquid 第二场
2018/04/05 DOTA
Django实现的自定义访问日志模块示例
2017/06/23 Python
python使用tkinter实现简单计算器
2018/01/30 Python
Python Selenium Cookie 绕过验证码实现登录示例代码
2018/04/10 Python
mac下pycharm设置python版本的图文教程
2018/06/13 Python
使用python 3实现发送邮件功能
2018/06/15 Python
Python Cookie 读取和保存方法
2018/12/28 Python
python pcm音频添加头转成Wav格式文件的方法
2019/01/09 Python
pandas进行时间数据的转换和计算时间差并提取年月日
2019/07/06 Python
手把手教你进行Python虚拟环境配置教程
2020/02/03 Python
Python中用pyinstaller打包时的图标问题及解决方法
2020/02/17 Python
方法名是否可以与构造器的名字相同
2012/06/04 面试题
银行竞聘上岗演讲稿
2014/09/12 职场文书
副总经理岗位职责范本
2014/09/30 职场文书
街道党工委党的群众路线教育实践活动对照检查材料思想汇报
2014/10/05 职场文书
避暑山庄导游词
2015/02/04 职场文书
2015年行政人事部工作总结
2015/05/13 职场文书
爱国影片观后感
2015/06/18 职场文书
医院病假条范文
2015/08/17 职场文书
浅谈Nginx 中的两种限流方式
2021/03/31 Servers
详细谈谈MYSQL中的COLLATE是什么
2021/06/11 MySQL