windows性能监视功能

来源:互联网 发布:莆田淘宝美工培训 编辑:程序博客网 时间:2024/05/22 00:34

这篇主要说说windows系统自带的性能监视功能----->performancecouonter.

打开管理工具-->性能,我们可以立即看到服务器的CPU,进程运行时间,磁盘容量等性能参数走势图。然而不仅仅是这几项,我们可以通过添加技术器来查看其他的性能指标:

如果你说,这么看太麻烦了,OK,我们通过C#将这些值取出来,用于实现自身的性能监视:

1.添加引用:

using System.Diagnostics;

2.创建并实例化PerformanceCounter

public static System.Diagnostics.PerformanceCounter pc= new System.Diagnostics.PerformanceCounter();
public static System.Diagnostics.PerformanceCounter pcm= new System.Diagnostics.PerformanceCounter();
public static System.Diagnostics.PerformanceCounter pcb= new System.Diagnostics.PerformanceCounter();
public static System.Diagnostics.PerformanceCounter pcc= new System.Diagnostics.PerformanceCounter();
//我们用四个对象做不同的操作,注意:是static的,不然每次取出的数据都是初始值,如cpu利用率就是0

3.构造函数

static CapabilityScout()
{
pc.CategoryName 
= "Processor";
pc.CounterName 
= "% Processor Time";
pc.InstanceName 
= "_Total";
pc.MachineName 
= ".";
pcm.CategoryName 
= "Memory";
pcm.CounterName 
= "% Committed Bytes In Use";
pcm.MachineName 
= ".";
pcb.CategoryName 
= "Windows Media Unicast Service";
pcb.CounterName 
= "Allocated Bandwidth";
pcb.MachineName 
= ".";
pcc.CategoryName 
= "Windows Media Unicast Service";
pcc.CounterName 
= "Connected Clients";
pcc.MachineName 
= ".";
}

4.获取计数器值


        
#region 获取CPU利用率
        
public static string getCpuUsage()
        
{
            
string used = pc.NextValue().ToString();
            
return used;
        }

        
#endregion

        
#region 获取内存使用率
        
public static string getMemory()
        
{
            
float used = pcm.NextValue();
            
return used.ToString();
        }

        
#endregion

        
#region 获取WMS连接数
        
public static string getConnectedCount()
        
{
            
string count = pcc.NextValue().ToString();
            
return count;
        }

        
#endregion

        
#region 获取网络流量
        
public static string getServerBandWidth()
        
{
            
string bandwidth = pcb.NextValue().ToString();
            
return bandwidth;
        }

        
#endregion

当然,这里只是其中及少的部分,不过通过使用同样的方式,我们可以获取更多的性能以及进程运行的情况,但是要说明的一点是,所获取的数据必定是windows服务所提供的,当然我们也可以自己写一些windows服务,添加到系统performancecounter中来,对.net来说也是非常方便的。


原创粉丝点击