VoimiX
VoimiX

Reputation: 1200

Simple WCF "Hello world" service occupies only one CPU core under load

I have a simple "Hello world" service based on basicHttpBinding. The service is hosted on a quad-core CPU.

When I run load tests only one core is occupied (95%), and the others three approximately 4-8%.

Why are the other cores not used for proccessing?

Setting ConcurrencyMode = ConcurrencyMode.Multiple didn't help.

Enter image description here

Upvotes: 2

Views: 879

Answers (2)

Dmitry Harnitski
Dmitry Harnitski

Reputation: 6018

Configure a ServiceBehavior for your service.

WCF uses ConcurrencyMode=ConcurrencyMode.Single by default. That mode runs all requests to your service in one thread.

With ConcurrencyMode.Single, WCF does not call again into the object so long as the method is running. After the operation returns the object can be called again.

One CPU core is used to run that thread.

Add the attribute below for your service to use all the CPUs:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]

Be careful with service state when you enable that mode. You may need to implement your own locking if you change state.

Check ConcurrencyMode Enumeration for more details.

Also make sure that your client makes four calls simultaneously (implement multi-threading in client). Without that you still will have sequential one-thread calls processing even if your server supports multi-threading.

Update after checking the code:

Your WCF method doesn't do any work that can load the CPU. Please replace your methods with some heavy CPU-using function (calculate hashes or factorial) and re-check.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HelloService : IHelloService
{
   public string HelloWorld()
   {
      return "Hello world";
   }
}

Upvotes: 3

tjollans
tjollans

Reputation: 716

The API docs for BasicHttpBinding say this:

Any instance members are not guaranteed to be thread safe.

This implies that a single BasicHttpBinding instance should not be called from multiple threads in parallel, and therefore cannot be spread across multiple CPUs/CPU cores.

Upvotes: 1

Related Questions