使用PerformanceCounter获取计算机性能

一 。

这里说的性能是只cpu 内存 磁盘 网络的相关使用情况。可以在任务管理器-》性能中查看这些数据,也可以在资源管理器中查看更详细的情况。

使用的是win8.1的系统,任务管理器的执行挡:C:\Windows\System32\taskmgr.exe .

                                     资源监视器的执行档:C:\Windows\System32\resmon.exe

                                     性能监视器的执行档:C:\Windows\System32\perfmon.exe

二。计数器

PerformanceCounter (String, String, String)
功能:
初始化 PerformanceCounter 类的新的只读实例,
并将其与本地计算机上指定的系统性能计数器或自定义性能计数器及类别实例关联
参数说明:
public PerformanceCounter (
 string categoryName,
 string counterName,
 string instanceName
)
categoryName
想要关联的性能计数器类别名称。
counterName
性能计数器的名称。
instanceName
性能计数器类别的实例。

三。C#代码

//需要的名空间
using System.Diagnostics;
using System.Threading;
using System.Collections;

//获取磁盘写入速率

PerformanceCounter pc = new PerformanceCounter();
            pc.CategoryName = "PhysicalDisk";
            pc.CounterName = "Disk Write Bytes/sec";
            pc.InstanceName = "0 C: E: F:";
            pc.MachineName = ".";
            pc.ReadOnly = true;

            pc.NextValue();

System.Threading.Thread.Sleep(1000); //等1秒,让后系统获取下一个样本

pc.NextValue();

如上面的例子,只要填入三个参数值就可以获取相关的数据了。下面说下这三个参数怎么得到的

//获取性能计数器类别列表
        public static void GetCategoryNameList()
        {
            Console.WriteLine("性能计数器类别列表 : ");
            PerformanceCounterCategory[] myCat2;
            myCat2 = PerformanceCounterCategory.GetCategories();
            for (int i = 0; i < myCat2.Length; i++)
            {
                Console.WriteLine(myCat2[i].CategoryName.ToString());                
            }            
        }

我用的win8.1系统,运行几次这个列表的内容都不同。注意磁盘的种类有两个,LogicalDisk和PhysicalDisk

 //获取性能计数器类别下的实例的名称 实例下的性能计数器的名称
 public static void GetInstanceNameListANDCounterNameList(string CategoryName)
        {
            string[] instanceNames;
            ArrayList counters = new ArrayList();
            PerformanceCounterCategory mycat = new PerformanceCounterCategory(CategoryName);
            try
            {
                instanceNames = mycat.GetInstanceNames();
                if (instanceNames.Length == 0)
                {
                    counters.AddRange(mycat.GetCounters());
                }
                else
                {
                    for (int i = 0; i < instanceNames.Length; i++)
                    {
                        counters.AddRange(mycat.GetCounters(instanceNames[i]));
                    }
                }
                for (int i = 0; i < instanceNames.Length; i++)
                {
                    Console.WriteLine(instanceNames[i]);//获取性能计数器类别下的实例
                }
                Console.WriteLine("******************************");
                foreach (PerformanceCounter counter in counters)
                {
                    Console.WriteLine(counter.CounterName);
                }
            }
            catch (Exception)
            {
                 Console.WriteLine("Unable to list the counters for this category");
            }
        }

比如GetInstanceNameListANDCounterNameList("PhysicalDisk");

得到的实例有两个_Total和0 C: E: F:   //我的计算机没有D盘

两个实例下的计数器是一样的

每个计数器代表的意思:

Current Disk Queue Length   //磁盘队列长度
% Disk Time                           //当前物理磁盘利用率    
Avg. Disk Queue Length       //等待进行磁盘访问的平均系统请求数量
% Disk Read Time
Avg. Disk Read Queue Length
% Disk Write Time
Avg. Disk Write Queue Length

Avg. Disk sec/Transfer   //磁盘完成请求所用的时间
Avg. Disk sec/Read       //从磁盘对数据进行一个读操作所需的平均时间(单位:秒)。
Avg. Disk sec/Write

Disk Transfers/sec     //磁盘IOPS 。Disk Transfers/sec = Disk Reads/sec + Disk Writes/sec
Disk Reads/sec         //在读取操作时从磁盘上传送的字节平均数
Disk Writes/sec
Disk Bytes/sec         //读写期间的字节传输率

Disk Read Bytes/sec   
Disk Write Bytes/sec

Avg. Disk Bytes/Transfer
Avg. Disk Bytes/Read
Avg. Disk Bytes/Write

% Idle Time
Split IO/sec


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值