Google I/O: How to build battery-efficient apps
The faster an app responds, and the longer the battery lasts, the happier users will be with an app. Both aspects can be significantly influenced when a program is developed. Developers should pay particular attention to the special data transmission characteristics of mobile phones. Most of the time, but not always, it helps to reduce the amount of data that is generated. Requests that are never made use the smallest amount of power, which is why caching should be used wherever possible.
In his closing presentation at the Google I/O conference, Ilya Grigorik presented a practical example: When an early version of the Pandora app was checked by a mobile network operator, it was found to be very power hungry. Just 0.2 per cent of the transmitted bytes were responsible for 46 per cent of the app's power consumption: the tracking beacons. This is due to the design of the common UMTS and LTE mobile telephony standards, which are optimised to transmit large amounts of data in short bursts.
To send a data packet, a 3G mobile activates the modem for at least 100ms. When transmission is complete, the modem switches to a state that consumes less power (FACH), but it will only go into standby (idle) after ten seconds. Therefore, sending a ping every five seconds, which is what Grigorik says he observed with analytics on the CNN.com website, is the best way of draining a battery quickly: the modem will never go into standby.
Nevertheless, usage can be monitored almost in real-time. Data packets that are to be sent should be pooled and then transmitted when a larger amount of data has accumulated. Grigorik says that Google Analytics has been optimised in this way.
Data saving also benefits battery life. The use of compression techniques and modern formats such as WebP for images or VP9 for videos, as well as intensive caching, are all battery-friendly approaches. After the display, the modem, rather than the processor, is the component with the highest power consumption in most mobile phones.
While caching reaches its limits during live streams, streaming pre-recorded content is a different matter. Sometimes users themselves can contribute as well, for example by downloading certain data in advance, as is possible in the mobile version of Google Maps; when using a Wi-Fi connection, this can also benefit the user's phone bill.
Caching also increases an app's perceived speed. When data must be transmitted, help is at hand via technologies such as HTTP 2.0 or SPDY, and optimised TCP server stacks. Another important factor are long timeouts for the TCP connection itself. Even in an HSPA network, the network latency overhead is 0.6 to 3.5 seconds. A user that is driving going through a short tunnel could soon emerge again. Grigorik says that servers often set their TCP timeouts too aggressively. Rebuilding the connection later costs time and power. Mobile networks themselves have TCP timeouts of 5 to 30 minutes, said the developer. It's a particularly bad idea to wake up the modem just to close the TCP connection after a certain amount of idle time, he added.
Less is therefore generally more, but sometimes more can also be less. The latter applies to very small amounts of data. Below a certain threshold, modern mobile networks transfer data on what is called the control plane, a channel that is otherwise used to exchange control data between the mobile phone and the network. Compared to normal data transmission pathways, this channel has a high latency.
Apparently, Google itself experienced this with its search app that is supposed to present suggestions while the user is still typing. Very high latencies were observed with this app. It turned out that the individual characters' data amounts were so small that they were processed via the mobile network's control plane. Performance improved abruptly when Google added a simple stack of empty bits. However, the threshold that defines whether data is transmitted on the control plane varies between networks and can't be changed by the network provider without prior notification.
US mobile telephony provider AT&T has provided information and tools to help developers create power-efficient designs. Grigorik himself has made an early, rough draft of his book on High Performance Browser Networking available free of charge.
(Daniel AJ Sokolov / djwm)