cpuinfo命令
adb shell dumpsys cpuinfo
大家看第一个应用CPU占用率13%,这个过程是在用户(user)中花8%的时间,并在内核空间(kernel)花费5.7%的时间。
如果你想筛选出你自己的应用的话可以用下面这一段:
Linux 系统
adb shell dumpsys cpuinfo |grep packagename
Windows系统 使用 findstr 过滤
adb shell dumpsys cpuinfo | findstr com.sina.weibo
top 命令
adb shell
top -m 10 -s cpu
可查看占用cpu最高的前10个程序(-t 显示进程名称,-s 按指定行排序,-n 在退出前刷新几次,-d 刷新间隔,-m 显示最大数量)
备注: 如果是连接的手机比如小米手机 不支持参数 -s cpu
请把cpu 改为数字 1-9
即
adb shell
top -m 10 -s 1
python 脚本
#/usr/bin/python
#encoding:utf-8
import csv
import subprocess
import time
#控制类
class Controller(object):
def __init__(self, count):
self.counter = count
self.alldata = [("timestamp", "cpustatus")]
#单次测试过程
def testprocess(self):
content= subprocess.Popen("adb shell dumpsys cpuinfo | findstr com.android.browser",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
for line in content.stdout.readlines():
line = line.decode('utf-8')
cpuvalue = line.split("%")[0]
cpuvalue = cpuvalue.strip()
currenttime = self.getCurrentTime()
self.alldata.append((currenttime, cpuvalue))
#多次执行测试过程
def run(self):
while self.counter >0:
self.testprocess()
self.counter = self.counter - 1
time.sleep(3)
#获取当前的时间戳
def getCurrentTime(self):
currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return currentTime
#数据的存储
def SaveDataToCSV(self):
csvfile = open('cpustatus.csv', 'w',encoding='utf-8',newline='')
writer = csv.writer(csvfile)
writer.writerows(self.alldata)
csvfile.close()
if __name__ == "__main__":
controller = Controller(10)
controller.run()
controller.SaveDataToCSV()