Monday, December 19, 2011

Sample Application to Capture Counters

As testers we would like to capture a lot of counters during our testing. This becomes crucial especially critical if we are performing any sort of Performance oriented testing. There are quite a few performance loading tools but nothing like custom built code. I will share this in my next post, but for this post, let us assume we have a free ware to load our system.

Now we would like to monitor the performance of a few machines, [I had to monitor a 3 Service Machines, 2 Web App Server, 2 DB Servers]. Also each machine we would like to monitor difference performance counters. If we had tools like VSTS Perf Monitoring, we can do the same, though I have never been able to connect and gather this from all machines. But then VSTS Perf version is not a free version.
Alas Microsoft always gives us the code to get the required details. The ultimate perfom counters.

The steps are definetely not rocket science
Step 1
Add all machines that you would want to include in your perf test. I have added all these in my appconfig file. Then I launch a Simple Windows Forms and populate all these machines as Check Boxes

Step 2 - Gather the required Category, Instance and Counter Names
a. Add all category names to a List and send this List to next step
categories = PerformanceCounterCategory.GetCategories(PMmachineName);
foreach (PerformanceCounterCategory cat in categories)
{ categoryNames.Add(cat.CategoryName);
b. For Each category Name get the instance name and save it again as List of strings
PerformanceCounterCategory category = new PerformanceCounterCategory()
{
MachineName = PMmachineName,
CategoryName = PMcategoryName
};
List instanceNames = category.GetInstanceNames().ToList();
c. For the supplied CategoryName and Instance Name display the counters and select which ever is required for performance testing
PerformanceCounterCategory pcc = new PerformanceCounterCategory()
{
MachineName = PMmachineName,
CategoryName = PMcategoryName,
};
PerformanceCounter[] counters;
counters = pcc.GetCounters(PMinstanceName);

List counterNames = new List();
foreach (PerformanceCounter counter in counters)
{ counterNames.Add(counter.CounterName); }

Step 3
Schedule a refresh rate to make sure how often you would want to capture the results

Step 4
For each of the selected counters spawn a new thread with the simple code mentioned below [A new thread is required because if all machines are in same thread then the .NextValue() method would take ages for each machine switching. But if 1 unique thread is maintained for each counter value, then results come in lightining speed]
PerformanceCounter performanceCounter = new PerformanceCounter();
performanceCounter.CategoryName = categoryName;
performanceCounter.CounterName = counterName;
performanceCounter.InstanceName = instanceName;
performanceCounter.MachineName = machineName;
perfCounterVal = performanceCounter.NextValue();

Step 5
Capture this value in parent thread and wait for the refresh rate and once refresh rate is reached store the perfCounterVal to another list and store the results in .csv file

Step 6
Add any logic you would want depending on ther perfCounterValue. We could add spwan another small thread to send out an email with the value of the last x mins from all machines if the Threshold goes beyond a point.
We could add a logic to control our Perftesting depending on threshold values.

Once we have the counter values its our imagination to do any activity that we want.

It would have been easier to share my code, but I guess if anybody really wants it you could drop me an email and I will be more than glad to help you. Probably with this approach you find out a better method

No comments: