Docstoc

counters

Document Sample
counters Powered By Docstoc
					/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////
//
public class CounterInstaller
{
   public void Install()
   {
       Dictionary<string, List<Counter>> counters = new
Dictionary<string, List<Counter>>();

       //check the current running dll's for any class which is a counter
and group them by category
       Assembly assembly = Assembly.GetExecutingAssembly();

                     assembly.GetTypes().ForEach(type =>
                        {
                            if (type is Counter)
                            {

                                  if (counters[type.CategoryName] == null)
                                      counters[type.CategoryName] = new
List<Counter> { type };
                                  else
                                      counters[type.CategoryName].Add(type);
                              }
                        });

            //now for each group of counters install everything

    }
}



/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////
//
public abstract class Counter
{
    public Counter(string name, string category, string description,
PerformanceCounterType counterType)
    {
         counter = new PerformanceCounter { ReadOnly     = false,
                                            MachineName = ".",
                                            CounterName = name,
                                            CategoryName = category };

              counterType = counterType;
        }


        protected PerformanceCounter        counter;
    protected PerformanceCounterType counterType;
}


/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////
//
[DllImport("Kernel32.dll")]
public static extern void QueryPerformanceCounter(ref long ticks);


public abstract class TimeCounter : Counter
{
    public TimeCounter(string name, string category, string description,
PerformanceCounterType counterType)
         : base(name, category, description, counterType)
    {
    }

    public void Trace(Action method)
    {
         // measure starting time
       QueryPerformanceCounter(ref startTime);

          //run the code block
          method();

          // measure ending time
        QueryPerformanceCounter(ref endTime);

        // do some processing
        counter.RawValue = endTime - startTime;
    }

    protected long startTime ,
                   finishTime;
}

# operation time
public class ProfileCounter : TimeCounter
{
    public TimeCounter(string name, string category, string description)
         : base(name, category, description,
PerformanceCounterType.NumberOfItems32)
    {
    }
}

# operation time average
public class ProfileAverageCounter : TimeCounter
{
    public OperationAverageCounter(string name, string category, string
description)
         : base(name, category, description,
PerformanceCounterType.PerformanceCounterType.AverageCount64)
    {
    }
}

/////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////
//
# operations
public abstract class CountCounter : Counter
{
    public CountCounter(string name, string category, string description,
PerformanceCounterType counterType)
         : base(name, category, description, counterType)
    {
    }

    public int Value
    {
        set { counter.RawValue = value; }
    }

    public void Increment()
    {
        Increment(1);
    }

    public void Decrement()
    {
        Decrement(1);
    }

    public void Increment(int value)
    {
        counter.RawValue += value;
    }

    public void Decrement(int value)
    {
        counter.RawValue -= value;
    }
}

#operation count
public class OperationCounter : CountCounter
{
    public OperationCounter(string name, string category, string
description)
         : base(name, category, description,
PerformanceCounterType.NumberOfItems32)
    {
    }
}

# operations average
public class OperationAverageCounter : CountCounter
{
    public OperationAverageCounter(string name, string category, string
description)
         : base(name, category, description,
PerformanceCounterType.PerformanceCounterType.AverageCount64)
    {
    }
}

# operations per second
public class OperationSecondCounter : CountCounter
{
    public OperationSecondCounter(string name, string category, string
description)
         : base(name, category, description,
PerformanceCounterType.RateOfCountsPerSecond32)
    {
    }
}

				
DOCUMENT INFO
Shared By:
Tags:
Stats:
views:26
posted:5/27/2011
language:Swedish
pages:4