app性能测试:(四)电量
下面对电量进行分析:
需注意,手机通过USB连接电脑,手机会处于充电状态,必须保证手机为非充电状态
-
切换非充电状态
命令:adb shell dumpsys battery set status 1
其中 status 1 为非充电状态,status 2 为充电状态
即 -
切换为充电状态
命令:adb shell dumpsys battery set status 2
-
获取电量
命令:adb shell dumpsys battery
(UPDATES STOPPED – use ‘reset’ to restart)
AC powered: false
USB powered: false
Wireless powered: false
Max charging current: 0
Max charging voltage: 0
Charge counter: 0
status: 1
health: 2
present: true
level: 30
scale: 100
voltage: 3764
temperature: 200
technology: Li-ion
监控脚本监控app电量消耗
python3代码:#/usr/bin/python #encoding:utf-8 import csv import os import time #控制类 class Controller(object): def __init__(self, count): #定义测试的次数 self.counter = count #定义收集数据的数组 self.alldata = [("timestamp", "power")] #单次测试过程 def testprocess(self): #执行获取电量的命令 result = os.popen("adb shell dumpsys battery") #获取电量的level for line in result: if "level" in line: power = line.split(":")[1] #获取当前时间 currenttime = self.getCurrentTime() #将获取到的数据存到数组中 self.alldata.append((currenttime, power)) #多次测试过程控制 def run(self): #设置手机进入非充电状态 os.popen("adb shell dumpsys battery set status 1") while self.counter >0: self.testprocess() self.counter = self.counter - 1 #每5秒钟采集一次数据 time.sleep(5) #获取当前的时间戳 def getCurrentTime(self): currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) return currentTime #数据的存储 def SaveDataToCSV(self): csvfile = open('power.csv', mode='w') writer = csv.writer(csvfile) print(self.alldata.__len__()) writer.writerows(self.alldata) csvfile.close() if __name__ == "__main__": controller = Controller(5) controller.run() controller.SaveDataToCSV()