Live data from Hacker News

If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

highscalability.com

61–70 of 77 posts

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#61

Earlier quoted context omitted.

Respecting known limitations and working within best practices is absolutely not premature optimization. If you know that using the mobile radio in a certain way is a source of excessive battery usage, it's silly to just disregard this information. It's not premature optimization if you know where the performance issues are from the outset, and these performance issues are so incredibly common that the Android team p…

I just want to add in from a network carrier perspective. I work for a mobile operator, and have had to work on several network issues related to poorly designed apps. I agree with kintamanimatt on this, it's not premature optimization, you need to know the cost of what you're doing. There is an incredible resource cost, to all the naive developers making apps these days, that treat the wireless interface as an alway…

Thank you for this insider's insight. It's not something you come across every day!

Aside from these things, is there anything else as a mobile developer that I can do to make your lives happier? I'm not guilty of any of those things you've mentioned and try to write well-behaving apps, but I'm always on the look-out to find out about stuff I don't even know I don't know!

Also, perhaps you could you lobby your higher ups to put together some kind of best practice guide from a network operator's perspective. I know the Android team do touch on this in their guides and in the Google I/O conference talks, but I'm guessing there's additional information that would be beneficial.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#62
Since Android is an open system, would it be possible to write an app that runs on a phone and forces applications to batch transfers?

I'm thinking of a "limiter" app that runs locally on the phone, and disables TCP/IP transmissions every 18 minutes out of 20. That way things like email you can still get relatively quickly, but the radio's only on 10% of the time.

Developers and users both being aware of the problem and trying to fix it might mean their solutions collide. E.g. a badly behaved app that tries to access the network once every 30 seconds will always get through during the 2-minute window, but a well-behaved app that only hits the internet every 10 minutes might miss the window entirely.

So maybe the limiter should intercept outgoing TCP/IP connections, lie to the initiating app and tell it the connection happened but the remote end isn't sending any data, then connect the outgoing app to the real remote end when the radio switches on. Of course if there's an application-controlled timeout interval on the connection, it'll trigger and the 10-minute-interval app would miss the window.

You might get around that problem by using whatever UNIX signal Ctrl+Z generates, to lock up an app entirely when it tries to open an outgoing connection, then resume it when the radio's on. Of course, this'll probably lock the app's UI too, so it'll become totally unusable. Although if the limiter acts like a screensaver and goes away whenever the user's recently given any touch or keyboard input, that doesn't really matter, does it?

Or maybe Google should make an API where an app can say, "I want to make a connection, and I'm willing to wait if turning the radio on right now wouldn't be good for the user."

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#63

> Every decision you make should be based on minimizing the number of times the radio powers up. This is lunacy. Ok, lunacy is a bit strong. But I disagree with this and am throwing a "premature optimization" flag. Modern phone batteries last plenty long, and the radio being on is nothing compared to the big bright screen. /edit Given the opportunity to chose, I know I would gladly sacrifice a few minutes per charge…

Respecting known limitations and working within best practices is absolutely not premature optimization. If you know that using the mobile radio in a certain way is a source of excessive battery usage, it's silly to just disregard this information. It's not premature optimization if you know where the performance issues are from the outset, and these performance issues are so incredibly common that the Android team p…

The thing is that recognizing something as premature optimization isn't the same as disregarding it. Developers that spend all of their resources optimizing for minimal battery life impact or other minutia are just as bad as developers who refuse to publish their SaaS app until they're confident that the infrastructure will support one million concurrent users.

Release early, release often. If users love your app after release, focus on fixing the littler things like battery usage. If users hate it, try to fix the things that make them hate it, and then focus on the battery life.

Caveat: As in all optimization discussions, there are reasonable minimum thresholds for acceptability even in the "unoptimized" state. Desktop software can't take thirty minutes to boot no matter what stage of life it's in, and mobile software can't kill your battery in an hour or crash the nearby cellular network at any point either.

Don't make your software egregiously bad; just make it good enough.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#64

Earlier quoted context omitted.

The difference between DCH (the full-on high power state) and PCH (the lowest power, waiting-for-paging state) is about 2-orders of magnitude. Last time I measured, it was about ~100mA vs ~1mA (at ~3.7V) used by the radio. So, it's not couple of minutes of battery life, but rather _many_ hours of standby life instead. People usually aren't very happy when a fully charged phone dies overnight. I've seen apps do some c…

That kind of frequent-on behavior is why the iPhone 5S has the new M7 chip.

What the hell? The M7 is a marketing label for some COTS silicon they licensed from some IP company somewhere (probably ARM/Broadcom or one of their associates).

It doesn't make unicorns shit rainbows or defy the laws of physics.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#65
post #62

Since Android is an open system, would it be possible to write an app that runs on a phone and forces applications to batch transfers? I'm thinking of a "limiter" app that runs locally on the phone, and disables TCP/IP transmissions every 18 minutes out of 20. That way things like email you can still get relatively quickly, but the radio's only on 10% of the time. Developers and users both being aware of the problem…

