了解有关PerformanceCounter的信息

时间:2014-07-03 14:06:57

标签: c# .net windows performance monitoring

我正在尝试使用PerformanceCounter来检索Windows上的一些性能详细信息。我使用了PerformanceCounterCategory.GetCategories方法并运行了链接中的示例代码,该代码提供了运行它的机器上的所有类别名称。

我运行了BlackWasp.co.uk的代码,演示了如何将PerformanceCounter与示例代码一起使用。

我缺少的是,constructor至少需要两个字符串作为参数,counterCategory和counterName。我可以从PerformanceCounterCategory.GetCategories获取计数器类别但是如何设置名称?

我从中理解我应该使用PerformanceCounterCategory.Create(...)设置名称,但是如何将其与我想要监视的指定行为(例如磁盘读取等)联系起来?

示例代码为:

Console.WriteLine("Creating Inventory custom counter");
if (!PerformanceCounterCategory.Exists("Inventory"))
    PerformanceCounterCategory.Create("Inventory",
        "Truck inventory",
        PerformanceCounterCategoryType.SingleInstance,
        "Trucks", "Number of trucks on hand");

这种情况让我陷入黑暗中,因为我不知道“卡车库存”或“卡车”的来源。

有人能指出我正确的方向吗?有没有更好的方法来进行绩效监控?

那么如何创建性能计数器并将其与有意义的硬件性能联系起来呢?

1 个答案:

答案 0 :(得分:0)

如果您想以编程方式执行此操作,我找到了我要查找的内容here(如下所述)和here

基本上,如果你需要找出你可以访问的性能计数器 -

开始 - >搜索框 - >性能监视器

点击perfmon.exe。这会给你一个窗口。单击

监控工具 - >绩效监测

这将为您提供图表。右键单击图表,然后单击添加计数器

这将为您提供所有可能的计数器类别的列表。单击类别列表左侧的箭头,将显示所有可能的名称和实例的列表。

这涵盖了PerformanceCounter的两个主要构造函数:

PerformanceCounter(string counterCategory, string counterName)
PerformanceCounter(string counterCategory, string counterName, string counterInstance)

另外,如果你像我一样对这个话题是新手,那么你可能会知道这必须与计时器(一般来说)相关联,因为它需要从最后一个时间间隔进行测量。

这是一个裸骨控制台应用程序:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Performance
{
    class PerformanceTest
    {
        PerformanceCounter memory;

        Timer timer;

        public PerformanceTest()
        {
            memory = new PerformanceCounter("Memory", "Available MBytes");
            // Get a new value every 10 seconds
            timer = new Timer(PerformanceTimer_Tick, null, 0, 10000);
        }

        static void Main(string[] args)
        {

            PerformanceTest pt = new PerformanceTest();

            Console.ReadLine();
        }

        private void PerformanceTimer_Tick(object sender)
        {
            Console.WriteLine("Memory Available: " + memory.NextValue().ToString() + "\n");
            // Force garbage collection
            GC.Collect();
        }
    }
}