【问题标题】:Assign a float value to performance counter为性能计数器分配一个浮点值
【发布时间】:2015-04-22 06:58:39
【问题描述】:

我在 C# 中创建了一个性能计数器。但是,在为其分配值时,我希望该值是 float 而不是 long,但我似乎不知道该怎么做。有人可以帮忙吗?

我使用的代码来自Counter of type RateOfCountsPerSecond32 always shows 0

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);
    pc.RawValue = 1000; // <-- want to assign a float value here
}

有人可以帮忙吗?

【问题讨论】:

  • RawValue 是一个长属性,那么为什么要将浮点值分配给长属性?

标签: c# performancecounter


【解决方案1】:

嗯,你不能;数据类型为long。只需按某个因子缩放它(这样你就可以保留几个小数位,作为低位数字) - 例如 x1000 - 并四舍五入:

pc.RawValue = (long)(value * 1000);

但是,由于您使用的是RateOfCountsPerSecond32 - 您应该记录总数,而不是比率。后端计算费率。

【讨论】:

    【解决方案2】:

    你不能,如果你仔细想想,这没有任何意义。计数器 count 个不同的事件,你不能有一半的事件。计数器只能在事件发生时递增。

    速率计数器实际上计算间隔之间的基本计数器的差异以用于显示目的。 PerSecond 计数器使用系统提供的间隔计数器,该计数器每秒递增。您只需在事件发生时增加计数器,操作系统将计算速率。

    其他计算平均值的计数器也允许您增加基本计数器,因此您可以拥有例如。 Bytes per Packet柜台

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      相关资源
      最近更新 更多