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

Re: Calculating download speed from HTTP Stream by Oliver

Oliver
Thu Oct 20 10:48:50 CDT 2005

Annette Miller wrote:

>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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Re: Calculating download speed from HTTP Stream by Annette

Annette
Sun Oct 23 00:44:59 CDT 2005

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.

"Oliver Sturm" <oliver@sturmnet.org> wrote in message
news:xn0e8q0pl38dw2m000@msnews.microsoft.com...
> Annette Miller wrote:
>
>>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
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)



Re: Calculating download speed from HTTP Stream by Oliver

Oliver
Sun Oct 23 09:37:52 CDT 2005

Annette Miller wrote:

>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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Re: Calculating download speed from HTTP Stream by Annette

Annette
Tue Oct 25 20:24:46 CDT 2005

Hey thanks Oliver - works a treat!

Cheers.
"Oliver Sturm" <oliver@sturmnet.org> wrote in message
news:xn0e8u6ka7g6ahv001@msnews.microsoft.com...
> Annette Miller wrote:
>
>>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
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)