Click here to Skip to main content
15,028,402 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

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?
C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Spartan
{
    class Program
    {
        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);

            theCPUCounter.NextValue();

            var tasks = new int[1000];
            var counter = 0;
            for (int i = 0; i< 1000; i++)
            {

                Parallel.ForEach(tasks, x =>
                {

                     client.GetAsync("http://www.google.co.uk/").GetAwaiter();
                });

                // for 8 cores processor
                Console.WriteLine((theCPUCounter.NextValue() / 8));
                System.Threading.Thread.Sleep(2950);
            }

        }
    }
}




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!!
Posted
Updated 8-May-15 22:38pm
v11
Comments
gggustafson 8-May-15 16:09pm
   
You need to specify that you are interested in .Net 4.0 or 4.5. Your example cannot be compiled in .Net 3.5
Veronica S. Zotali 9-May-15 4:32am
   
Its in 4.5.

I am aware that there are tools, the reason for creating my own tool is :

1) Knowledge regarding CPU resources and Memory management

2) Simplify the way I could benchmark a complex Web Api.
gggustafson 9-May-15 11:25am
   
I'm sorry, I cannot help. Net 4.5 is not available to me. Try Googling "c# webclient performance measurement" and check out the StackOverflow response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900