2

I have WCF service. I have done some testing with basichttp binding and tcp binding.

In a console client i created hundreds of threads and hit the service with tcp and http binding. Turned out TCP is twice faster than http.

Then i made a web client that runs on IIS and hits the service. Turned out that http is faster then TCP.

How does this happen? isnt TCP supposed to be faster than basichttp? COde is like the one below. similar.

stopwatch start here

Thread[] ts = new Thread[100];  
for(int i= 0; i< ts.lenght;i++){  
  ts[i] = new Thread(foo);  // replace with bar for the second test.
  ts[i].start();  
}

for(int i 0;i< ts.lenght;i++){  
  ts[i].join();  
}  

stopwatch stop here.

public static void foo(){  
MyServiceClient myclient = new MyServiceClient("netTcpBinding");  
myclient.GetResult(1);  
} 

public static void bar(){
MyServiceClient myclient = new MyServiceClient("basicHttpBinding");
myclient.GetResult(1);
}
DarthVader
  • 52,984
  • 76
  • 209
  • 300

2 Answers2

3

Did you know that WCF has throttling turned on by default. Before you run these tests, you should turn it off by using this bit of config in your web.config's serviceModel section:

<behaviors>
  <serviceBehaviors>
    <behavior> <!-- can omit name in .NET 4 -->
      <serviceThrottling 
        maxConcurrentCalls="10000" 
        maxConcurrentSessions="100000" 
        maxConcurrentInstances="100000" />
    </behavior>
  </serviceBehaviors>
</behaviors>

The defaults vary by version but are around 16, 10, 100 or something along those lines in .NET 4. Those lower defaults are applied even if you have no throttling entry in your web.config / app.config.

Good luck.

Michael Kennedy
  • 3,202
  • 2
  • 25
  • 34
  • 1
    In .NET 4.0 they are dependent on the `ProcessorCount` of your system so that the throttling limits are automatically scaled according to the capabilities of the machine the service is running on. For more information, see [WCF 4: Higher Default Throttling Settings for WCF Services](http://blogs.msdn.com/b/wenlong/archive/2009/07/26/wcf-4-higher-default-throttling-settings-for-wcf-services.aspx). – Allon Guralnek Nov 12 '12 at 15:09
0

i think the problem is that of the hosting process. .NET defaults to a certain number of worker threads in the thread pool which can be overridden by the process prior to creation of thread tasks. considering your second test is in IIS, which prior to WCF4 in conjunction with AppFabric defaults to HTTP, IIS due to the nature of the beast could quite possibly be changing the default number of worker threads.

just because you ask for say 100 threads on any given machine, does not mean all will be created at once but rather queued not to mention limitations of thread context switching.

as a general rule you should not create more worker threads than the number of cores on the machine.

  • This is true. You can do things like ThreadPool.SetMinThreads(), etc. But be aware that WCF imposes throttling limits on top of this restriction. – Michael Kennedy Nov 13 '12 at 19:30