Skip to content

TcpClient is slow when connecting via constructor with hostname and port. #26952

Closed

Description

@djlaim
using(var client = new TcpClient("hostname", 1234))
{
   //exchange data
}

The time taken to exchange data with a remote host with the above code is considerably slower
than simply doing:

using(var client = new TcpClient())
{
    client.Connect("host",12345);

    //exchange data
}

There appears to be code within the TcpClient constructor that is then repeated at the start of the call to connect (validating port numbers, logging), but as I can't test it currently I'm not entirely sure.

Any thoughts :) ?

Activity

  1. samsosa commented on Jul 25, 2018

    @samsosa

    I can't see any noteworthy difference.

    My results:

    Method1: 2913,248 ms total!  << Faster!
    Method2: 2923,164 ms total!
    Diff: 9,916 ms!
    
    Method1: 2984,969 ms total!
    Method2: 2926,874 ms total!  << Faster!
    Diff: 58,095 ms!
    
    Method1: 2885,578 ms total!  << Faster!
    Method2: 2934,020 ms total!
    Diff: 48,443 ms!
    
    Method1: 2942,206 ms total!
    Method2: 2901,467 ms total!  << Faster!
    Diff: 40,738 ms!
    
    Method1: 2974,631 ms total!  << Faster!
    Method2: 3010,156 ms total!
    Diff: 35,525 ms!
    

    Bench-Code:

    using System;
    using System.Net.Sockets;
    
    namespace TimingTestTcpClient
    {
        class Program
        {
            private static readonly byte[] _buffer = new byte[8 * 1024];
            private static readonly byte[] _benchTestWrite 
                = System.Text.Encoding.ASCII.GetBytes("HEAD / HTTP/1.0\r\nHost: example.org\r\n\r\n");
    
            static void Main(string[] args)
            {
                for (int i = 0; i < 5; i++)
                {
                    DoBench();
                    System.Threading.Thread.Sleep(5000); // <- No DoS pause!
                }
    
                Console.ReadKey();
            }
    
            static void DoBench()
            {
                TestSingleCallMethod1(); // <- Dry!
                TestSingleCallMethod2(); // <- Dry!
    
                var sw1 = System.Diagnostics.Stopwatch.StartNew();
                for (int i = 0; i < 10; i++)
                {
                    TestSingleCallMethod1();
                }
                sw1.Stop();
    
    
                var sw2 = System.Diagnostics.Stopwatch.StartNew();
                for (int i = 0; i < 10; i++)
                {
                    TestSingleCallMethod2();
                }
                sw2.Stop();
    
                Console.WriteLine("Method1: " + sw1.Elapsed.TotalMilliseconds.ToString("f3") + " ms total!");
                Console.WriteLine("Method2: " + sw2.Elapsed.TotalMilliseconds.ToString("f3") + " ms total!");
                Console.WriteLine("Diff: " + (sw1.Elapsed - sw2.Elapsed).Duration().TotalMilliseconds.ToString("f3")+ " ms!");
                Console.WriteLine();
            }
    
            static void TestSingleCallMethod1()
            {
                using (var client = new TcpClient("example.org", 80))
                {
                    client.GetStream().Write(_benchTestWrite);
                    client.GetStream().Read(_buffer);
                }
            }
    
            static void TestSingleCallMethod2()
            {
                using (var client = new TcpClient())
                {
                    client.Connect("example.org", 80);
    
                    client.GetStream().Write(_benchTestWrite);
                    client.GetStream().Read(_buffer);
                }
            }
        }
    }
  2. wfurt commented on Sep 19, 2018

    @wfurt
    Member

    From the example above is not clear if you connecting to same or different destination.
    Not that there is always DNS factor. Also there were cases when DNS resolves to both IPv4 and IPv6 addresses. If one or the other AF is not available you can see differences.

    What OS & Version do you use? You should collect some traces and perhaps packet capture for DNS (port 53) and target service.

  3. djlaim commented on Sep 19, 2018

    @djlaim
    Author

    Apologies if the above wasn't clear enough, I am connecting to the same destination every time.

    ** System Details **
    OS: Windows 10,
    Version: 10.0.17134 Build 17134

    It's been a while since i've looked at this myself, so I'll try my best to get some additional test evidence to you.

  4. wfurt commented on Sep 19, 2018

    @wfurt
    Member

    thanks. With more info we have better chance of solving the mystery.
    Also on Windows you can enable network tracing (ETW) and that may provide some useful insight.

  5. karelz commented on Nov 2, 2018

    @karelz
    Member

    Not enough information, closing. If you have additional details, please let us know and we can reopen. Thanks!

  6. transferred this issue fromdotnet/corefxon Jan 31, 2020
  7. added this to the 3.0 milestone on Jan 31, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Netbugtenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions