官术网_书友最值得收藏!

  • Expert Android Programming
  • Prajyot Mainkar
  • 1250字
  • 2021-07-08 10:29:06

Batterystats and Battery Historian

The Android Settings menu options provide high level measurements of battery drain. However, to gain more insight into battery consuming apps, there are some tools that can be useful. Tools such as Batterystats and Battery Historian come in handy. Batterystats collects battery data from your device, and Battery Historian converts that data into a HTML visualization that helps you understand power consumption graphically.

Batterystats, was introduced in KitKat, but Lollipop brought a large dataset for the users which included data about every Wakelock action on the device.

Please reset the data and enable full Wakelock reporting to receive comprehensive information including how the device utilizes the battery, along with details about all the running processes.

Here are some of the commands you can run after connecting your device:

adb shell dumpsys batterystats --reset (Resets all of the data in the Battery Settings menu before generating the new dataset)
adb shell dumpsys batterystats --enable full-wake-history (Enables full Wakelock reporting)
adb shell dumpsys batterystats --charged (Enables you to receive the data since the phone was last fully charged)

If you wish to know what a Batterystats System Dump looks like, you can run the command using the command line interface in Android Studio. Take a look at the sample as follows:

We will be talking more about what each of these components mean during performance in Chapter 15, Understanding App Store Analytics for Optimization later in the book, where we will cover in detail how each of these elements help users to optimize the Android App for better performance.

Google I/O 2015 saw the launch of an updated tool called Battery Historian 2.0 (Download at: https://github.com/google/battery-historian). This was launched with new reports completely rewritten in GO. This tool provides extensive information that helps you drill down into battery data for your specific application.

To run the latest edition of Battery Historian, you will need Android Devices with versions later than Android 5.0 and Golang version 1.8.1. Please follow the installation guide given on the link above to complete the setup.

You can either save the file or check the output right from the terminal window. Exploring each component, the following are the important keywords of the stats:

  • Battery History: This shows the time series of various power-relevant events, such as the screen being on, radio signals and app launch. You can also get details of each one of these using the Battery Historian graph, which we will discuss in the next section.
  • Per-PID Stats: Shows how each process ran, including the wake time.

  • Statistics since last charge: This information provides an overall picture of what's happening with the device, to ensure no external events are affecting your experiments. Some of the statistics include, but are not limited to: phone signal levels, screen brightness, signal scanning time, time on battery, time on battery screen off, Wi-Fi signal levels, Wi-Fi idle time, and Wi-Fi Power drain (in mAh).
  • Estimated power use (mAh) by UID and peripheral: This is an extremely rough estimate and its use should avoid consideration as an experiment data.
  • Per-app mobile ms per packet: Shows the Radio-awake-time divided by packets sent - which means, since an efficient app transfers all its traffic in batches, the lower the value, the better it is for the app's performance.
  • All partial wake locks: All app-held Wakelocks, by aggregate duration and count.

Batterystats collects battery data from your device. It creates a dump of all the battery data of a particular selected device using Android Debug Bridge (ADB) commands. Using these Batterystats, the battery usage could be discovered using a Battery Historian, which would create a HTML file for viewing the Batterystats results in a browser for user viewing.

A graphical representation from Battery Historian of the live Zomato App is shown in the following screenshot:

Using the Battery Historian, important statistics can be drawn upon:

  • It shows you the processes from the device that consume your battery. Here, you could compare the battery usage for each process.
  • It also shows you the tasks from an app that consume more of the battery, so that you can take necessary action on them.

The preceding graph representation shows the readings, based on various categories of the Historian. A few of the major categories to look out for, and to analyze regarding the battery consumption for a device, are listed as follows:

  • Battery level: This shows the battery level of the device when it was recorded. It is calculated in percent where 099 is 99% battery. It helps you determine how fast the device battery drains out.
  • Screen: Shows whether the screen was ON.
  • Top: It shows the application that consumes the most battery usage. Currently the Zomato App is at the top, as the records have been taken, resetting the battery usage and using only the Zomato App.
  • Wake_lock: This is the most important field to check battery consumption. The usual process of the App is to wake up, do a small amount of work, and then go back to sleep. Waking up the app is very expensive to the battery. So if there are lot of bars showing up, it might be a problem. In this case, the app was continuously awake, hence showing a continuous wake_lock value.
  • Running: This shows if the CPU is awake. Here, it should be awake at the times when you are doing some processing, and in sleep mode when not. If it shows as running even if you are not performing any actions, it means there are background processes that are making your CPU do a lot of processing, and hence consuming the battery.
  • Wifi_running: Shows whether the Wi-Fi network was active.
  • Phone_signal_strength: Shows the signal strength of the mobile device network.

Let us now discuss how we can set up to plot Battery Historian for the App we are going to build:

  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. Find the historian.py file in the folder 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 the directory path to the path where you have saved historian.py. For example, if it is on desktop, run:
     cd ~/Desktop
  1. Shut down your running adb server :
     adb kill-server
  1. Restart adb and check for connected devices: >adb devices. This will show the list of devices.
  2. If you don't see any devices, make sure your phone is connected and USB Debugging is turned on, and then kill and restart adb.
  3. Reset battery data gathering by running the command :
     >adb shell dumpsysbatterystats --reset
  1. Resetting erases old battery collection data; otherwise, the output will be huge.
  2. Disconnect your device from your computer so that you are only drawing current from the device's battery.
  3. Use the app for a while, until it gathers a sufficient amount of data for your use.
  4. Reconnect your phone.
  5. Make sure your phone is recognized:
      >adb devices
  1. Dump all battery data. This can take a while:
      >adb shell dumpsysbatterystats> batterystats.txt
  1. Create a HTML version of the data dump for Battery Historian:
     > python historian.py batterystats.txt > batterystats.html

  1. Open the batterystats.html file in your browser.
  2. The Battery Historian tool makes it simpler to dig into the data for one single process. As a developer, you can easily spot battery drain functions using this tool and work on its resolution. In the next section, we will discuss the memory monitor tools.
主站蜘蛛池模板: 长子县| 广元市| 金秀| 花垣县| 安仁县| 筠连县| 辰溪县| 荥经县| 宣汉县| 石首市| 白玉县| 互助| 健康| 吴堡县| 邯郸市| 密山市| 福清市| 界首市| 敖汉旗| 信宜市| 资溪县| 牙克石市| 孝义市| 平塘县| 南京市| 工布江达县| 昌吉市| 醴陵市| 岳普湖县| 宁德市| 马龙县| 马公市| 舞阳县| 大化| 治多县| 抚顺市| 济南市| 托里县| 土默特左旗| 丹寨县| 搜索|