Calculating download speed from HTTP Stream

  • Thread starter Thread starter Annette Miller
  • Start date Start date
A

Annette Miller

Hi All,

In my app I am using the following code to read from the response stream of
a web page.

...
do
{
bytesRead = receiveStream.Read(byteArray, 0, 256);
receiveLocal.Write(byteArray, 0, bytesRead);
} (while bytesRead > 0)
...

where receiveStream is the Response Stream from the HttpWebRequest and
receiveLocal is a memory stream. Now I'm just wondering - what is the best
way to work out the download speed per second.

Thanks in advance...
Ann
 
Annette said:
In my app I am using the following code to read from the response stream
of a web page.

..
do
{
bytesRead = receiveStream.Read(byteArray, 0, 256);
receiveLocal.Write(byteArray, 0, bytesRead);
} (while bytesRead > 0)
..

where receiveStream is the Response Stream from the HttpWebRequest and
receiveLocal is a memory stream. Now I'm just wondering - what is the best
way to work out the download speed per second.

Well, to extend your sample, maybe something like this:

int bytesTotal = 0;
int bytesPerSecond = 0;
DateTime startTime = DateTime.Now;

do {
bytesRead = receiveStream.Read(byteArray, 0, 256);
bytesTotal += bytesRead;
receiveLocal.Write(byteArray, 0, bytesRead);
bytesPerSecond = bytesTotal / (DateTime.Now - startTime).TotalSeconds;
} while(bytesRead > 0);

Be aware that there are several different algorithm of calculating the
average throughput - the status dialogs for file copying or program
installation are famous for their inaccuracy in "time left" prognoses,
which is one example of an algorithm that's often not appropriate for the
task.

But this depends on what exactly you're going to do with the calculated
values. The average value calculated by my sample is not wrong, but
depending on its purpose there could be more useful ways to do the same
(or a very similar) thing.


Oliver Sturm
 
Thanks Oliver - that works awesomely!
As for it being the average speed, what do you think would be the best way
to have it more accurate - reset the start time every say, 50 or 100 loops?

Cheers.
 
Annette said:
As for it being the average speed, what do you think would be the best way
to have it more accurate - reset the start time every say, 50 or 100 loops?

If you're sending data over a WAN connection, the resulting average will
probably be more realistic if you calculate it only over a number of most
recent values, as opposed to all the data points - simply because the
download speed will probably vary over the whole duration. With a LAN
connection this might not be necessary, but it wouldn't hurt either.
Problem is, this type of calculation is much harder to get right, because
just starting over will make the average value lose significance for a
while - an average value calculated over 100 samples is quite exact, but
it would be followed by an average value calculated over only 1 sample,
then two and so on, which isn't exact at all.

I found the topic interesting and I just created a blog post about it,
complete with a class that calculates your running average for you. Find
it here:

http://www.sturmnet.org/blog/archives/2005/10/23/running-average/


Have fun!



Oliver Sturm
 
Hey thanks Oliver - works a treat!

Cheers.
Oliver Sturm said:
If you're sending data over a WAN connection, the resulting average will
probably be more realistic if you calculate it only over a number of most
recent values, as opposed to all the data points - simply because the
download speed will probably vary over the whole duration. With a LAN
connection this might not be necessary, but it wouldn't hurt either.
Problem is, this type of calculation is much harder to get right, because
just starting over will make the average value lose significance for a
while - an average value calculated over 100 samples is quite exact, but
it would be followed by an average value calculated over only 1 sample,
then two and so on, which isn't exact at all.

I found the topic interesting and I just created a blog post about it,
complete with a class that calculates your running average for you. Find
it here:

http://www.sturmnet.org/blog/archives/2005/10/23/running-average/


Have fun!



Oliver Sturm
 
Back
Top