PerformanceCounter 基本介绍以及示例方法

一 PerformanceCounter 基本介绍
1 简单介绍
表示 Windows NT 性能计数器组件
命名空间:System.Diagnostics
程序集:System(在 system.dll 中)
2 构造函数(只介绍本文要用到的)
PerformanceCounter (String, String, String)
功能:
初始化 PerformanceCounter 类的新的只读实例,
并将其与本地计算机上指定的系统性能计数器或自定义性能计数器及类别实例关联
参数说明:
public PerformanceCounter (
 string categoryName,
 string counterName,
 string instanceName
)
categoryName
性能计数器关联的性能计数器类别(性能对象)的名称。
counterName
性能计数器的名称。
instanceName
性能计数器类别实例的名称,或者为空字符串 ("")(如果该类别包含单个实例)。
二 示例方法:
需要引用命名空间

using  System.Diagnostics;
using  System.Threading;
using  System.Collections;

1 获取性能计数器类别列表
虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“Cache”(缓存)、“Memory”(内存)、
“Objects”(对象)、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、
“Server”(服务器)、“System”(系统
)和“Thread”(线程)等类别

         public   static   void  GetCategoryNameList()
        {
            PerformanceCounterCategory[] myCat2;
            myCat2 
=  PerformanceCounterCategory.GetCategories();
            
for  ( int  i  =   0 ; i  <  myCat2.Length; i ++ )
            {
                Console.WriteLine(myCat2[i].CategoryName.ToString());
            }
        }

2 获取性能计数器类别下的实例的名称实例下的性能计数器的名称

         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 " );
            }
        }

3 根据categoryName,counterName,instanceName获得性能情况显示

         private   static   void  PerformanceCounterFun( string  CategoryName,  string  InstanceName,  string  CounterName)
        {
            PerformanceCounter pc 
=   new  PerformanceCounter(CategoryName, CounterName, InstanceName);
            
while  ( true )
            {
                Thread.Sleep(
1000 );  //  wait for 1 second 
                 float  cpuLoad  =  pc.NextValue();
                Console.WriteLine(
" CPU load =  "   +  cpuLoad  +   "  %. " );
            }
        }

4 调用方法3显示cpu使用率

PerformanceCounterFun( " Processor " " _Total " " % Processor Time " );
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PerformanceCounter是一个.NET Framework中的类,用于监视应用程序的性能。它可以用来测量CPU使用率、内存使用率、磁盘读写速度、网络流量等指标。通过PerformanceCounter,开发人员可以实时监控应用程序的性能,及时发现和解决性能问题。 ### 回答2: PerformanceCounter是.NET Framework中的一个类,用于跟踪各种系统性能计数器的信息。它提供了一种简单的方法来查看操作系统、网络、数据库等各种资源的使用情况,从而使开发人员能够更好地了解应用程序的性能瓶颈并对其进行优化。 在使用PerformanceCounter时,我们可以使用它来跟踪CPU和内存的使用情况,以及网络延迟、磁盘读写等系统指标的值。我们可以通过创建PerformanceCounter对象,设置所需的计数器类别和实例名称,然后使用该对象来读取计数器的值。 除了可以用于监视系统性能之外,PerformanceCounter还可以用于创建自定义的性能计数器,并将其嵌入到我们自己的应用程序中。这些自定义计数器可以用于跟踪我们的应用程序的特定性能指标,例如数据库查询次数、请求处理速度等。这些计数器可以在代码中动态地增加或删除,并且它们的值可以随着应用程序的运行而实时更新。 总之,PerformanceCounter是一个非常有用的.NET类,它可以帮助我们了解应用程序的性能瓶颈,并提供了一种简单的方法来创建和跟踪自定义性能计数器。无论是在开发过程中还是在应用程序部署后,都可以利用它来优化我们的应用程序。 ### 回答3: PerformanceCounter是.NET Framework中的一个类,用于获取应用程序或系统的性能计数器数据。性能计数器提供了一个逐步检测、记录和监视应用程序或系统性能的方法PerformanceCounter类有以下几种用途: 1.监视应用程序的性能 在应用程序运行时,可以使用PerformanceCounter类来监视应用程序的性能。可以使用PerformanceCounter类来测量CPU使用率、内存使用率、磁盘I/O 使用率以及网络I/O使用率等。 2.优化应用程序的性能 使用PerformanceCounter类,将应用程序的性能数据记录在日志文件或数据库中,可以帮助开发人员分析应用程序性能的瓶颈和优化应用程序性能。 3.监视系统的性能 可以使用PerformanceCounter类来监视操作系统的性能。例如,可以使用PerformanceCounter类来监视CPU使用率、内存使用率、磁盘I/O使用率以及网络I/O使用率等,从而可以识别系统性能瓶颈和故障。 总的来说,PerformanceCounter是.NET Framework中一个强大的类,它可以帮助开发人员监视和优化应用程序的性能,以及识别系统性能瓶颈和故障。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值