Android 系统 >= 5.0

清空耗电数据

adb shell dumpsys batterystats --reset

获取电量数据

adb shell dumpsys batterystats >battery.txt

battery.txt 各个数值的意义参考 https://developer.android.com/studio/profile/battery-historian-charts.html#FilteringBatterystats

其中关键词 Estimated power use(估算电量)下有具体 uid 的耗电,uid 根据文本头部通过进程名可以筛选到。

另外,根据 uid,还可以找到流量数据,alarm 和唤醒次数,等等。

比如:
第一行 70.0 为微信的耗电

Estimated power use (mAh):
    Capacity: 3900, Computed drain: 439, actual drain: 117-156
    Uid u0a166: 70.0 ( cpu=1.66 wake=1.69 radio=66.1 sensor=0.540 )
    Idle: 45.5
    Uid u0a84: 43.4 ( cpu=0.732 wake=0.000292 radio=1.02 gps=0.304 sensor=41.3 )
    Uid u0a15: 42.5 ( cpu=0.118 wake=0.120 radio=42.2 )
    Uid 0: 42.2 ( cpu=8.27 wake=7.58 radio=26.4 )
    Uid 1000: 26.8 ( cpu=8.09 wake=1.24 radio=16.8 gps=0.534 sensor=0.118 )
    Uid u0a298: 10.9 ( cpu=3.11 wake=4.49 radio=2.74 sensor=0.560 )

具体统计:

u0a166:
  Mobile network: 181.22KB received, 79.26KB sent (packets 488 received, 617 sent)
  Mobile radio active: 22m 2s 198ms (29.1%) 76x @ 1197 mspp
  Wake lock *alarm*: 2s 75ms partial (48 times) realtime
  Wake lock WakerLock:overflow: 2m 51s 280ms partial (220 times) realtime
  TOTAL wake: 2m 53s 355ms partial realtime
  Sensor 17: 2h 20m 58s 990ms realtime (0 times)
  Background for: 7h 33m 26s 672ms 
  Total running: 7h 33m 26s 672ms 

一段提取耗电量的脚本:

pkgName = "com.xx.xx"
ps = subprocess.Popen("adb shell dumpsys batterystats", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

uid = ""
uid_tag = False
power_tag = False
uid_re = '=(.+?):"{}"'.format(pkgName)
while True:
    line = ps.stdout.readline()
    if line:
        if "Battery History" in line:
            uid_tag = True
        if "Estimated power use (mAh):" in line:
            power_tag = True
    else:
        break

    if uid_tag:
        temp = re.findall('=(.+?):"{}"'.format(pkgName), line)
        if len(temp) > 0:
            uid = temp[0]
            print uid
            uid_tag = False
    if power_tag:
        temp = re.findall("Uid {}: (.+?) ".format(uid), line)
        if len(temp) > 0:
            print temp[0]
            power_tag = False

battery-historian

更详细的图表需要battery-historian下的/script/historian.py 生成图表。

python historian.py -a battery.txt > battery.html

这里生产的网页查看需要 ***,因为请求了一些 google 的 js 和 css,这里把需要的资料放网盘了。(这样本地化也方便集成到其他系统)

百度网盘

一共需要替换两个地方

  1. 资源中 jsapi 最后两行 writeLoadTag 方法中 load 资源的路径,改成自己的路径
oogle.loader.writeLoadTag("css", google.loader.ServiceBase + "/api/visualization/1.0/40ff64b1d9d6b3213524485974f36cc0/ui+zh_CN.css", false);
google.loader.writeLoadTag("script", google.loader.ServiceBase + "/api/visualization/1.0/40ff64b1d9d6b3213524485974f36cc0/dygraph,format+zh_CN,default+zh_CN,ui+zh_CN,timeline+zh_CN.I.js", false);
  1. battery-historian 生成的 html 文件,需要导入所有的资源(两个 css,两个 js,一个 jsapi)

比如:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>

替换成:

<link href="/static/dataProcess/js/battery/tooltip.css" rel="stylesheet" type="text/css">
      <script src="/static/dataProcess/js/battery/jquery.min.js"></script>
      <script type="text/javascript" src="/static/dataProcess/js/battery/jsapi"></script>
      <link href="/static/dataProcess/js/battery/ui+zh_CN.css" type="text/css" rel="stylesheet">
      <script src="/static/dataProcess/js/battery/dygraph,format+zh_CN,default+zh_CN,ui+zh_CN,timeline+zh_CN.I.js" type="text/javascript"></script>

一切都可以写成 python 脚本~美滋滋(historian.py 中有很多可以拿来现成用的代码,建议可以看看)

目前刚开始做,准备集成到性能测试的自动化脚本中,求喷求建议

坑:
记录一个坑,在连接 usb 数据线的情况下,数据不会更新,数据不会更新,数据不会更新。必须拔掉数据线,才会开始记录。


↙↙↙阅读原文可查看相关链接,并与作者交流