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

C# CountPerTimeInterval32性能计数器类型如何用于测量每分钟事件的平均速率?,c#,.net,performancecounter,C#,.net,Performancecounter,我需要测量一个应用程序事件的速率,该事件的发生频率为每秒一次。使用CountPerTimeInterval32计数器类型可以实现这一点吗?如果可以,如何实现?如果不是,用于测量不经常发生事件的最佳性能计数器类型是什么?CountPerTimeInterval32可用于测量每个时间间隔队列中的平均项目数。您需要的是计算秒的速率32 设立: const string CATEGORY_NAME = "AAA - My Own Perf Counter Category"; const string

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

CountPerTimeInterval32可用于测量每个时间间隔队列中的平均项目数。您需要的是计算秒的速率32

设立:

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

CountPerTimeInterval32可用于测量每个时间间隔队列中的平均项目数。您需要的是计算秒的速率32

设立:

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