Android 使用 Batterystats 和 Battery Historian 分析电池用量

Android开发中涉及到耗电量怎么分析呢
google官方提供了文档,链接:https://developer.android.com/studio/profile/battery-historian

Batterystats 工具和 Battery Historian 脚本的基本用法和工作流程
Batterystats 是包含在 Android 框架中的一种工具,用于收集设备上的电池数据。您可以使用 adb 将收集的电池数据转储到开发计算机,并创建一份可使用 Battery Historian 分析的报告。Battery Historian 会将报告从 Batterystats 转换为可在浏览器中查看的 HTML 可视化内容

适合的场景:
显示进程从什么位置以及通过何种方式消耗电池电量。
识别系统为了延长电池续航时间可能会延迟甚至移除应用中的哪些任务
安装 Battery Historian
链接:https://github.com/google/battery-historian

1.使用Docker安装
安装 Battery Historian 的最简单方法是使用 Docker。对于其他安装方法(包括从源代码编译),请参阅项目的 GitHub 页面上的自述文件。要使用 Docker 进行安装,请执行以下操作:

按照 Docker https://www.docker.com/ 网站上的说明安装 Docker 社区版

要确认 Docker 是否已正确安装,请打开命令行并输入以下命令:

  docker run hello-world
1
如果您已正确安装 Docker,将会看到如下输出:

  Unable to find image 'hello-world:latest' locally
  latest: Pulling from library/hello-world
  78445dd45222: Pull complete
  Digest:
  sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
  Status: Downloaded newer image for hello-world:latest

  Hello from Docker!
  This message shows that your installation appears to be working correctly.

  To generate this message, Docker took the following steps:
   1. The Docker client contacted the Docker daemon.
   2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
   3. The Docker daemon created a new container from that image which runs the
      executable that produces the output you are currently reading.
   4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
在终端搜索Battery Historian相关的镜像

  docker search battery 
1
在列出的列表中选取一个镜像

  docker run --name=battery -d -p 9999:9999 bhaavan/battery-historian
1
拉取并运行battery-historian镜像,会自动下载并运行镜像,之前pull过了就直接运行

2.通过编译gitbub上面的源码来安装
1.Go 环境安装

链接:https://golang.org/doc/install

配置GOROOT和GOPATH(配置环境变量)

GOROOT的作用是告诉 Go 命令和其他相关工具,配置的是 Go 的安装目录

GOPATH可以简单理解为是工程的目录,配置的是 Go 的工程路径

安装完执行 go env 查看环境变量配置的是否一样,如下图

2.Git 环境安装

链接:https://git-scm.com/
3.python环境安装,仅支持python2.7

链接:https://www.python.org/
4.Java 环境安装(配置环境变量)

链接:https://www.oracle.com/java/technologies/javase-jdk8-downloads.html
5.下载 Battery Historian

链接:https://github.com/google/battery-historian 源码并且运行

  下载到GOPATH配置目录下,耗时长 
  go get -d -u github.com/google/battery-historian/...
1
2
进入到$GOPATH/src/github.com/google/battery-historian目录

运行Battery Historian

  go run setup.go
1
如果没有下载成功,可以手动下载

closure-library

https://github.com/google/closure-library
closure-compiler

https://github.com/google/closure-compiler
flot-axislabels

https://github.com/markrcote/flot-axislabels
解压到 GOPATH/src/github.com/google/battery-historian/third_party,如果没有该文件夹需要去创建,文件夹下方的的 closure-compiler 和 closure-library 和 flot-axislabels

目录如下

在 battery-historian 文件夹执行

  go run cmd/battery-historian/battery-historian.go
1
启动程序后,在浏览器输入localhost:9999,会出现如下界面

3.Batterystats 采集数据
1.将移动设备连接到计算机

2.在终端窗口中,关闭正在运行的 adb 服务器

  adb kill-server
1
3.重启 adb 并检查是否有已连接的设备

  adb start-server
  adb devices
1
2
如下图

4.重置电池数据收集

  adb shell dumpsys batterystats --reset
1
设备始终会在后台收集 batterystats 和其他调试信息。重置操作会清除旧的电池收集数据。如果不重置,输出内容会超大

5.断开设备与计算机的连接,以便仅消耗设备电池的电量

6.使用您的应用并执行您想要获取数据的操作;例如,断开 WLAN 连接并将数据发送到云端

7.重新手机连接

8.确保您的手机已被识别:

  adb devices
1
9.转储所有电池数据。此过程可能需要一段时间:

  adb shell dumpsys batterystats > [path/]batterystats.txt
1
系统会使用可选路径参数在您指定的目录中创建 batterystats.txt 文件。如果您没有指定路径,则会在您的主目录中创建该文件

10.使用原始数据创建报告

在创建报告之前,可以使用monkey针对某个应用做测试

  adb shell monkey -p com.tencent.mm -v 1000
1
如果要保存在某个目录下,先进入此目录

  cd /Users/Desktop/battery
1
对于搭载 Android 7.0 及更高版本的设备:

  adb bugreport bugreport.zip
1
对于搭载 Android 6.0 及更低版本的设备:

  adb bugreport bugreport.txt
1
错误报告可能需要几分钟才能完成。在完成之前,请勿断开设备连接或取消该进程

与上面的 batterystats.txt 一样,这些文件是系统使用可选的路径参数在您指定的目录中创建的。如果您没有指定路径,系统将在您的主目录中创建这些文件

导出的文件如下

4.将采集到的数据上传
打开浏览器输入 localhost:9999,点击上传,上传之后出现submit 提交按钮


点击提交后,进入电池耗量分析界面


另一张视图

在搭建的过程中遇到些问题,上传zip包之后报错,无法加载视图
Docker搭建的方式,对于安卓6.0以上不友好,出现上传zip后报错,无法加载视图的情况:

解决方式:使用线上版: https://bathist.ef.lc/

报错如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值