需要使用 CounterDelta32 PerformanceCounter 的示例

Posted

技术标签:

【中文标题】需要使用 CounterDelta32 PerformanceCounter 的示例【英文标题】:Need example of using CounterDelta32 PerformanceCounter 【发布时间】:2009-10-18 21:11:18 【问题描述】:

我试图显示自上次性能迭代以来操作发生的次数。我使用以下方法创建了一个性能计数器:

var clearStateCounterData = new CounterCreationData()

    CounterName = ClearStateName,
    CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
    CounterType = PerformanceCounterType.CounterDelta32
;

然后我在我的应用程序中调用counter.Increment(),但我从未看到性能计数器值移动。即使我每秒运行多次。

我需要什么特别的东西或者我需要增加一个特定的值来让 PerformanceCounter 显示一些东西吗?

想通了

我在下面的答案中举了一个使用这个计数器的例子。谢谢大家的帮助。

【问题讨论】:

【参考方案1】:

这是一个对我有用的例子。

class Program

    const string CategoryName = "____Test Category";
    const string CounterName = "Clear State Operations";

    static void Main(string[] args)
    
        if (PerformanceCounterCategory.Exists(CategoryName))
            PerformanceCounterCategory.Delete(CategoryName);

        var counterDataCollection = new CounterCreationDataCollection();

        var clearStateCounterData = new CounterCreationData()
        
            CounterName = CounterName,
            CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
            CounterType = PerformanceCounterType.CounterDelta32
        ;
        counterDataCollection.Add(clearStateCounterData);

        PerformanceCounterCategory.Create(CategoryName, "Test Perf Counters", PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

        var counter = new PerformanceCounter(CategoryName, CounterName, false);

        for (int i = 0; i < 5000; i++)
        
            var sw = Stopwatch.StartNew();
            Thread.Sleep(10300);
            sw.Stop();

            counter.Increment();
        

        Console.Read();
    

【讨论】:

【参考方案2】:

这还不足以创建计数器...根据文档,您需要创建一个PerformanceCounterCategory 并创建一个PerformanceCounter 的实例。查看 MSDN 中的示例:http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

【讨论】:

是的,我做到了。计数器出现在我的类别中。我似乎无法通过增加计数器来显示任何值。我猜那个 counter.Increment 不是正确的事情,也不是在这种情况下我需要调用的唯一事情,但我没有看到任何可用的这种计数器类型的示例。【参考方案3】:

创建计数器后(使用 CounterCreationData 和 PerformanceCounterCategory 中的 Create),然后创建计数器的实例(使用 PerformanceCounter),您需要初始化计数器值以在性能监视器中启动实例。

此外,请确保您在读写模式下创建计数器(通过将 false 传递给 readOnly 参数)。

您可以尝试设置 RawValue = RawValue 或 RawValue = 0 来启动它,看看它是否出现。

【讨论】:

以上是关于需要使用 CounterDelta32 PerformanceCounter 的示例的主要内容,如果未能解决你的问题,请参考以下文章

监控 ActiveMQ 性能的工具

实体框架与 NHibernate - 性能

Xpath定位元素方式

为什么页面没有完全加载?

移动端资源集锦

移动端资源集锦