You can do similar to what you want right now: pass to AlarmManager flag ELAPSED_REALTIME, and your event won't be delivered, until device is waked up.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#66
post #7

Great article, made me explicitly aware of something I had at the back of mind as a web developer. Another point that struck me is that when I briefly used Android more than a year ago, there was a nifty tool that showed which app drained what %-age of the battery. If I am having troubles with my battery life on Android, I am likely to uninstall (or not use) an app that consumes more than its fair share of the batter…

The problem with the battery statistics is that even though an app that is causing battery drain it might not be registered with that app. Imagine an app that sends a 1kb probe message every 15 seconds in the background thus keeping the radio alive the entire time (i.e. never allowing it to idle). I believe that the battery drain caused by the radio usage would count towards "Android System" and not the app when in reality the app would be shortening your phone battery lifetime significantly.

Assigning battery usage to apps is a hard thing as there are so many grey areas. OTOH you will notice apps that are stuck in CPU expensive loops that will not terminate, however this is not that common in my experience.

The Android battery statistics are nice but they usually don't provide me with a useful answer to the question: Which app is really draining my battery.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#67
So what happens in a situation like long polling?

Suppose no data is being transferred for 60 seconds ... is the radio kept on at maximum power? Or does it power down in between? Or does it turn off and break the poll (abort the TCP connection)?

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#68

Earlier quoted context omitted.

I can understand what dllthomas is talking about. I'd never write a full-blown prototype, but whenever I'm writing a non-trivial app for myself it can be quite productive to write 10% of the app, throw that away, and start again. This strategy always gives me a better understanding of the problem, the app's architecture and so on, as well as a ton of new ideas.

Right, that's pretty much what I was talking about. Not so much something "officially blessed as a prototype" but "does this make sense at all?" experimentation shy of an MVP. I've heard "write it, throw it away, rewrite it" attributed to Knuth, and while that's overstating it a little you definitely learn things in your first attempt.

Fred Brooks, maybe?

> The management question, therefore, is not whether to build a pilot system and throw it away. You will do that. […] Hence plan to throw one away; you will, anyhow.

But as someone on Ward's Wiki said http://c2.com/cgi/wiki?PlanToThrowOneAway ,

> The corollary to this is that if you plan to throw one away, you will end up throwing away two.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#69
post #62

Since Android is an open system, would it be possible to write an app that runs on a phone and forces applications to batch transfers? I'm thinking of a "limiter" app that runs locally on the phone, and disables TCP/IP transmissions every 18 minutes out of 20. That way things like email you can still get relatively quickly, but the radio's only on 10% of the time. Developers and users both being aware of the problem…

My phone (Android 2.3) shipped with something similar. I don't know how to find it on more modern phones, but it's probably there.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#70

> Every decision you make should be based on minimizing the number of times the radio powers up. This is lunacy. Ok, lunacy is a bit strong. But I disagree with this and am throwing a "premature optimization" flag. Modern phone batteries last plenty long, and the radio being on is nothing compared to the big bright screen. /edit Given the opportunity to chose, I know I would gladly sacrifice a few minutes per charge…

Respecting known limitations and working within best practices is absolutely not premature optimization. If you know that using the mobile radio in a certain way is a source of excessive battery usage, it's silly to just disregard this information. It's not premature optimization if you know where the performance issues are from the outset, and these performance issues are so incredibly common that the Android team p…

>Respecting known limitations and working within best practices is absolutely not premature optimization.

Absolutely.

I'm not saying throw caution to the wind and poll a remote host constantly and indefinitely, as I said originally, I think it's right to be concerned about resource usage. This is just a baseline for decent software. Be a "good citizen" and all that. (In most cases) If you can make one query for 100 items, rather than 100 queries, or 10 queries, do that. But that's just common sense.

>Excessive power drain is undoubtedly a bug. I absolutely love apps that respect the fact I want to go as long as possible without charging my phone.

Again, I think we are in agreement. Excessive drain is a bug, but my experience is that if you're already following best practices and not doing anything wild, you won't be a battery hog.

The article makes a HUGE list of do's and don'ts

I think it can be a lot shorter:

* just because you can, doesn't mean you should

* learn and follow best practices

* be mindful and conservative in your usage of all resources on the system

> I might often be in a position where I can't charge my phone too. (Carrying a second or third battery is useful, if you've got a phone that has a replaceable battery.)

(gadget aside) Even if your phone doesn't have a replaceable battery, there are a lot of slim, rechargeable battery packs you can buy for around $20, many with short little USB cords that fold up in to them and others where you can use your own cord as well. The nice part is it will still be usable even if you change phones, and can be shared with other devices and people if needed. Some of them will even let you charge them inline with the phone so you're only using one usb port.

What are you doing on your phone/what phone do you have that you need 3 batteries?

Post reply on HN