Dim*_* C. 5 .net silverlight performance http httpwebrequest
当我使用小块中的HttpWebRequest(在Silverlight上)发送/接收数据时,我通过"localhost"连接测量500字节/秒的非常小的吞吐量.当以大块发送数据时,我得到2 MB/s,这快了5000倍.
有谁知道什么可能导致这个令人难以置信的巨大开销?
附加信息:
更新:我使用的Silverlight客户端代码本质上是我自己的WebClient类实现.我写它的原因是因为我注意到WebClient存在相同的性能问题,我认为HttpWebRequest可以调整性能问题.遗憾的是,这没有用.实施如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
我使用此代码来回传输数据到HTTP服务器.
更新:经过广泛的研究,我得出结论,性能问题是Silverlight v3所固有的.
您很可能正在见证 Nagle 算法的效果,请尝试:
this.webRequest.UseNagleAlgorithm.ServicePoint = false;
Run Code Online (Sandbox Code Playgroud)
此外,Expect100Continue“握手”与 SOAP 服务调用性能相关:
this.webRequest.Expect100Continue.ServicePoint = false;
Run Code Online (Sandbox Code Playgroud)
UDP日期:
刚刚意识到 ServicePoint 在 Compact Framework 中不可用。但是,您可以通过执行以下操作来证明这一点:
ServicePointManager.UseNagleAlgorithm = false
Run Code Online (Sandbox Code Playgroud)
或者更改应用程序配置文件中的相关设置,或者 silverlight 中的任何等效设置?
归档时间: |
|
查看次数: |
1071 次 |
最近记录: |