Yos*_*han 13 c# performancecounter
我有一个Windows服务,通过WCF服务接口提供某些虚拟队列的消息.我想揭露两个性能指标 -
第一个工作正常,第二个在PerfMon.exe中始终显示为0,尽管RawValue看起来是正确的.
我正在创建计数器 -
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);
}
Run Code Online (Sandbox Code Playgroud)
然后,在每次服务呼叫时,我增加相关的计数器,对于每秒/秒计数器,这当前看起来像这样 -
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();
Run Code Online (Sandbox Code Playgroud)
如上所述 - 如果我在增加调用之后放置一个断点,我可以看到RawValue不断增加,与服务调用一致(相当频繁,每秒不止一次,我想)但是性能计数器本身保持不变0.
提供"队列"项目计数的性能计数器,以相同的方式实现(虽然我分配RawValue,而不是调用Increment)工作得很好.
我错过了什么?
Mik*_*ain 15
我最初也有这个柜台的问题.MSDN有一个完整的工作示例,帮助了我很多:
http://msdn.microsoft.com/en-us/library/4bcx21aa.aspx
由于他们的例子相当漫长,我将其归结为一种方法来展示最基本的东西.运行时,我在PerfMon中看到每秒10个计数的预期值.
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);
// ensure category exists
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 - roughly ten counts per second
while (true)
{
pc.IncrementBy(10);
System.Threading.Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助别人.
当您使用Average性能计数器类型时,有两个组成部分 - 分子和分母。因为您使用的是平均值,所以计数器计算为“每 y 实例 x 个实例”。在您的情况下,您正在计算每“秒数”的“数量项目”。换句话说,您需要计算从队列中取出的项目数量以及删除它们所需的秒数。
Performance Counters类型Average实际上创建了两个计数器 - 一个名为 的分子组件{name}和一个名为 的分母组件{name}Base。如果您转到性能计数器管理单元,您可以查看所有类别和计数器;您可以查看柜台名称Base。当队列处理过程开始时,您应该
{name}从队列中删除的项目数{name}Base秒表上的刻度数计数器应该自动知道将第一个计数器除以第二个计数器以获得平均速率。检查CodeProject以获取其工作原理的好示例。
您很可能不想要这种类型的计数器。这些Average计数器用于确定每秒操作发生多少个实例;例如,完成订单或执行某些复杂交易或流程所需的平均秒数。您可能想要的是“实时”的平均实例数,而不是处理时间。
考虑一下,如果队列中有 1 个项目,并且需要 1 毫秒才能删除,即每秒 1000 个项目。但一秒钟后,您只删除了 1 个项目(因为这就是全部),因此您每秒“实时”处理 1 个项目。同样,如果队列中有 100 万个项目,但您只处理了一个,因为您的服务器正忙于执行其他工作,那么您希望看到理论值/秒 1000 个项目还是实际值/秒 1 个项目?
如果您想要这个“真实”数字,而不是理论吞吐量数字,那么这种情况并不真正适合性能计数器 - 相反,您需要知道开始时间、结束时间以及已处理的项目数。这确实不能用一个简单的“计数器”来完成。相反,您可以在某处存储系统启动时间,并计算(number of items) / (now - startup time)。
| 归档时间: |
|
| 查看次数: |
5669 次 |
| 最近记录: |