Viewing PIAB performance counters in web page

Topics: Policy Injection Application Block
Sep 10, 2008 at 1:20 AM
I have created, via installutil, performance counters for a category.

I have also created an interceptable class with a method that has a tag based policy.  The policy has the performance counter handler associated with it.

It all seems to work great!  I can run/test the assembly method and see the counters incremented in perfmon no problem.

I got the bright idea to display the counter data on a ajax enabled web page, where the page posts back and refreshed the counter data every 10 seconds or so.

That works! however, I can't seem to intrepret the "# calls / second" correctly - it shows the NextValue as 0 and RawValue as the "# of calls.

I tried looking at the CounterSample representation of the data but can't seem to figure out how to display the data like perfmon does.

I think this has more to do with my lack of knowledge of performance counters more than anything else; however, any help would be appreciated.

Here is the timer callback code...

    protected void Timer1_Tick(object sender, EventArgs e)
        System.Diagnostics.PerformanceCounter numberOfCalls = new System.Diagnostics.PerformanceCounter("SomeCategory", "# of calls", "total", true);
        System.Diagnostics.PerformanceCounter numberOfCallsPerSecond = new System.Diagnostics.PerformanceCounter("SomeCategory", "# calls / second", "total", true);
        System.Diagnostics.PerformanceCounter numberOfExceptions = new System.Diagnostics.PerformanceCounter("SomeCategory", "# of exceptions", "total", true);
        System.Diagnostics.PerformanceCounter numberOfExceptionsPerSecond = new System.Diagnostics.PerformanceCounter("SomeCategory", "# exceptions / second", "total", true);
        System.Diagnostics.PerformanceCounter averageSecondsPerCall = new System.Diagnostics.PerformanceCounter("SomeCategory", "average seconds / call", "total", true);

        TotalNumberOfCallsLabel.Text = string.Format("RawValue={0}", numberOfCalls.RawValue());
        AverageNumberOfCallsLabel.Text = string.Format("NextValue={0}, RawValue={1}", numberOfCallsPerSecond.NextValue(), numberOfCallsPerSecond.RawValue);
        AverageDurationOfCallsLabel.Text = string.Format("NextValue={0}, {1} milliseconds", averageSecondsPerCall.NextValue(), new TimeSpan(averageSecondsPerCall.RawValue).TotalSeconds);
        TotalNumberOfExceptionsLabel.Text = string.Format("RawValue={0}", numberOfExceptions.RawValue);
        AverageNumberOfExceptionsLabel.Text = string.Format("NextValue={0}, RawValue={1}", numberOfExceptionsPerSecond.NextValue(), numberOfExceptionsPerSecond.RawValue);

        System.Diagnostics.CounterSample cs = numberOfCallsPerSecond.NextSample();
        PercentageOfExceptionsToSuccessfulCallsLabel.Text = string.Format("cs.BaseValue={0}, cs.CounterFrequency={1}, cs.CounterTimeStamp={2}, cs.RawValue={3}, cs.SystemFrequency={4}, cs.TimeStamp={5}, cs.TimeStamp100nSec={6}, System.Diagnostics.CounterSample.Calculate(cs)={7}", cs.BaseValue, cs.CounterFrequency, cs.CounterTimeStamp, cs.RawValue, cs.SystemFrequency, cs.TimeStamp, cs.TimeStamp100nSec, System.Diagnostics.CounterSample.Calculate(cs));

Thanks in advance

Sep 10, 2008 at 3:46 PM

You should be using NextValue(). The problem is that you're creating the performance counters every time; check out the documentation for PerformanceCounter.NextValue() and you will find that "If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.". Try implementing a version that reuses the same performance counter instances accross calls.

Hope this helps,