c# – 跨越不同性能计数器的性能计数器实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 跨越不同性能计数器的性能计数器实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3531字,纯文字阅读大概需要6分钟。
内容图文
![c# – 跨越不同性能计数器的性能计数器实例](/upload/InfoBanner/zyjiaocheng/794/824a42c70f944560a1a1d77689113598.jpg)
我所看到的是我的性能计数器实例被添加到指定计数器之外的性能类别中的其他计数器.
给出以下代码:
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# – 跨越不同性能计数器的性能计数器实例全部内容,希望文章能够帮你解决c# – 跨越不同性能计数器的性能计数器实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。