Personal blog

www.milovetingting.cn

Optimizing Android Battery Historian -Battery Historian

preface

Batterystats has been built into Android since Android5.0 to collect battery data from devices. Adb can be used to export the collected Battery data and generate reports that can be analyzed using Battery Historian. Battery Historian translates the report from Batterystats into an HTML viewable view in a browser.

After Android8.0, it is also available via Android Studio’s profiler-Energy tool, which I won’t cover here.

Install the Docker

Battery Historian can be installed from Docker or downloaded from Battery Historian.

Through source code compilation, more steps, more complex process. I won’t tell you much here.

The easiest way to do this is through Docker.

Install the Docker

Docker installation, you can download the corresponding system platform installation package on the official website, you can refer to the link: yeasy.gitbook. IO /docker_prac…

Configuration mirror accelerator

Sometimes it is difficult to pull the image from Docker Hub in China, so you can configure the image accelerator. Check out this link :yeasy.gitbook. IO /docker_prac…

Install ‘Historian

  1. After starting Docker, run CMD and typedocker search battery, search for images of Battery Historian

  1. As listed above, battery Historian can apply only one image from anotherdocker pull blystad/battery-historian-2Command to pull a mirror

The download time varies depending on your network. If the download is slow, refer to the previous link to configure the image accelerator.

  1. Run –name= battery-d-p 9999:9999 blystad/battery-historian-2 ‘

    • The name argument can also specify other names, as long as they are not duplicated. The last argument to the command is the name used to pull the image above.

    • If you have already run the preceding command, an error will be reported. You can specify other values for name and port

  2. Then open the Docker Dashboard panel. Take Windows10 as an example, right click the Docker icon in the lower right corner of the computer and select Dashboard from the pop-up menu

  3. As you can see, we have created the containers we need. Click on the Start icon

  4. Click the icon in the red box below to open http://localhost:9999/ in your browser

  5. When opened, the interface looks like this:

    If the opening process is slow, consider whether there is an error in loading the page. You can open developer tools in F12

    If you see something like the red box in the figure above, you’re probably doing something wrong. Click to view, you can see that the js file is not loaded. In this case, only scientific online, otherwise the Submit button will not appear when uploading the Bugreport file later.

    Alternatively, instead of installing Battery Historian as a Docker, compile the source code by modifying the js file path referenced in the HTML file as an accessible one.

Export power data

  • Reset battery data collection

    adb shell dumpsys batterystats –reset

    The device always collects Batterystats and other debugging information in the background. The reset operation clears old battery collection data. If not reset, the output will be very large.

  • This parameter is required if you need to collect statistics on all wakeup locks

    adb shell dumpsys batterystats –enable full-wake-history

  • After the statistics are collected, disable this function

    adb shell dumpsys batterystats –disable full-wake-history

  • Disconnect the device from the computer so that only the battery of the device is consumed.

  • Operate the application to be tested

  • Reconnect the mobile phone and export data

    • For devices running Android 7.0 and later:

      adb bugreport > [path/]bugreport.zip

    • For devices running Android 6.0 or lower:

      adb bugreport > [path/]bugreport.txt

  • Upload the report

Analysis of power consumption

  • An analysis report page is generated after the upload is successful

    As you can see, the black line in the red box is trending down. Mouse over the line, will show the application of power consumption.

  • Data reports from AndroidQ and above may not be parsed

    In this case, you can change the mirror image

    docker pull runcare/battery-historian

    After the mirror pull is complete

    docker run –name=battery2 -d -p 9999:9999 runcare/battery-historian

    Note: name and the existing port number should not be repeated, you can stop the original container.

    The report can then be parsed normally

  • You can select the application to analyze to view specific information

  • View information about wakelock and Service on the right

  • View information under the System Stats TAB

Afterword.

Energy optimization is a systematic work. It is necessary to be familiar with the specific business of the project, analyze the specific power consumption points in terms of Historian analysis report, optimize the data in terms of codes, and verify the optimization effect.

In this article, Battery Historian documents the configuration of an environment and the export and uploading of Battery data reports. Specific analysis needs to be combined with the actual business to analyze the situation of power consumption, improve the capacity of electricity optimization in practice.

Record the preceding information for future reference.

reference

Developer.android.com/topic/perfo…

www.jianshu.com/p/378cf678b…

Yeasy. Gitbook. IO/docker_prac…

Developer.android.com/topic/perfo…

Developer.android.com/studio/prof…