Wednesday, November 3, 2010

How do you monitor your service?

Many times we may not even know how many hits we get on the service or which methods are getting the highest hits. We may have some information on logs that tells us what kind of request came in and what happened to the request but how much time did the request take to process etc. may not be available on log. For an enterprise application it is very critical that we have a monitoring in place. 

We can create our own performance categories and create counters that would tell us what’s going on our service and how much time is it taking etc. We will look at how we can adopt performance counters on our wcf service. 

if you haven’t used perf monitor, take a look here

Lets do a sample wcf service and see how we can include perf counters on it

Create a console app which will create counters that we need to use on our service.

Make sure you have included using System.Diagnostics;

//define perf category
            string categoryName = "My Demo Service";

            //counter that we going to use
            string CounterName = "Get Data calls per sec";

            // if exists delete the category Note that you will need to have admin rights to create or delete categories/counters
            if (PerformanceCounterCategory.Exists(categoryName))

            //create counters base that we will be using on this category
            var counterDataCollection = new CounterCreationDataCollection();

            var callspersecCounter = new CounterCreationData()
                CounterName = CounterName,
                CounterHelp = "Get Data calls per sec",
                CounterType = PerformanceCounterType.RateOfCountsPerSecond32
            // you can refer help for detailed explanation on different types


            //create the Category
            PerformanceCounterCategory.Create(categoryName, "My Demo Service", PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

Once you run above code, open perfmon from Administrative tools

When you click on App, below pop up will be opened and now you can identify the new category we just added

Select the counter and click on “OK”
The perf mon will show something like below

Since there is no activity the graph looks blank.

Lets use this counter on our service

Create a service method as below

        public string GetData(int value)
            var counter = new System.Diagnostics.PerformanceCounter("My Demo Service", "Get Data calls per sec", false);
           return "data " + value.ToString();

This method would just increment the counter and return the same text which is passed. Now lets refer this method and call this for couple of time.

            for (int i = 0; i < 100; i++)
                ServiceReference2.Service1Client obj = new ServiceReference2.Service1Client();
                string retString = obj.GetData(i);

                if (i % 3 == 0)

I am going to call the same method many times with some delays so that we can identify the patterns on the monitor.

The monitor would show something like this now

This shows how many calls we got on each second. This also helps us to identify if we need to optimize the service or where we need to concentrate on performance related issues. 

No comments:

Post a Comment