Batterystats collects battery data from your device.
Batterystats is part of the Android framework.
Prerequisites:
A mobile device with Developer Options enabled running Android 5.0 or higher.
(前提条件:一台Android5.0及以上系统的手机,并开启开发者选项,打开USB调试)
1、Download the open-source Battery Historion Python script from GitHub (https://github.com/google/battery-historian).
2、Unzip the file to extract the Battery Historian folder. Inside the folder, find the historian.py file and move it to the Desktop or another writable directory.
3、Connect your mobile device to your computer.
4、On your computer, open a Terminal window.
5、Change to the directory where you've saved historian.py,
for example: cd ~/Desktop
6、Shut down your running adb server.
> adb kill-server
7、Restart adb and check for connected devices.
> adb devices
注意,默认情况下wake数据不会被采集,为了更好的分析数据,需要启用。
> adb shell dumpsys batterystats --enable full-wake-history
8、Reset battery data gathering.
> adb shell dumpsys batterystats --reset
Resetting erases old battery collection data; otherewise, the output will be huge.
9、Disconnect your device from your computer so that you are only drawing current from the device's battery.
拔出USB,非常重要!因为一直插着USB,当电充满,数据会被清空。Batterystats只会记录最后一次充满电后的记录。因此建议先把电充满,完成以上操作后,拔出USB。
10、Play with your app for a short time.
操作要统计电量的应用,所有应用都会被统计。因此,也可以统计竞品的耗电情况。
11、Reconnect your phone.
12、Make sure your phone is recognized: > adb devices
13、Dump all battery data. This can take a while:
> adb shell dumpsys batterystats > batterystats.txt
batterystats.txt里记载了整台手机的耗电信息
注意,最后要关闭全量记录唤醒,因为保持开启会造成性能问题。
> adb shell dumpsys batterystats --disable full-wake-history
14、Create a HTML version of the data dump for Battery Historian:
> python historian.py batterystats.txt > batterystats.html
15、Open the batterystats.html file in your browser.