0

My goal is to develop a mini-benchmark tool, so I can test the performance of a Web Api.

I want to create the performance counters programmatically using a simple console application. As part of my research, I created the main method below. This is just a simple test. So basically, I need HttpClient in order to forward an x-number of requests and then to view some metrics (i.e % Processor Time ) for this operation.

Is my logic correct?

Hide Copy Code

  static void Main(string[] args)
  {
          var client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);

        Console.WriteLine(theCPUCounter.NextValue());
        System.Threading.Thread.Sleep(1000);

        for (int i = 0; i <= 5; i++)
        {
            HttpResponseMessage response = client.GetAsync("http://www.google.co.uk/").Result;
        }
        Console.WriteLine(theCPUCounter.NextValue());
    }

I am aware that there are performance counters that make more sense, but for the time being, I just want to see if I understand it correctly.

If the above is correct then my plan is :

1)Create various throughput scenarios with concurrent requests using HttpClient and TPL(i.e send 1000 concurrent requests, but create different scenarios for every 100 in order to utilize various resources in the Web Api). As soon as I have those scenarios executed, then

2)I will be releasing the performance counters for measuring CPU and Memory.

That's the plan.

Can you please advise?

Thanks!!

Veronica_Zotali
  • 253
  • 1
  • 3
  • 13

2 Answers2

1

The fundamental question here is "How to use PerformancCounters" and you got that part correctly. You can simplify your code:

public static void Main()
{
    var cpuSample = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total"); 
    while(true)
    {
       Thread.Sleep(1000);
       Console.WriteLine(cpuSample.NextValue() + " %");
    }
 }

Your post adds to the mix a HttpClient, but that is completely orthogonal. You use PerformanceCounters by periodically reading the sample, and the value read is the counter value in the interval from the previous sample.

For the particular counter in your example (total CPU %) it really doesn't matter what you do in your application between the samples (ie. the 10 HttpClient requests). The _Total CPU % is a global value, it covers all apps and services on that host.

If you're interested in improving your HTTP requests, then be aware that you should focus somewhere else. Read Adjusting HttpWebRequest Connection Timeout in C# and perhaps High performance TCP server in C#

Community
  • 1
  • 1
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Thanks for the reply. However, I need to measure the performance of an existing Web Api(like a super mini benchmarking tool), so I will need to use performance counters (programmatically). I understand that what I did refers to the total CPU%, but what if I do it only for the current process? I will need to get metrics for Processor and Memory. Therefore, I need the HttpClient, as I need to hammer the server with an x number of requests and then use counters to retrieve metrics. I am not interested in improving the Http requests. I want to identify potential bottlenecks for athroughput number – Veronica_Zotali May 06 '15 at 12:49
1

Try metrics.net library:

https://github.com/Recognos/Metrics.NET

You may measure CPU usage(and other metrics) like this:

using Metrics;

Metric.Config
    .WithHttpEndpoint("http://localhost:1234/")
    .WithAllCounters();
Dmitrii Zyrianov
  • 2,208
  • 1
  • 21
  • 27