How is the CountPerTimeInterval32 performance counter type used to measure the average rate of events per minute?
I need to measure the rate an application event which is occuring开发者_如何学Go with a frequence bellow once per sec. Can this be achieved using the CountPerTimeInterval32 counter type and if so how? If not, what is the best type of performance counter to use to measure infrequently occuring events?
CountPerTimeInterval32 could be used to measure average number of items in a queue per time interval. What you want is RateOfCountsPerSecond32.
Set up:
const string CATEGORY_NAME = "AAA - My Own Perf Counter Category";
const string CATEGORY_HELPTEXT = "My own perf counter category to study effects of using different perf counter types.";
const string COUNTER_NAME = "RateOfCountsPerSecond32";
const string COUNTER_HELPTEXT = "Demonstrates usage of the RateOfCountsPerSecond32 performance counter type.";
// This should be in an installer class and run during your application set up - do not set up and them immediately use the counter.
if (!PerformanceCounterCategory.Exists(CATEGORY_NAME))
{
var counters = new CounterCreationDataCollection();
var rateOfCounts32 = new CounterCreationData();
rateOfCounts32.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
rateOfCounts32.CounterName = COUNTER_NAME;
rateOfCounts32.CounterHelp = COUNTER_HELPTEXT;
counters.Add(rateOfCounts32);
// You could set up a multi instance category. I'm using single instance for brevity.
PerformanceCounterCategory.Create(CATEGORY_NAME, CATEGORY_HELPTEXT, PerformanceCounterCategoryType.SingleInstance, counters);
}
Usage:
public void OnSomeEvent(object sender, EventArgs e)
{
using (var counter = new PerformanceCounter(CATEGORY_NAME, COUNTER_NAME, false))
{
counter.Increment();
}
// do your stuff here...
}
精彩评论