python-获取cpu,内存

2017-01-01 10:46

获取cpu使用情况

  • 获取整个手机的cpu使用情况
 adb shell dumpsys cpuinfo

Paste_Image.png

  • 这里可以看到所有进程的Cpu占用率:
  • 看第一个应用CPU占用率68%,这个过程是在用户(user)中花61%的时间,并在内核空间(kernel)花费7.1%的时间。

  • 获取指定app的cpu

D:\> adb shell dumpsys cpuinfo | grep com.dgm.user
  0.6% 25631/com.dgm.user: 0.6% user + 0% kernel

获取内存使用情况

D:\>adb shell  dumpsys  meminfo  com.dgm.user
Applications Memory Usage (kB):
Uptime: 221850323 Realtime: 245470576

** MEMINFO in pid 25631 [com.dgm.user] **
                   Pss  Private  Private  Swapped     Heap     Heap     Heap
                 Total    Dirty    Clean    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------   ------
  Native Heap     9084     9080        0        0    11716     9335     1992
  Dalvik Heap    23567    23220        0        0    39956    35893     4063
 Dalvik Other     4577     4540        0        0
        Stack      304      304        0        0
    Other dev        5        0        4        0
     .so mmap     1274      264      524        0
    .apk mmap      205        0        4        0
    .ttf mmap      783        0      416        0
    .dex mmap     3684       56     2120        0
   Other mmap       10        4        0        0
           GL    18496    18496        0        0
      Unknown      176      176        0        0
        TOTAL    62165    56140     3068        0    51672    45228     6055

 Objects
               Views:     1609         ViewRootImpl:        2
         AppContexts:        4           Activities:        2
              Assets:        3        AssetManagers:        3
       Local Binders:       24        Proxy Binders:       30
    Death Recipients:        1
     OpenSSL Sockets:        0

 SQL
         MEMORY_USED:       90
  PAGECACHE_OVERFLOW:       16          MALLOC_SIZE:       62

 DATABASES
      pgsz     dbsz   Lookaside(b)          cache  Dbname
         4       28             36        1/136/2  /data/data/com.dgm.user/databases/dgmuser.db

 Asset Allocations
    zip:/data/app/com.dgm.user-1.apk:/resources.arsc: 289K
  • PSS Total 就是你的应用实际所占内存

python代码获取CPU,内存

  • 检查设备是否存
 class AndroidDebugBridge(object):
    def call_adb(self, command):
        command_result = ''
        command_text = 'adb %s' % command
        results = os.popen(command_text, "r")
        while 1:
            line = results.readline()
            if not line: break
            command_result += line
        results.close()
        return command_result

    def attached_devices(self):
        result = self.call_adb("devices")
        devices = result.partition('\n')[2].replace('\n', '').split('\tdevice')
        flag = [device for device in devices if len(device) > 2]
        if flag:
            return True
        else:
            return False
import subprocess
import base_adb
pkg_name = "com.dgm.user"
def top_cpu():
    cpu = 0
    cmd = "adb shell dumpsys cpuinfo | grep com.dgm.user"
    # cmd = "adb shell top -n %s -s cpu | grep %s$" %(str(times), pkg_name)
    top_info = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
    for info in top_info:
        cpu = info.split()[2].decode() # bytes转换为string
    print("cpu占用:%s" %cpu)
    return cpu

def get_men():
    cmd = "adb shell  dumpsys  meminfo %s"  %(pkg_name)
    men = []
    men_s = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
    for info in men_s:
        temp = info.split()
        men.append(temp)
    print(men)
    print("内存占用:%s" %men[19][1].decode()+"K")
    return men
if base_adb.AndroidDebugBridge().attached_devices():
    top_cpu()
    get_men()
else:
    print("请插入设备")

结果如下:

  • cpu占用:19%
  • 内存占用:61589K

下篇帖子会集成monkey 采集数据,将cpu,内存,电量,流量等信息生成html