android 取消git_Android 使用 Batterystats 和 Battery Historian 分析电池用量

1fd000aff4b2f1ca661ca06d93842c5f.png

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

专注于Android开发,分享经验总结,欢迎加入

c354baeb4112f91ab3157437325aaa76.png

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
  • 如果您已正确安装 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.
  • 在终端搜索Battery Historian相关的镜像
    docker search battery
  • 在列出的列表中选取一个镜像
    docker run --name=battery -d -p 9999:9999 bhaavan/battery-historian
    • 拉取并运行battery-historian镜像,会自动下载并运行镜像,之前pull过了就直接运行

2.通过编译gitbub上面的源码来安装

  • 1.Go 环境安装
    • 链接:https://golang.org/doc/install
    • 配置GOROOT和GOPATH(配置环境变量)
    • GOROOT的作用是告诉 Go 命令和其他相关工具,配置的是 Go 的安装目录
    • GOPATH可以简单理解为是工程的目录,配置的是 Go 的工程路径

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

938a0c610a662321a3899e9ab04453cf.png
  • 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 http://github.com/google/battery-historian/...
    • 进入到$GOPATH/src/http://github.com/google/battery-historian目录
    • 运行Battery Historian
      go run setup.go
      如果没有下载成功,可以手动下载
    • 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/http://github.com/google/battery-historian/third_party,如果没有该文件夹需要去创建,文件夹下方的的 closure-compiler 和 closure-library 和 flot-axislabels

目录如下

3e81e3629832406bc70ea123bd60b465.png
  • 在 battery-historian 文件夹执行go run cmd/battery-historian/battery-historian.go

启动程序后,在浏览器输入localhost:9999,会出现如下界面

6a08066bd248e8235f6ca7f51385dbf4.png

3.Batterystats 采集数据

  • 1.将移动设备连接到计算机
  • 2.在终端窗口中,关闭正在运行的 adb 服务器
    adb kill-server
    • 3.重启 adb 并检查是否有已连接的设备
      adb start-server adb devices

如下图

e35aba05890dc0ae71818f349ef61c4d.png
  • 4.重置电池数据收集adb shell dumpsys batterystats --reset

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

  • 5.断开设备与计算机的连接,以便仅消耗设备电池的电量
  • 6.使用您的应用并执行您想要获取数据的操作;例如,断开 WLAN 连接并将数据发送到云端
  • 7.重新手机连接
  • 8.确保您的手机已被识别:
    adb devices
  • 9.转储所有电池数据。此过程可能需要一段时间:
    adb shell dumpsys batterystats > [path/]batterystats.txt
    系统会使用可选路径参数在您指定的目录中创建 batterystats.txt 文件。如果您没有指定路径,则会在您的主目录中创建该文件
  • 10.使用原始数据创建报告
    在创建报告之前,可以使用monkey针对某个应用做测试
    adb shell monkey -p com.tencent.mm -v 1000
    如果要保存在某个目录下,先进入此目录
    cd /Users/Desktop/battery
    • 对于搭载 Android 7.0 及更高版本的设备:
      adb bugreport bugreport.zip
    • 对于搭载 Android 6.0 及更低版本的设备:
      adb bugreport bugreport.txt

错误报告可能需要几分钟才能完成。在完成之前,请勿断开设备连接或取消该进程
与上面的 batterystats.txt 一样,这些文件是系统使用可选的路径参数在您指定的目录中创建的。如果您没有指定路径,系统将在您的主目录中创建这些文件

导出的文件如下

d2be73599d0f27e9a80025c4e6d3661e.png

4.将采集到的数据上传

  • 打开浏览器输入 localhost:9999,点击上传,上传之后出现submit 提交按钮

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

e94fb7bb085ae10ff4025120b3d43bcd.png

另一张视图

d4b80f69c020e255b1b347c22494e4b5.png

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

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

报错如下:

9b0449c84a7aa699a8c00ed29cc79419.png
微信公众号 -->> 他晓 (欢迎加入)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值