什么可能导致作出的HttpWebRequest调用的大开销?开销、HttpWebRequest

2023-09-03 07:42:06 作者:い 第 一 先 生 が

当我发送/接收使用数据的的HttpWebRequest 的的(对的Silverlight )的小块,我测量的很小吞吐量500字节/结束了一个本地主机连接。当发送大块的数据,我得到2 MB / s,这是一些快5000倍。

When I send/receive data using HttpWebRequest (on Silverlight) in small blocks, I measure the very small throughput of 500 bytes/s over a "localhost" connection. When sending the data in large blocks, I get 2 MB/s, which is some 5000 times faster.

有谁知道这是什么原因令人难以置信的大开销?

Does anyone know what could cause this incredibly big overhead?

其他信息

在我使用HTTP POST方法 在我做了两个火狐3.6和Internet Explorer 7的性能测试都显示了类似的结果。 在我的CPU加载只有10%(四核,所以40%实际上) 在Web客户端显示类似的结果 WCF/SOAP显示类似的结果 I'm using the HTTP POST method I did the performance measurement on both Firefox 3.6 and Internet Explorer 7. Both showed similar results. My CPU is loaded for only 10% (quad core, so 40% actually) WebClient showed similar results WCF/SOAP showed similar results

更新:Silverlight的客户端code我用的基本上是我自己的实现WebClient类的。我之所以写了那是因为我发现同样的性能问题与Web客户端,我认为的HttpWebRequest将允许调整性能问题。遗憾的是,并没有工作。实施如下:

Update: The Silverlight client-side code I use is essentially my own implementation of the WebClient class. The reason I wrote it is because I noticed the same performance problem with WebClient, and I thought that the HttpWebRequest would allow to tweak the performance issue. Regrettably, 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;
}

我用这个code到来回发送数据到HTTP服务器。

I use this code to send data back and forth to an HTTP server.

更新:经过广泛的研究,我得出结论:the性能问题是固有的Silverlight V3 。

Update: after extensive research, I conclude that the performance problem is inherent to Silverlight v3.

推荐答案

很可能你看到的Nagle算法的影响,请尝试:

Quite possibly you're witnessing the the effects of the Nagle Algorithm, try:

this.webRequest.UseNagleAlgorithm.ServicePoint = false;

此外,Expect100Continue握手是有关SOAP服务调用的性能:

Also, the Expect100Continue 'handshake' is relevant to soap service call performance:

this.webRequest.Expect100Continue.ServicePoint = false;

UDPATE:

UDPATE:

刚刚意识到的ServicePoint是不是在Compact Framework中可用。然而,你可以通过做证明这一点:

Just realised that the ServicePoint isn't available in Compact Framework. However you can prove the point by doing:

ServicePointManager.UseNagleAlgorithm = false

或改变在应用程序配置文件的初步认识的设置,或任何equivalnent是在Silverlight?

Or changing the relavant setting in the app config file, or whatever the equivalnent is in silverlight?