【问题标题】:Confusion about AverageTimer32 PerformanceCounter关于 AverageTimer32 PerformanceCounter 的困惑
【发布时间】:2013-03-25 14:46:33
【问题描述】:

我正在尝试设置PerformanceCounter 来测量某个方法的平均执行时间。我尝试阅读 AverageTimer32 并查看了很多示例,但我似乎无法正确理解。

我设置了类别

CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

// Add the counter.
CounterCreationData averageTimer32 = new CounterCreationData();
averageTimer32.CounterType = PerformanceCounterType.AverageTimer32;
averageTimer32.CounterName = counterName;
CCDC.Add(averageTimer32);

// Add the base counter.
CounterCreationData averageTimer32Base = new CounterCreationData();
averageTimer32Base.CounterType = PerformanceCounterType.AverageBase;
averageTimer32Base.CounterName = baseCounterName;
CCDC.Add(averageTimer32Base);

// Create the category.
PerformanceCounterCategory.Create(categoryName, "Demonstrates usage of the AverageTimer32 performance counter type", PerformanceCounterCategoryType.SingleInstance, CCDC);

然后我创建计数器

PC = new PerformanceCounter(categoryName, counterName, false);

BPC = new PerformanceCounter(categoryName, baseCounterName, false);

PC.RawValue = 0;
BPC.RawValue = 0;

最后我记录每次调用我的方法时的经过时间

private void TheMethodIWantToMeasure() {
    Stopwatch stopwatch = Stopwatch.StartNew();

    // Fake work that take ~50ms
    Thread.Sleep(50 + random.Next(-10, 10));

    stopwatch.Stop();

    PC.IncrementBy(stopwatch.ElapsedTicks);
    BPC.Increment();
}

这样做,我最终得到的性能监视器看起来像这样。我在 50 毫秒左右得到尖峰而不是连续曲线:

我误解了AverageTimer32 吗?我读过它,但它有点令人困惑。但是,我已经看到示例与我做几乎相同的事情,所以我猜它应该可以工作。我只得到尖峰的原因可能是什么?

编辑 值得一提的是,TheMethodIWantToMeasure 仅每 ~5 秒调用一次,而我刚刚意识到我每 ~5 秒就会收到一次尖峰信号。但我不明白如果AverageTimer32 使用公式 ((N 1 -N 0)/F)/(B 1 -B 0) 会如何影响结果。它不应该取决于我存储 N 和 B 值的频率吗?

【问题讨论】:

    标签: c# .net performancecounter


    【解决方案1】:

    您的答案在于您的 permon 设置的刷新/采样率。如果您要取出 ~5s 间隔或至少将其更改为 ~10ms 之类的时间,您可能会注意到图表看起来更像您最初的预期。或者,将您的性能计数器刷新率更新为更高的间隔(30 秒)将是相同的。 (通过右键单击 perfMon 图 -> 属性 -> 常规选项卡 -> 每 x 秒采样一次)。

    原因是 perfMon 每 1 秒刷新一次(默认情况下),然后需要显示你的平均值。因此,它会采用“所有”您在那一秒内添加到计数器的操作并将其绘制在图表上。

    示例:如果您在一秒钟内执行了 3 个操作(0.1ms、0.2ms 和 0.3,ms),则会显示 perfMon你的平均值是 0.2ms,这是正确的。

    为什么会有差距?我相信这是因为现在在计算出你的平均值并且你看到你的“Spike”之后,下一秒(当 perfMon 再次刷新时),它将计算 0secs = 0 内 0 次操作的平均值。

    我的建议(如果您想真正了解您的 TheMethodIWantToMeasure 平均运行多长时间的平均值,请完全取消您的 ~5 秒间隔,然后让该方法持续运行. 这应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 2020-03-06
      • 2016-03-16
      • 2013-07-27
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多