CountPerTimeInterval32性能计数器类型如何用于衡量每分钟的平均事件率?

时间:2010-08-11 17:04:36

标签: c# .net performancecounter

我需要测量一个应用程序事件的速率,该事件发生的频率低于每秒​​一次。可以使用CountPerTimeInterval32计数器类型实现这一点,如果是这样,怎么办?如果没有,用于测量不常发生事件的最佳性能计数器类型是什么?

1 个答案:

答案 0 :(得分:1)

CountPerTimeInterval32可用于测量每个时间间隔的队列中的平均项目数。你想要的是RateOfCountsPerSecond32。

设置:

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);
}

用法:

public void OnSomeEvent(object sender, EventArgs e)
{ 
    using (var counter = new PerformanceCounter(CATEGORY_NAME, COUNTER_NAME, false))
    {
        counter.Increment();
    }

    // do your stuff here...
}