cpuinfo命令

  1. adb shell dumpsys cpuinfo

image.png

大家看第一个应用CPU占用率13%,这个过程是在用户(user)中花8%的时间,并在内核空间(kernel)花费5.7%的时间。
如果你想筛选出你自己的应用的话可以用下面这一段:
Linux 系统

  1. adb shell dumpsys cpuinfo |grep packagename

Windows系统 使用 findstr 过滤

  1. adb shell dumpsys cpuinfo | findstr com.sina.weibo

top 命令

  1. adb shell
  2. top -m 10 -s cpu

image.png
可查看占用cpu最高的前10个程序(-t 显示进程名称,-s 按指定行排序,-n 在退出前刷新几次,-d 刷新间隔,-m 显示最大数量)

备注: 如果是连接的手机比如小米手机 不支持参数 -s cpu 请把cpu 改为数字 1-9

  1. adb shell
  2. top -m 10 -s 1

python 脚本

  1. #/usr/bin/python
  2. #encoding:utf-8
  3. import csv
  4. import subprocess
  5. import time
  6. #控制类
  7. class Controller(object):
  8. def __init__(self, count):
  9. self.counter = count
  10. self.alldata = [("timestamp", "cpustatus")]
  11. #单次测试过程
  12. def testprocess(self):
  13. content= subprocess.Popen("adb shell dumpsys cpuinfo | findstr com.android.browser",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  14. for line in content.stdout.readlines():
  15. line = line.decode('utf-8')
  16. cpuvalue = line.split("%")[0]
  17. cpuvalue = cpuvalue.strip()
  18. currenttime = self.getCurrentTime()
  19. self.alldata.append((currenttime, cpuvalue))
  20. #多次执行测试过程
  21. def run(self):
  22. while self.counter >0:
  23. self.testprocess()
  24. self.counter = self.counter - 1
  25. time.sleep(3)
  26. #获取当前的时间戳
  27. def getCurrentTime(self):
  28. currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  29. return currentTime
  30. #数据的存储
  31. def SaveDataToCSV(self):
  32. csvfile = open('cpustatus.csv', 'w',encoding='utf-8',newline='')
  33. writer = csv.writer(csvfile)
  34. writer.writerows(self.alldata)
  35. csvfile.close()
  36. if __name__ == "__main__":
  37. controller = Controller(10)
  38. controller.run()
  39. controller.SaveDataToCSV()