2

Really confused with bandwidth calculation formula.
Referring to the bandwidth detection question Check the bandwidth rate in Android i am trying to calculate bandwidth as following.

long startTime = System.currentTimeMillis();
HttpResponse response = httpclient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
long contentLength = httpEntity.getContentLength();
long endTime = System.currentTimeMillis();
float bandwidth = contentLength / ((endTime-startTime) / 1000); // In the refereed question it is "((endTime-startTime) * 1000)" don't know why multiplication. 

What i need is current bandwidth in bits (not bytes or kbytes). I don't know using above formula if it is calculating bandwidth in bytes or bits.

also if download time is 0 seconds then what should be the bandwidth is it downloaded content length. can someone please suggest correct formula to calculate bandwidth in bits.

Community
  • 1
  • 1
User7723337
  • 11,857
  • 27
  • 101
  • 182

4 Answers4

5

First of all for precision's sake, you should use 1000.0 to convert to seconds since you are assigning your bandwidth to a float variable:

float bandwidth = (contentLength / (endTime-startTime)) / 1000.0;

Now since your contentLength is measured in bytes, you need to convert to bits (Kb, Mb, etc). There are 8 bits to each byte and socontentLength*8 converts bytes to bits.

Kilo->Mega->Giga ->... scale conversion for the unit of bits is on the order of 1000 which means converting bits to Megabits requires a division by 1000*1000. All this put together should yield:

int bits = contentLength * 8;
int megabits = contentLength / (1000*1000); //Megabits
float seconds = endTime-startTime / 1000.0;
float bandwidth = (megabits / seconds);  //Megabits-per-second (Mbps)

EDIT #1: If bandwidth is denominated by Bytes/Time (like KB/s for example), scale conversion is on the order of 1024

int bytes = contentLength;
int kilobytes = contentLength / 1024; //Kilobytes

EDIT #2: The definition of "Mega" and "Kilo" etc. when talking in terms of bandwidth can be somewhat ambiguous I am realizing. Often times 1024 (210) and 1000 (103) may be used interchangeably (most likely an accident). For many cases 1024 may be favored as the order of magnitude when calculating bandwidth as memory storage space on computers is measured in base 2. However network bandwidth is usually controlled by the clock speed of a CPU which regulates the transmission of bits, and this rate is measured in hertz (MHz to be exact) which is on the order of magnitude of 1000, not 1024. However in most cases, these two numbers are close enough to not produce significant error.

Phil
  • 401
  • 8
  • 18
  • But i want it in bits not in bytes or kbytes. so `float bandwidth` is in bits right? – User7723337 Mar 20 '13 at 10:10
  • `bandwidth`'s unit of measurement depends on which conversion you use. Since `getContentLength()` returns an amount in bytes, you must convert to bits by multiplying by 8. – Phil Mar 20 '13 at 10:18
  • I have to compare the calculated bandwidth with some calculated bits to check if it in that particulate range, so i want the bandwidth to be in bits. yes `getContentLength` is returning number of bytes but can i convert the bandwidth calculation to bits? – User7723337 Mar 20 '13 at 10:30
3

Based on your link that you gave its commented that its // Bandwidth : size(KB)/time(s) so just multiply it by 1024 to get bytes or 1024*8 to get it in bits, And if the download time is 0 then technically nothing has been downloaded because speed is amout/time

1.For calculating in bits just use

float bandwidth = contentLength / ((endTime-startTime) *(1000*1024*8));

2.If the dowload time is 0 then nothing has been downloaded so we cannot suggest the bandwith

EDIT 2

CALCULATION

Thats just simple math

If you have kb per second like 1 kb/ per 1 second

so speed is 1 kbps => 1*(1024 bytes)/ 1* (1000000000 nano seconds)

so its 0.000001024 bytes per nano seonds

now in bits would be 1*(1024 * 8 bits)/ 1* (1000000000 nano seconds)

so its 0.000008192 bits per nano seconds

now if you want it in seoncds then just multiply it by 1000000000

so its 0.000008192 * 1000000000 = 8192 bits per second

and (0.000008192/8) * 1000000000 = 1024 bytes per second or 1 kbps

Community
  • 1
  • 1
Girish Nair
  • 5,148
  • 5
  • 40
  • 61
  • If download time is 0 seconds that means download is taking time in Milli seconds also as per your calculations if contentLength is 51200 (50KB) then bandwidth i am getting as 0. – User7723337 Mar 20 '13 at 09:17
  • The code that your using has the code `System.currentTimeMillis()`. So the answer that which is the difference `(endTime-startTime)` is in milli seconds only right ??. If your gettimg time as 0 then have you checked the log weather the `startTime` and `endTime` are being recorded or are having 0 values ?? – Girish Nair Mar 20 '13 at 09:21
  • yes start time and end time are recorded and my http request is taking less than 0 seconds to download. so using your formula i am getting bandwidth value as 0 for content length of 51200 bytes. – User7723337 Mar 20 '13 at 09:26
  • Did you try using nanoTime instead of milli seconds `long startTime = System.nanoTime();` `long endTime= System.nanoTime() - startTime;` – Girish Nair Mar 20 '13 at 10:42
  • Actual problem that i am facing is, i want bandwidth in bits not bytes/MB/kbytes. i can get system time in nano/mills/seconds but i am gettign stuck in calculating bandwidth in bits. – User7723337 Mar 20 '13 at 12:16
3
public static String internetSpeed(long msecs, long bytes)
{
    long  secs  = msecs / 1000;                                     
    long  bits  = bytes * 8;
    float speed = bits / secs;

    long  Kbit  = 1024;
    long  Mbit  = Kbit * 1024;
    long  Gbit  = Mbit * 1024;

    if (speed < Kbit )                  return String.valueOf(speed)        + " bit-sec" ;
    if (speed > Kbit && speed < Mbit)   return String.valueOf(speed / Kbit) + " Kbit-sec";
    if (speed > Mbit && speed < Gbit)   return String.valueOf(speed / Mbit) + " Mbit-sec";
    if (speed > Gbit)                   return String.valueOf(speed / Gbit) + " Gbit-sec";
    return "???";
}

You can call this method like

....
System.out.println(internetSpeed((endTime-startTime), contentLength));
....
XXX
  • 8,996
  • 7
  • 44
  • 53
1

You can download a known-size file from your server.

long startTime = System.currentTimeMillis();
HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
long endTime = System.currentTimeMillis();

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity;
bufHttpEntity = new BufferedHttpEntity(entity);

//You can re-check the size of your file
final long contentLength = bufHttpEntity.getContentLength();

// Log
Log.d(TAG, "[BENCHMARK] Dowload time :"+(endTime-startTime)+" ms");

// Bandwidth : size(KB)/time(s)
float bandwidth = contentLength / ((endTime-startTime) *1000);

and get wifi speed directly by this code

WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
    Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}
XEENA
  • 569
  • 2
  • 6