zoukankan      html  css  js  c++  java
  • Android应用耗电量统计,无需USB连接

    Android应用耗电量统计一直是一个很头疼的问题,手工统计耗时太长,自动化统计又不是非常精准(执行自动化代码需要通过USB连接,而USB又会充电,这就造成统计数据不准)。后来从前辈那里得知可以通过adb connect来实现无线连接,下面就来说说方法。

    1、首先找到一台已经root的手机

    2、使手机与PC处于同一个网段

    3、下载安装Wireless ADB

    4、打开Wireless ADB,勾选Wireless ADB,设置端口(默认5555,被占用时设置)

    5、打开pc的CMD窗口,输入adb connect 192.168.1.100:5555 回车(详细IP会在Wireless ADB中显示)

    6、连接成功

    如果提示not implement,则与其他软件冲突,请尝试关掉豌豆夹、360等软件

    下面来执行统计耗电量的代码,由于要统计应用前台操作一小时耗电量,人工点击显然是不现实的,这里用到了monkey来配合测试。这里只演示整个系统的耗电量,单个应用的耗电量dumpsys较为复杂。

    #coding=utf-8
    '''
    Create on 2015-1-7
    python 2.7 for window
    @auther: tangdongchu
    '''
    import os
    import sys
    import time
    import re
    import datetime
    
    class monkeyTest():
        
        def __init__(self):
            """ init """
                
        #monkey命令,packageName包名,interval间隔时间单位ms ,frequency执行次数
        def monkeyApp(self,packageName,interval,frequency):
            try:
                os.popen("adb shell monkey -p %s --throttle %s --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v %s >e:monkeylogmonkeyScreenLog.log" % (packageName, interval, frequency),'r')
            except Exception,e:
                print e
    
        #获取当前电量
        def getCurrentBattery(self):
            try:
                for Battery in os.popen('adb shell dumpsys battery','r').readlines():
                    reList = re.sub('Battery:','',Battery)
                    reList = reList.replace('
    ','')
                    result = re.search('level', reList)
                    if result != None :
                        List = reList.split()
                        level=List.pop()#删除第i个元素,并返回这个元素。若调用pop()则删除最后一个元素
                        #print "battery level " + level + "%"
                        return level
                        break
            except Exception,e:
                print e
                
        #获取当前时间,用于计算应用运行时间
        def getCurrentTime(self):
            try:
                currentTime = datetime.datetime.now()
                return currentTime
            except Exception,e:
                print e
               
    def main():
        print """"""
        
        
    if __name__=="__main__":
        
        packageName = 'ctrip.android.view'  
        myApp = monkeyTest()  
        level = int(myApp.getCurrentBattery())
        runtime = myApp.getCurrentTime()
        myApp.monkeyApp(packageName,500,2500) #0.5秒点一次,运行2500次
        #判断是否执行完成,执行完成后统计耗电量
        for i in range(1, 1000000):
            monkeylog = open('E:monkeylogmonkeyScreenLog.log')
            try:
                temp = monkeylog.read( )
            finally:
                monkeylog.close( )
            if temp.count('Monkey finished')>0:
                level = int(myApp.getCurrentBattery())-level
                runtime = myApp.getCurrentTime()-runtime
                break
            else:
                time.sleep(2)
        print "run time " + str(runtime)
        print "use battery" + str(level) + "%"
    

      

  • 相关阅读:
    你眼中的程序员 VS 程序员眼中的自己,是时候打破代沟了
    GaussDB(for openGauss)让数据“存得下、算得快、算得准”
    初识GaussDB(for Cassandra)
    云原生势不可挡,华为云GaussDB加速企业数字化转型
    HDC.Cloud2021|开发者们都在谈的云原生到底长什么样?
    基于深度神经网络的噪声标签学习
    华为云PB级数据库GaussDB(for Redis)揭秘第七期:高斯Redis与强一致
    Delphi 窗体函数GetActiveWindow
    Delphi 窗体函数GetWindowText -获取窗口的标题
    深度学习数据预处理
  • 原文地址:https://www.cnblogs.com/tangdongchu/p/4208760.html
Copyright © 2011-2022 走看看