类型RateOfCountsPerSecond32的计数器总是显示0 [英] Counter of type RateOfCountsPerSecond32 always shows 0

查看:187
本文介绍了类型RateOfCountsPerSecond32的计数器总是显示0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过WCF服务接口提供一些虚拟队列的消息的窗口服务。
我想公开两个性能计数器 -

I have a windows service that serves messages of some virtual queue via a WCF service interface. I wanted to expose two performance counters -


  1. 项队列数

  2. 从每秒

第一个工作正常,第二个始终显示为0 PerfMon中。.EXE,尽管Ra​​wValue看来是正确的。

The first one works fine, the second one always shows as 0 in PerfMon.exe, despite the RawValue appearing to be correct.

我创建的计数器这样 -

I'm creating the counters as such -

    internal const string PERF_COUNTERS_CATEGORY = "HRG.Test.GDSSimulator";
    internal const string PERF_COUNTER_ITEMSINQUEUE_COUNTER = "# Messages on queue";
    internal const string PERF_COUNTER_PNR_PER_SECOND_COUNTER = "# Messages read / sec";

if (!PerformanceCounterCategory.Exists(PERF_COUNTERS_CATEGORY))
{
    System.Diagnostics.Trace.WriteLine("Creating performance counter category: " + PERF_COUNTERS_CATEGORY);
    CounterCreationDataCollection counters = new CounterCreationDataCollection();

    CounterCreationData numberOfMessagesCounter = new CounterCreationData();
    numberOfMessagesCounter.CounterHelp = "This counter provides the number of messages exist in each simulated queue";
    numberOfMessagesCounter.CounterName = PERF_COUNTER_ITEMSINQUEUE_COUNTER;
    numberOfMessagesCounter.CounterType = PerformanceCounterType.NumberOfItems32;
    counters.Add(numberOfMessagesCounter);

    CounterCreationData messagesPerSecondCounter= new CounterCreationData();
    messagesPerSecondCounter.CounterHelp = "This counter provides the number of messages read from the queue per second";
    messagesPerSecondCounter.CounterName = PERF_COUNTER_PNR_PER_SECOND_COUNTER;
    messagesPerSecondCounter.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
    counters.Add(messagesPerSecondCounter);

    PerformanceCounterCategory.Create(PERF_COUNTERS_CATEGORY, "HRG Queue Simulator performance counters", PerformanceCounterCategoryType.MultiInstance,counters);
}



然后,在每一个服务电话,我增加相关计数器,每对/秒计数器目前这看起来是这样的 -

Then, on each service call, I increment the relevant counter, for the per/sec counter this currently looks like this -

messagesPerSecCounter = new PerformanceCounter();
messagesPerSecCounter.CategoryName = QueueSimulator.PERF_COUNTERS_CATEGORY;
messagesPerSecCounter.CounterName = QueueSimulator.PERF_COUNTER_PNR_PER_SECOND_COUNTER;
messagesPerSecCounter.MachineName = ".";
messagesPerSecCounter.InstanceName = this.ToString().ToLower();
messagesPerSecCounter.ReadOnly = false;

messagesPerSecCounter.Increment();

正如前面提到的 - 如果我把一个断点,来电后递增我可以看到RawValue不断增加,与调用服务(相当频繁,不止一次第二,我想)
,而本身停留在0性能计数器一致性。

As mentioned - if I put a breakpoint after the call to increment I can see the RawValue constantly increasing, in consistence with the calls to the service (fairly frequently, more than once a second, I would think) But the performance counter itself stays on 0.

性能计数器提供关于队列,这是在以同样的方式执行的项目数(虽然我指定RawValue,而不是调用增量)的作品就好了。

The performance counter providing the count of items on the 'queue', which is implemented in the same way (although I assign the RawValue, rather than call Increment) works just fine.

我缺少什么?

推荐答案

我也有问题,此计数器。 MSDN有一个完整的工作的例子,对我帮助很大:

I was also having problem with this counter. MSDN has a full working example that helped me a lot:

http://msdn.microsoft.com/en-us/library/4bcx21aa.aspx

由于他们的例子是相当长篇大论,我煮它归结为一个单一的方法来演示使用该计数器的最基本的要素。当我运行它,我观察每秒10计数在性能监视器的预期值。

As their example was fairly long winded, I boiled it down to a single method to demonstrate the bare essentials of using this counter. When I run it, I observe the expected value of 10 counts per second in PerfMon.

public static void Test()
{
    var ccdc = new CounterCreationDataCollection();

    // add the counter
    const string counterName = "RateOfCountsPerSecond64Sample";
    var rateOfCounts64 = new CounterCreationData
    {
        CounterType = PerformanceCounterType.RateOfCountsPerSecond64,
        CounterName = counterName
    };
    ccdc.Add(rateOfCounts64);

    // create the category
    const string categoryName = "RateOfCountsPerSecond64SampleCategory";
    if (PerformanceCounterCategory.Exists(categoryName))
        PerformanceCounterCategory.Delete(categoryName);
    PerformanceCounterCategory.Create(categoryName, "",
        PerformanceCounterCategoryType.SingleInstance, ccdc);

    // create the counter
    var pc = new PerformanceCounter(categoryName, counterName, false);

    // send some sample data - ten counts per second
    while (true)
    {
        pc.IncrementBy(10);
        System.Threading.Thread.Sleep(1000);
    }
}



我希望这可以帮助别人。

I hope this helps someone.

这篇关于类型RateOfCountsPerSecond32的计数器总是显示0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