2 AverageTimer32 性能计数器
Posted
技术标签:
【中文标题】2 AverageTimer32 性能计数器【英文标题】:2 AverageTimer32 Performance Counter 【发布时间】:2012-07-01 10:17:06 【问题描述】:如何在一个应用程序 (.exe) 中定义和使用两个 AverageTimer32 性能计数器?
附加说明: 我正在尝试衡量在操作的不同部分花费的时间。
我有一个 AverageTimer32 性能计数器和一个 AverageBase。它有效。
现在我想添加第二个 AverageTimer32 性能计数器。即使我添加另一个 AverageBase 计数器,它也不起作用。我想我错了。代码示例非常感谢。
这是代码的不同部分:
_AverageDuration = new PerformanceCounter();
_AverageDuration.CategoryName = CategoryName;
_AverageDuration.CounterName = "average time per operation";
_AverageDuration.MachineName = ".";
_AverageDuration.ReadOnly = false;
_AverageDurationBase = new PerformanceCounter();
_AverageDurationBase.CategoryName = CategoryName;
_AverageDurationBase.CounterName = "average time per operation base";
_AverageDurationBase.MachineName = ".";
_AverageDurationBase.ReadOnly = false;
...
CounterCreationDataCollection counters = new CounterCreationDataCollection();
...
CounterCreationData avgDuration = new CounterCreationData();
avgDuration.CounterName = "average time per operation";
avgDuration.CounterHelp = "Average duration per operation execution";
avgDuration.CounterType = PerformanceCounterType.AverageTimer32;
counters.Add(avgDuration);
CounterCreationData avgDurationBase = new CounterCreationData();
avgDurationBase.CounterName = "average time per operation base";
avgDurationBase.CounterHelp = "Average duration per operation execution base";
avgDurationBase.CounterType = PerformanceCounterType.AverageBase;
counters.Add(avgDurationBase);
...
PerformanceCounterCategory.Create(CategoryName, "Category for Receiver", PerformanceCounterCategoryType.Unknown, counters);
...
AverageDuration.IncrementBy(performanceCounterwatch.ElapsedTicks);
AverageDurationBase.Increment();
【问题讨论】:
顺便说一句,您可以考虑使用Using XSLT to Generate Performance Counters Code 之类的东西以避免重复所有代码 【参考方案1】:添加它们的顺序很关键。您必须添加平均值,然后是其对应的基数,然后是第二个平均值,然后是第二个对应的基数。
【讨论】:
我(认为)还必须删除该类别并使用新的性能计数器再次添加它。以上是关于2 AverageTimer32 性能计数器的主要内容,如果未能解决你的问题,请参考以下文章