How to access base counter types?

Nov 3, 2009 at 6:54 AM

I'm having the hardest time trying to figure out how to access the base counters created by this library.

For example, I have a SampleFraction counter. The installer created a SampleFraction counter its corresponding Base.  How do I access this base through this library?

After looking at the source code, I see no way to do that.


Nov 8, 2009 at 3:37 PM

It's great to see people actually use this. I have hide the base counters so you don't need to worry when to add them when you need to use a more complex counter type. I have never need a fraction counter, but if you send me an example of what you are trying to achieve (i don't need yor actual code, but you can modify the example client i have) to see what you are trying to achieve. Why do you need the Base counter information?

Again, it will be simpler for me i have some code about what you are trying to achieve in order to see how can i help you, or modify this helper code to help you better. Please contact me with the source code of your example i will gladly take a look at it.


Nov 8, 2009 at 6:43 PM

I have been playing a bit about what you said. I think I now understand what you are trying to achieve. I have made some modifications, please if you can test my code and send me some feedback I might consider adding it to the next release. The changes are at revision 31569.

Nov 8, 2009 at 7:34 PM

Hi Javier,

I think you might be misunderstanding what Base counters are used for.

At least from my understanding, Base counters are not like Base types. In fact, Base counters are used apart of the calculation to determine the final value.

For example, a fraction/percent calculation is calculated by:

( X / B ) x 100

Where X (numerator) is the number of items and B (denominator) is the total number of items. B in terms of performance counter is called a Base Counter.


Now, let's say, I use your library, and call CounterHelper<T>.Increment(); The problem with this call is that you are increasing both X and B at the same time, which, by definition will always yield a X/X = 1 calculation or 100%. There is no way, currently, to access B to generate 50%.

After examining your code, CounterHelper<T>.IncrementBy(Metric.A, 0); actually calls Counter.IncrementBy(value), and Base.Increment().  So, by passing in 0 in IncrementBy(), I can increment the Base counter without affecting the primary counter, which will allow me to generate a 50% reading.

    public long IncrementBy(T counterName, long value)
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().Name);

            PerformanceCounterContainer counter = null;
            if ((counter = this.GetContainer(counterName)) == null)
                return FAILURE;
                long rtnValue = counter.PerformanceCounterInstance.IncrementBy(value);
                if ((counter.PerformanceCounterBaseInstance != null) && (counter.IsBaseAutoIncreased))

                return rtnValue;
Nov 9, 2009 at 11:34 AM

Well, you are right about one thing, when i was writing this helper i wasn't paying attention to counters that needs another one for support. When I became stuck with one or two for my own use, I modified the helper to achieve my needs (that were pretty simple actually). That's why I made a modification to my code when you asked me how to treat the SimpleFraction Counter, which one I have never used before. 

Seeing that base counters are more than just an inner support for the actual relevant counter I decided to add the same operations for the base counter as for its relevant (in case there is one). Further than that, I have implemented 2 more methods that let you work straight with both counters (the relevant one and the base associated with it).

This helper is so far, a simple approach to work with performance counter without knowning all the complex theory behind them, I won't encourage any advance or complex use of performance counters through this library, because when you start learning about it you will notice how easy was for me to do this library and probably will notice all the limitations I have imposed to actually lower the complexity of working with performance counters.


The code you pasted, let you autoincrement the base counter in case you don't want to deal with it, like on average counters. But if you prefer handling both the relevant and the base counter, you can set the autoincreased property on the attribute defining the complex counter. For example:


        	"Example Average Counter", 
        	System.Diagnostics.PerformanceCounterType.SimpleFraction, true)]


Doing so, you will need to mannually increment the base one using one of the methods I have added, or by getting the base counter associated with the complex one:


using (CounterHelper<SingleInstance_PerformanceCounters>  counterHelper = 
        counterHelper.IncrementBy(PerformanceCounters.SimpleFractionExample, value);
        counterHelper.IncrementBaseBy(PerformanceCounters.SimpleFractionExample, value2);


Hope this modification fits with your needs, in case it doesn't let me know i will do my best to help you. :)