Quando invio/ricezione di dati tramite HttpWebRequest (su Silverlight) in piccoli blocchi, misuro il molto piccola portata di 500 byte/s per un "localhost" connessione. Quando si inviano i dati in blocchi grandi, ottengo 2 MB/s, ovvero 5000 volte più veloce.Che cosa potrebbe causare il grande sovraccarico di effettuare una chiamata HttpWebRequest?
Qualcuno sa cosa potrebbe causare questo sovraccarico incredibilmente grande?
Ulteriori informazioni:
- sto usando il metodo HTTP POST
- ho fatto la misurazione delle prestazioni sia su Firefox 3.6 e Internet Explorer 7. Entrambi hanno mostrato risultati simili.
- mio CPU è caricato solo il 10% (quad-core, quindi il 40% in realtà)
- WebClient ha mostrato risultati simili
- WCF/SOAP showed similar results
Aggiornamento: il codice di Silverlight lato client che uso è essenzialmente la mia implementazione della classe WebClient. Il motivo per cui l'ho scritto è perché ho notato lo stesso problema di prestazioni con WebClient e ho pensato che HttpWebRequest avrebbe consentito di modificare il problema delle prestazioni. Purtroppo, questo non ha funzionato. L'implementazione è la seguente:
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;
}
Io uso questo codice per inviare dati avanti e indietro su un server HTTP.
Aggiornamento: dopo approfondite ricerche, concludo che the performance problem is inherent to Silverlight v3.
Si prega di mostrare un po 'di codice - è davvero difficile dire cosa sta succedendo senza vedere il codice. –