开发者

How is the CountPerTimeInterval32 performance counter type used to measure the average rate of events per minute?

开发者 https://www.devze.com 2023-01-10 23:17 出处:网络
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?

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...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号