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

Tuesday, May 24, 2011

Can elevators teach us how to write good code?

Most of you might have taken the lift from West Lake Station to Onvia office and I wanted co-relate the same to performance

We usually find us waiting for the lift to come down when going up and coming up when going down !! HOW STRANGE !!
Would not it be so simple to always make the lift 1 to always come down after delivering a passenger. Is that all that needs ?? Atleast to begin with the lift can always be made to go down for passenger entering into Seattle.

This applies for lift that takes up from Platform to Mezzaine [From bus level to intermediate] and another lift from Mezzaine to Platform. But there is one only one lift that takes passenger from Surface to Mezzaine and this is shared by both sides. How do we optimize it? Well in the mornings you always make sure the lift returns to Mezzaine because that’s where most of the people would use it and during the evenings make sure it always default to surface. Aren’t we helping the MOST passengers save time

But that’s like me thinking like a developer. Everything I develop is cool. From a test perspective, I would like to validate it and that’s were counters would come in. If I could track the no of times user presses the buttons to the actual time the lift were used, it would be easy for me to validate the claims. No wonder as testers we need to be so sure of the requirement and ask for appropriate counters

Finally is this like universal law and applied everywhere [Make lifts go to ground floor during morning and make lifts go to Top floors in evening] Not quite though. Take the Medical building in which Onvia resides in 5th floor. Making the lift always goto a desired level is a waste of energy because there are enough lifts. We hardly save any time so let’s try optimizing some-thing else..
[When there is enough memory or threads or resources for me don’t spend it in optimizing time rather try saving ENERGY - electricity]

Hope we get some correlation between Lifts and Performance in our applications.

Golden Rule - There is no golden rule to solve all problems. Be innovative and think through and capture as many counters as possible INITIALLY to keep building and improvising systems..