When I send / receive data using HttpWebRequest (in Silverlight ) in small blocks, I measure a very small throughput of 500 bytes / s on a "local" connection. When sending data in large blocks, I get 2 MB / s, which is 5000 times faster .
Does anyone know what could cause this incredibly large overhead?
Additional information :
- I am using the HTTP POST method
- I took a performance measurement on both Firefox 3.6 and Internet Explorer 7. Both showed similar results.
- My processor is only 10% busy (quad core, so 40% actually)
- WebClient showed similar results.
- WCF / SOAP showed similar results
Refresh . The Silverlight client code I use is essentially my own implementation of the WebClient class. The reason I wrote this is because I noticed the same performance issue with WebClient, and I thought that HttpWebRequest would allow for performance tuning. Unfortunately, this did not work. The implementation is as follows:
public class HttpCommChannel
{
public delegate void ResponseArrivedCallback(object requestContext, BinaryDataBuffer response);
public HttpCommChannel(ResponseArrivedCallback responseArrivedCallback)
{
this.responseArrivedCallback = responseArrivedCallback;
this.requestSentEvent = new ManualResetEvent(false);
this.responseArrivedEvent = new ManualResetEvent(true);
}
public void MakeRequest(object requestContext, string url, BinaryDataBuffer requestPacket)
{
responseArrivedEvent.WaitOne();
responseArrivedEvent.Reset();
this.requestMsg = requestPacket;
this.requestContext = requestContext;
this.webRequest = WebRequest.Create(url) as HttpWebRequest;
this.webRequest.AllowReadStreamBuffering = true;
this.webRequest.ContentType = "text/plain";
this.webRequest.Method = "POST";
this.webRequest.BeginGetRequestStream(new AsyncCallback(this.GetRequestStreamCallback), null);
this.requestSentEvent.WaitOne();
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
System.IO.Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
postStream.Write(requestMsg.Data, 0, (int)requestMsg.Size);
postStream.Close();
requestSentEvent.Set();
webRequest.BeginGetResponse(new AsyncCallback(this.GetResponseCallback), null);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
Dim.Ensure(streamResponse.CanRead);
byte[] readData = new byte[streamResponse.Length];
Dim.Ensure(streamResponse.Read(readData, 0, (int)streamResponse.Length) == streamResponse.Length);
streamResponse.Close();
response.Close();
webRequest = null;
responseArrivedEvent.Set();
responseArrivedCallback(requestContext, new BinaryDataBuffer(readData));
}
HttpWebRequest webRequest;
ManualResetEvent requestSentEvent;
BinaryDataBuffer requestMsg;
object requestContext;
ManualResetEvent responseArrivedEvent;
ResponseArrivedCallback responseArrivedCallback;
}
I use this code to send data back and forth to an HTTP server.
Update : After extensive research, I came to the conclusion that the problem is inherent in Silverlight v3 .