-1

I am trying to write simple client-server application that can use 100% of CPU using WCF.

Program is sending 10000 times request and is receiving string like in example below. I am sending f9b6af85-8826-49c8-9524-39511f1a241a and receiving "Hello f9b6af85-8826-49c8-9524-39511f1a241a" for 10000 times and I measure time. I tried to increase number of clients from 2 to 4, but CPU is used on 30%. Service is very simple and it is self hosted in console application. Client is web form application. I need advice: what should I do to get 100% CPU usage? Should i send more request, increase number of clients, send bigger message or do something else that I am missing?

Service:

public class HelloService : IHelloService
{
    public string Hello(string name)
    {
        return "Hello " + name;
    }
}

Host:

class Program
{
    static void Main()
    {
        using (ServiceHost host = new ServiceHost(typeof(HelloWcf.HelloService)))
        {
            host.Open();
            Console.WriteLine("Host started @ " + DateTime.Now.ToString());
            Console.ReadLine();
        }
    }
}

Client:

private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            label1.Text = Guid.NewGuid().ToString();
            var start = DateTime.Now.TimeOfDay;
            HelloServiceReference.HelloServiceClient client;

            for (int i = 0; i < 10000; i++)
            {
                label2.Text = string.Empty;

                client = new HelloServiceReference.HelloServiceClient("NetTcpBinding_IHelloService");

                label2.Text = client.Hello(label1.Text);
                client.Close();
            }

            var end = DateTime.Now.TimeOfDay;

            var totalTime = end - start;

            label3.Text = string.Format("Total time of execution {0}", totalTime.ToString(@"mm\:ss\.ffff"));

            button1.Enabled = true;
        }
kendzi
  • 311
  • 2
  • 6
  • 22
  • 1
    Most likely your CPU isn't pegged because I/O is the bottleneck. To make I/O *not* be the bottleneck, you'd have to do something more CPU-intensive when handling the requests. – adv12 Sep 29 '15 at 19:11
  • do more iterations? run multiple instances at once? – Jeremy Sep 29 '15 at 19:11
  • 4
    "what should I do to get 100% CPU usage" - why? A developer's goal is to *reduce* CPU usage. – Dai Sep 29 '15 at 19:13
  • This application is for validation purpose. I tried with more iterations 100000 but there was little increase with CPU usage. @adv12 what can I do for exp? Do some mathematical operations? I also turn off TCP offloading. – kendzi Sep 29 '15 at 19:15
  • If its for validation i think then 30% is probably all you can get. If you insist on 100% then its more like cpu benchmark tool. – M.kazem Akhgary Sep 29 '15 at 19:23
  • Use [Prime 95](http://www.mersenne.org/download/) – Tim Freese Sep 29 '15 at 19:30
  • @TimFreese Unfortunately I need to use WCF app. – kendzi Sep 29 '15 at 19:33

2 Answers2

0

Try to queue some threads, something like this:

for (int cont = 0; cont < 100; i++)
{
    ThreadPool.QueueUserWorkItem(new WaitCallback((a) =>
    {
        while (true) 
        { 
        }
    }));
}

Put it on your requests, it will raise a lot your memory usage.

Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43
  • You mean inside my for loop on client, or in service before I send response? – kendzi Sep 29 '15 at 19:30
  • If you want to stress the memory of your server, you can put in your wcf method before send the response – Ricardo Pontual Sep 29 '15 at 19:35
  • Ok, i have now 100% of CPU usage when I send request, but what this is doing exaclty when I send reques?? public string Hello(string name) { for (int cont = 0; cont < 100; count++) { ThreadPool.QueueUserWorkItem(new WaitCallback((a) => { while (true) { } })); } return "Hello " + name; } – kendzi Sep 29 '15 at 19:42
  • and there is also a problem when response is send CPU stays on 100%. – kendzi Sep 29 '15 at 19:47
  • ThreadPool.QueueUserWorkItem puts a task in a queue to be executed and wait for it finishes, but with the while(true) line, it will never finish, so o raise the cpu usage as you wanted. – Ricardo Pontual Sep 30 '15 at 03:18
  • cpu at 100% will crash your system, only use to reach your goal and then stop. – Ricardo Pontual Sep 30 '15 at 03:20
  • If my answer helped you, don't forget to mark as useful, or the best answer if resolves your issue – Ricardo Pontual Oct 01 '15 at 17:02
0

Take a look at the example program that I used to answer WCF performance, latency and scalability. The full command line code is included.

The threading code uses an older pattern because its from 2011. However it can still fully consume any CPU.

Community
  • 1
  • 1
ErnieL
  • 5,773
  • 1
  • 23
  • 27