c# – 跨越不同性能计数器的性能计数器实例
作者:互联网
我所看到的是我的性能计数器实例被添加到指定计数器之外的性能类别中的其他计数器.
给出以下代码:
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication26
{
class Program
{
static void Main(string[] args)
{
string category = "Foo";
string categoryHelp = "Test counters";
string fooCounter1Name = "Test Foo counter 1";
string fooCounter1InstanceName = fooCounter1Name + "Instance";
string fooCounter2Name = "Test Foo counter 2";
string fooCounter2InstanceName = fooCounter2Name + "Instance";
if (PerformanceCounterCategory.Exists(category))
PerformanceCounterCategory.Delete(category);
var counterCreationDataCollection = new CounterCreationDataCollection();
counterCreationDataCollection.Add(new CounterCreationData(fooCounter1Name, "", PerformanceCounterType.RateOfCountsPerSecond64));
counterCreationDataCollection.Add(new CounterCreationData(fooCounter2Name, "", PerformanceCounterType.RateOfCountsPerSecond64));
PerformanceCounterCategory.Create(category, categoryHelp, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection);
PerformanceCounter fooCounter1Instance = new PerformanceCounter();
fooCounter1Instance.CategoryName = category;
fooCounter1Instance.CounterName = fooCounter1Name;
fooCounter1Instance.InstanceName = fooCounter1InstanceName;
fooCounter1Instance.InstanceLifetime = PerformanceCounterInstanceLifetime.Process;
fooCounter1Instance.ReadOnly = false;
PerformanceCounter fooCounter2Instance = new PerformanceCounter();
fooCounter2Instance.CategoryName = category;
fooCounter2Instance.CounterName = fooCounter2Name;
fooCounter2Instance.InstanceName = fooCounter2InstanceName;
fooCounter2Instance.InstanceLifetime = PerformanceCounterInstanceLifetime.Process;
fooCounter2Instance.ReadOnly = false;
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
var task = Task.Factory.StartNew(() =>
{
while (!cancellationTokenSource.IsCancellationRequested)
{
fooCounter1Instance.Increment();
fooCounter2Instance.Increment();
SpinWait.SpinUntil(() => false, 500);
}
}, cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
cancellationTokenSource.Cancel();
task.Wait(10000);
}
}
}
为什么实例会在两个计数器中显示?
来自PerfMon:
我希望实例(fooCounter1InstanceName)只显示在“Test Foo counter 1”计数器下,但它也列在“Test Foo counter 2”下面.更有趣的是,该实例可用于在PerfMon中进行选择,但是值仅在正确的计数器名称下写入实例,例如, “Test Foo counter 1”计数器中的fooCounter1InstanceName和“Test Foo counter 2”计数器中的fooCounter2InstanceName.
我究竟做错了什么?
解决方法:
我找到了自己的答案.来自文档here
In some situations, categories are subdivided into instances, which
track data about multiple occurrences of the object that a category
relates to. Instances apply to the category as whole, rather than to
individual counters. Every counter within a category has each instance
defined for the category. For example, the Process category contains
instances named Idle and System. Every counter within the Process
category thus contains data for each instance, showing information
about either idle processes or system processes.
这意味着我应该定义诸如“每秒读取#个消息”和“每秒发送的#个消息”之类的计数器,然后为该类别创建一个实例,并且在每个计数器下面将成为该实例的“视图”.可能会也可能不会写入.
这似乎是反直觉的(双关语),因为一些计数器不会写入值,所以为什么列出该计数器下的实例以及为什么在设置计数器实例时指定实例名称但给性能监视器的用户给人的印象是计数器何时适用于实例?
标签:c,performancecounter 来源: https://codeday.me/bug/20190624/1279891.html