2

Hi I'm running a simple download test to see how fast my connection is. I record the trafficStats rx value, then I download a 5MB file, record the rx value afterwards then calculate the difference. I know this file is exactly 5MB.

Here is the code:

URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(connectTimeout);
urlConnection.setReadTimeout(readTimeout);

InputStream is = urlConnection.getInputStream();
// BufferedInputStream bis = new BufferedInputStream(is);
InputStream bis = new BufferedInputStream(is, 8192);
int inputFileSize = 1024;
byte[] baf = new byte[inputFileSize];

totalRxBeforeTest = TrafficStats.getUidRxBytes(uid);

int current = 0;
while ((current = bis.read(baf)) != -1) {
}
totalRxAfterTest = TrafficStats.getUidRxBytes(uid);
rxDiff = totalRxAfterTest - totalRxBeforeTest;

The issue is, that I should be getting a value of around 5242880 bytes (5MB), but instead I'm getting values like 5414730, 5408715, 5413867, 5426647. (From 4 test runs) I would expect there to be some overhead but this is an extra 168KB!! That seams like a lot of overhead for a 5MB file. I'm using TrafficStats by uid as well, so it should just be network stats from my particular application.

Any idea what could account for this extra traffic?

flx
  • 14,146
  • 11
  • 55
  • 70
Dave
  • 3,178
  • 5
  • 28
  • 44
  • Why is 3% a lot of overhead? There are HTTP header, there are TCP header, there are IP header and even ethernet headers. you should sniff the traffic and analyse this first. – flx Feb 26 '14 at 02:52
  • You may be right. I'm no network expert, this just seams like an excessive amount of header traffic. I've tried sniffing the traffic, but I cannot root my device. The only real option I've found for sniffing on Android without root is tPacketCapture which I get a SocketTimeoutException using the code above. I do not get this exception with the code otherwise. Also, I would have hoped that a lot of that type of traffic would be when I open the connection. If you look at where I'm getting the rxBytes it is after the connection is opened, and just before I start reading from the input stream. – Dave Feb 26 '14 at 03:41
  • Interesting, from this it seems that TrafficStats actually counts the header bytes. I read this [answer](http://stackoverflow.com/a/15774056) which states that header bytes are not included in the count. I'm looking for a way to measure ALL bytes - including headers - sent/received when downloading a file. – Janus Varmarken Oct 22 '14 at 23:36

0 Answers0