Introduction
Using power effeciently is essential to making a successful app. Not only is it rude to drain your users’ batteries but if their phones run out of battery quickly they physically can’t use your app. Sadly, the built-in Instruments energy diagnostics are fairly limited. You can see which major power draws (GPS, screen, cell radio, etc) are currently on but there’s no way to see how much current the device is currently drawing. This is the crucial piece of information because ultimately you want to be able to answer two basic questions:
- How long will the battery last when running my app?
- How much is the battery life extended when I perform a particular optimization?
Powergremlin leverages private APIs to measure the battery’s current capacity over time and determine current draw. Using this information you can easily answer these questions.
Tutorial
First you’ll need to clone the repo from Github. The README has instructions for building; essentially open in XCode and run.
Now, let’s imagine you’re working on off-brand Foursquare. Your users complain that even an hour of use decimates their batteries. Intuitively you know that doing things like reducing CPU usage, turning off location services as much as possible, minimizing network traffic and so on should help but it’s risky to start making optimizations without profiling. So let’s profile! Here’s what you do:
- Make sure that location services aren’t active. If you have a solid arrow in the status bar then something is using location services and you should turn it off so that it won’t skew results. A hollow arrow indicates geofencing. Remove any reminders that depend on a location to make it go away.
- Now restart your phone to minimize the number of background processes. Background processes can cause CPU activity and network activity that would skew your results.
- Start Powergremlin and tap “Reset”.
- Use your app for at least 15mins, preferably more like 30mins.
- Go back to Powergremlin. The cruical figure in this case is “Current Draw (mA)”. It indicates the average current draw since you tapped “Reset”. You divide the battery capacity in milliamp-hours by the average draw in milliamps to get a rough sense of how long your battery will last while running your app.
Now that you have a baseline you can try implementing different optimizations and check their improvements. For example, if your baseline is a draw of 300 mA and after switching to a less chatty API the draw goes down to 270 mA then you just bought an iPhone4 user an extra 30 minutes.
As you embark on your power profiling adventures there are several things to keep in mind:
- Never test while on external power. Powergremlin doesn’t take into account the current supplied by the charger so the numbers are incorrect. Also, even if Powergremlin handled this case gracefully you’d never want to do this because the phone uses much more power when charging.
- Batteries degrade over time. A brand new iPhone 4 has a capacity 1420 mAh. My year old iPhone, however, has a capacity of 1350 mAh. In practice most of your users’ phones will have batteries with less capacity than what the specs claim.
- Power usage is extremely variable. Always make several measurements of at least 15 minutes each.
- Test usage in the real world as much as possible. The power characteristics of an iPhone on WiFi in an area with good cell coverage are radically different from those of a phone in an an area with only spotty EDGE.