• 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) + "%"
    

      

  • 相关阅读:
    MYSQL数据库学习十二 使用MySQL运算符
    MYSQL数据库学习十一 多表数据记录查询
    MYSQL数据库学习十 单表数据记录查询
    MYSQL数据库学习九 数据的操作
    MYSQL数据库学习八 触发器的操作
    MYSQL数据库学习七 视图的操作
    MYSQL数据库学习六 索引的操作
    MYSQL数据库学习五 表的操作和约束
    MySQL数据库学习四 存储引擎和数据类型
    MySQL数据库学习三 数据库对象和基本操作
  • 原文地址:https://www.cnblogs.com/tangdongchu/p/4208760.html
Copyright © 2020-2023  润新知