CPU是移动设备非常重要的资源,我们必须保证app在使用过程中是一个稳定的状态,因此,我们需要对cpu的状态进行监控
监控cpu的指令
adb shell dumpsys cpuinfo | findstr packagename
我们在监控过程中,需要定时采集,因为我们需要模拟用户真实的使用场景,加入说对于浏览器,我们需要搜索10个词,那么我就要监控在搜索10个词过程中整个cpu的状态,那么我们就需要写一个脚本定时的数据cpu的状态值,定时我们可以通过加入时间间隔,例如10秒,5秒输入一个cpu状态值,自定义即可
命令效果
多次执行,每次数据取出放入数据列表,进行分析就ok了
下边来实现这个脚本
# -*- coding: utf-8 -*-
# @Time : 2020/4/7 11:40
# @Author : Willam.zja
# @FileName: cpuStatus.py
# @Software: PyCharm
# @Blog :https://blog.csdn.net/enemy_sprites
import os
import sys
sys.path.append(os.getcwd())
from common.libs.commandLines import dosLine
from common.libs.dataHelper import getCurrentTime
import time
from common.libs.saveToCsv import saveToCsv
class Controller(object):
def __init__(self,count):
self.counter = count
self.all_data = [('timestamp','cpustatus')]
def testprocess(self):
cpu_value = ''
result = dosLine.execute_result('adb shell dumpsys cpuinfo |findstr com.android.browser')
for i in result:
cpu_value = str(i).split('%')[0].strip()
nowTime = getCurrentTime()
self.all_data.append((nowTime,cpu_value))
print(self.all_data)
def run(self):
x= self.counter + 1
while self.counter > 0:
print('第{}次执行'.format(x - self.counter))
self.testprocess()
self.counter -= 1
time.sleep(5)
def run_main(self):
self.run()
fn = self.all_data[0][1]
saveToCsv(fn,self.all_data)
if __name__ == '__main__':
cpuController = Controller(11)
cpuController.run_main()
过程中我们执行一个操作,可以配合一个自动化脚本来使用,不同的操作cpu的变化