简体   繁体   中英

Performance comparison TcpClient vs HttpWebRequest

I'm posting an XML string to a port on an AIX box. I have two ways in which I'm connecting to this box (TcpClient & HttpWebRequest). I have timers in place to give me an idea how long it is taking the AIX box to process my request and respond.

It appears that the TcpClient is faster than the HttpWebRequest by up to 100 milliseconds. I suspect that my timer locations may be incorrect. Either way, I don't think the timer location would amount for such a big difference in response time.

Another thought I had was the using statements. Perhaps they are keeping the connection open longer than the TcpClient.

Is the TcpClient approach known to be faster?

// TcpClient
TcpClient client = new TcpClient(host, port);
DateTime x = DateTime.Now;
NetworkStream stream = client.GetStream();
NetworkStream stream = client.GetStream();
stream.Write(request, 0, request.Length);
stream.Flush();
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
            response.Append(encoder.GetString(buffer, 0, count));
DateTime y = DateTime.Now;
totalMS = y.Subtract(x).TotalMilliseconds;


// HttpWebRequest 
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URI);
using (Stream webStream = webRequest.GetRequestStream())
{
    webStream.Write(postdata, 0, postdata.Length);
    webStream.Close();
    DateTime x = DateTime.Now;
    using (WebResponse webresponse = webRequest.GetResponse())
    {
        webresponse.Close();
        DateTime y = DateTime.Now;
        using (Stream rs = webresponse.GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(rs, Encoding.Default))
            {
                // Read response to end
            }
        }
    }
}
totalMS = y.Subtract(x).TotalMilliseconds;

Well they're clearly doing different things - you're not sending any data down the TcpClient , so the other end must already know what to do.

In the WebRequest version you're even posting some data... why don't you need that data in the TcpClient version?

Basically it looks like you're not comparing apples with apples. Given that you're talking different protocols to a server, it could very well be that your particular server is quicker using TcpClient directly over a simplified protocol. That's not the same as comparing HttpWebRequest and TcpClient in general . You can't really make such a general comparison, as HTTP is layered over TCP to start with.

The TcpClient is going to be faster for most things, because the HttpWebRequest has to do a bunch of other things... Such as constructing the headers for the HTTP request, and managing streaming connections through things like chunked requests. Where a TcpClient is just the raw connection with out all the HTTP standards built in.

只需使用System.Diagnostics.Stopwatch进行时间比较,就可以更加精确。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM