You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TcpClient is slow when connecting via constructor with hostname and port. #26952
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.
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:
usingSystem;usingSystem.Net.Sockets;namespaceTimingTestTcpClient{classProgram{privatestaticreadonlybyte[]_buffer=newbyte[8*1024];privatestaticreadonlybyte[]_benchTestWrite=System.Text.Encoding.ASCII.GetBytes("HEAD / HTTP/1.0\r\nHost: example.org\r\n\r\n");staticvoidMain(string[]args){for(inti=0;i<5;i++){DoBench();System.Threading.Thread.Sleep(5000);// <- No DoS pause!}Console.ReadKey();}staticvoidDoBench(){TestSingleCallMethod1();// <- Dry!TestSingleCallMethod2();// <- Dry!varsw1=System.Diagnostics.Stopwatch.StartNew();for(inti=0;i<10;i++){TestSingleCallMethod1();}sw1.Stop();varsw2=System.Diagnostics.Stopwatch.StartNew();for(inti=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();}staticvoidTestSingleCallMethod1(){using(varclient=newTcpClient("example.org",80)){client.GetStream().Write(_benchTestWrite);client.GetStream().Read(_buffer);}}staticvoidTestSingleCallMethod2(){using(varclient=newTcpClient()){client.Connect("example.org",80);client.GetStream().Write(_benchTestWrite);client.GetStream().Read(_buffer);}}}}
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.
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.
Activity
samsosa commentedon Jul 25, 2018
I can't see any noteworthy difference.
My results:
Bench-Code:
wfurt commentedon Sep 19, 2018
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.
djlaim commentedon Sep 19, 2018
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.
wfurt commentedon Sep 19, 2018
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.
karelz commentedon Nov 2, 2018
Not enough information, closing. If you have additional details, please let us know and we can reopen. Thanks!