Android性能优化-电量-Battery Historian的安装与启动

最近面试了几家单位,从Android基础到性能优化再到系统底层源码都有一定程度的考察。在回答性能优化的时候总感觉做了一些这方面的工作,但却又没有办法系统的回答或者经历过的项目优化的量化指标还不够极限,还有很多优化空间。本篇记录电量优化中Battery Historian的安装与启动。

Battery Historian是什么

Battery Historian工具可以深入了解设备随时间的电池消耗情况。在系统级别,该工具以HTML表示形式显示系统日志中与电源相关的事件。在特定于应用程序的级别,该工具提供了各种数据,可帮助您识别耗尽电池的应用行为。(来源官网)

Battery Historian如何安装

Battery Historian Github官网 介绍了两种使用方式,这里采用的是Docker的方式。Docker简单理解为一种类似虚拟机的镜像,发布的应用以及应用所需要的软件环境都统一标准打包好再进行发布。比虚拟机开销小、部署快速、Docker容器之间互不干扰。

1.Docker的安装

Docker的官方网站提供Mac和Win 10的安装包下载。这里采用Mac版的安装包,整个安装过程和一般软件安装无异。

安装完成后启动,首次启动会输入电脑登录密码。

Docker的常用命令

查看docker的版本 docker --version

Docker version 18.03.1-ce, build 9ee9f40

查看docker详细信息,包含当前运行的镜像 docker info

Containers: 2
 Running: 0
 Paused: 0
 Stopped: 2
Images: 1
Server Version: 18.03.1-ce
...

安装Docker镜像 docker run hello-world 如果本地找不到镜像就会从远处仓储进行拉取,然后启动镜像的一个容器。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete 
Digest: sha256:3e1764d0f546ceac4565547df2ac4907fe46f007ea229fd7ef2718514bcec35d
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.
    (amd64)
 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.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

查看当前所有容器 docker ps -all

 CONTAINER ID        IMAGE                                                     COMMAND                  CREATED             STATUS                      PORTS                    NAMES
212de7bfb81b        hello-world                                               "/hello"                 5 minutes ago       Exited (0) 5 minutes ago                             kind_ramanujan

查看当前所有镜像 docker images

REPOSITORY                                                TAG                 IMAGE ID            CREATED             SIZE
hello-world                                               latest              e38bc07ac18e        2 months ago        1.85kB

删除镜像 docker rmi <IMAGE ID> 删除镜像的前台是先把镜像关联的container删除 docker rm <CONTAINER ID>,相当于说容器是镜像的一个状态副本(运行态、停止态)。

2.阿里云镜像进行拉取

docker的常用命令可以查看官网进行详细学习,这里回到Battery Histrian的安装。已经有了docker安装Battery Histrian就比较简单了,根据Github官网给出的命令进行拉取

这里需要把<port>替换一个端口号,也需要把 -- 去掉(不知道github上为什么会这样给出),那最后的命令是:docker run -p 9000:9999 gcr.io/android-battery-historian/stable:3.0 --port 9999

Unable to find image 'gcr.io/android-battery-historian/stable:3.0' locally
docker: Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

我虽然翻墙了,但是还是连不上Google云进行拉取镜像,那我就转向国内是否已经有稳定的镜像呢?一番查找,来到了阿里云镜像仓库https://dev.aliyun.com/search.html这里需要登录阿里云才可以搜索,刚开始我还以为我浏览器不行。 ̄□ ̄||。搜索关键词:battery
在结果中找到最新的一个镜像并且拉取即可。

最新版是stable-3.0,执行的命令是:docker run -p 9000:9999 registry.cn-hangzhou.aliyuncs.com/xyz10/android-battery-historian:stable-3.0

Unable to find image 'registry.cn-hangzhou.aliyuncs.com/xyz10/android-battery-historian:stable-3.0' locally
stable-3.0: Pulling from xyz10/android-battery-historian
4edc955e886b: Downloading [===>                                               ]  2.814MB/46.77MB
b38b3ce2f236: Download complete 
23eada996014: Download complete 
230e6aa21788: Download complete 
1512449f61db: Download complete 
d3c38f0faedf: Downloading [>                                                  ]  524.6kB/286.2MB
16c3ffb28dcd: Downloading [=========>                                         ]  327.1kB/1.711MB
d9f297fb0fb4: Waiting 
c9b753bf79ea: Waiting 
fb96998348cf: Waiting 
7000301bd4e4: Waiting 
69b7039b6413: Waiting 
dea3253a15d1: Pulling fs layer 

等待下载完成即可,使用docker images查看是否已经存在镜像。以及使用docker ps -a 查看是否启动

CONTAINER ID        IMAGE                                                                          COMMAND                  CREATED             STATUS                         PORTS                    NAMES
aaf7423e1315        registry.cn-hangzhou.aliyuncs.com/xyz10/android-battery-historian:stable-3.0   "battery-historian"      15 minutes ago      Up 15 minutes                  0.0.0.0:9000->9999/tcp   adoring_pasteur
212de7bfb81b        hello-world                                                                    "/hello"                 About an hour ago   Exited (0) About an hour ago                            kind_ramanujan

Battery Historian如何启动

在命令行已经看到监听着9000端口并且已经启动了,所以在浏览器输入http://localhost:9000/

获取手机的BugReport文件然后上传进行分析

1.详细记录唤醒锁信息 adb shell dumpsys batterystats --enable full-wake-history

Enabled: full-wake-history

2.重置手机电池消耗信息 adb shell dumpsys batterystats --reset

Battery stats reset.

3.启动APP进行一些测试案例

4.导出bugreport文件

在Android 7.0以及之后都采用

adb bugreport bugreport.zip

在Android 7.0之前采用

adb bugreport > bugreport.txt

经过测试如果使用adb bugreport bugreport.txt 也可以在7.0以上使用,这个时候会生成一个.zip文件和一个.txt文件。txt文件仅仅是记录了.zip文件的文件写入进度。

5.上传bugreport文件

点击Browse,选择bugreport.zip文件然后点击Submit即可。

6.最终呈现HTML结果页

截图是使用华为Mate 8,Android 7.0,EMUI 5.1。(Nexus 6,三星C5,锤子坚果Pro均测试可以导出并且生成结果页)

7.分析参数性能指标

这个部分还没深入。本篇就先记录安装与启动。

猜你喜欢

转载自blog.csdn.net/scau_zhangpeng/article/details/80919277