WebRequest / WebResponse performance issues

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I am trying to download a file using WebRequest / WebResponse using
HTTP over ActiveSync. I can successfully download the file but I am
seeing very poor performance (I have attached a copy of the code I am
using, below).

Copying a 4.6 MB file directly using ActiveSync takes 13 seconds.
Copying the same file using HTTP in 32 KB blocks is taking 83 seconds.
If I copy the file in 1000 byte blocks, I can get the time down to 40
seconds.

The really strange bit is that the code runs faster if I put a short
Thread.Sleep call in after each read. These are the results of some
tests I ran (I ran some of these multiple times and each time the
result was within +/- 2 seconds):
File size 4,664kb

Time Block Sleep
39.75 1000 None
58.80 15000 50
45.85 32769 100
27.34 32769 50
17.91 32769 25
17.28 32769 20
84.00 32769 15
84.17 32769 10
83.93 32769 0
83.83 32769 None

Where 'Time' is the time in seconds to complete the download, 'Block'
is the block size I used and 'Sleep' is the length of sleep between
each read call.

I am guessing that .Net has a background thread for the downloading
which is getting starved by the application thread if it does not sleep
between Read calls.

Is there a good way of doing this? I would like to be able to download
a file over HTTP as quickly as possible without resorting to ugly hacks
like Thread.Sleep.

This is the code I am testing with:

int tickCountStart = Environment.TickCount;

const int blockSize = 32769; // change this line to test block sizes
const int sleepTime = 20; // change this line to test sleeps

WebRequest request = WebRequest.Create("http://ppp_peer/bigfile.dat";);

using (WebResponse response = request.GetResponse())
{
int remaining = (int)response.ContentLength;

using (Stream stream = response.GetResponseStream())
{
byte[] buffer = new byte[blockSize];

while (remaining > 0)
{
remaining -= stream.Read(buffer, 0, Math.Min(blockSize,
remaining));
Thread.Sleep(sleepTime);
}
}
}

int tickCountEnd = Environment.TickCount;

MessageBox.Show(string.Format("Time taken {0:n}", (tickCountEnd -
tickCountStart) / 1000.0));



Thanks

Jon Payne

.



Relevant Pages

  • Re: Assembly conversion to Pocket Pc format
    ... string strfilename = ... for .NET assemblies, i.e. it is simply passing them through unmodified. ... rather I want to download from HTTP or HTTPS. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: HTTP Download complete detect using TCP sniffer
    ... its HTML have been received. ... HTTP parsing" would tell me the HTTP download is complete please? ... When the request is sent you start ...
    (microsoft.public.win32.programmer.networks)
  • Re: Updating a VB6 across the Internet
    ... Can you host the update download on an HTTP? ... I wrote an update function into the program using FTP calls. ... Many corporate firewalls do not allow users to use FTP. ...
    (comp.lang.basic.visual.misc)
  • Re: Bugs in http
    ... of-line translations are meaningful for ... download a tcl source file and try to edit ... it would mean that changing the encoding would be ... transform, great, but it isn't part of http and the http part of the ...
    (comp.lang.tcl)
  • Re: IPFilter Questions
    ... I would be assuming that it is http since the port that is in the output ... from ipmon is 80, however if it were trying passive ftp this would cause ... >> go to Microsoft's download page and try to download something, ...
    (FreeBSD-Security)