当前位置:编程学习 > C#/ASP.NET >>

PerformanceCounter 详解,使用方法

 

这两天写代码控制CPU的占用率,显示为一条直线或者曲线,显示为一条直线的时候用到了PerformanceCounter 这个类。

 

 

 

由于对这个类还是很感兴趣的,在网上找了一些资料也不是很清楚。就自己研究了一下。

 

 

 

首先,PerformanceCounter 分为各个category, 每个category 对应不同类型的资源,一般有"Processor", "IPv6",什么的。。。下面有一个所有的类型的列表:

 

view plain

Thread 

RAS Total 

TCPv6 

TCPv4 

Paging File 

SQLServer:Latches 

IPsec AuthIP IPv4 

MSDTC Bridge 4.0.0.0 

IPsec AuthIP IPv6 

.NET CLR Data 

WF (System.Workflow) 4.0.0.0 

Synchronization 

Processor 

Security Per-Process Statistics 

MSDTC Bridge 3.0.0.0 

Generic IKEv1, AuthIP, and IKEv2 

Database ==> TableClasses 

Event Tracing for Windows Session 

ASP.NET v4.0.30319 

.NET CLR Networking 

Objects 

Terminal Services 

BITS Net Utilization 

SQLServer:User Settable 

.NET CLR Exceptions 

IPsec IKEv2 IPv6 

Process 

IPsec IKEv2 IPv4 

SQLServer:Broker Activation 

WFPv6 

SQLServer:Database Mirroring 

Search Gatherer 

Teredo Relay 

IPv4 

SQLServer:Cursor Manager Total 

IPv6 

Job Object Details 

WFPv4 

IPHTTPS Global 

ICMP 

Offline Files 

SQLServer:Cursor Manager by Type 

SQLAgent:JobSteps 

PhysicalDisk 

Search Indexer 

Windows Workflow Foundation 

Teredo Server 

ServiceModelService 3.0.0.0 

Processor Information 

SQLServer:Replication Logreader 

SQLServer:Transactions 

ASP.NET Apps v4.0.30319 

SQLServer:Broker TO Statistics 

SQLServer:Deprecated Features 

Per Processor Network Inte易做图ce Card Activity 

Database 

Browser 

.NET CLR Remoting 

Pacer Pipe 

Event Tracing for Windows 

.NET CLR LocksAndThreads 

Job Object 

Client Side Caching 

.NET Data Provider for SqlServer 

ASP.NET Applications 

Terminal Services Session 

SQLServer:Broker/DBM Transport 

Network Inte易做图ce 

.NET Memory Cache 4.0 

SQLServer:General Statistics 

SQLServer:Resource Pool Stats 

SQLServer:Replication Merge 

Security System-Wide Statistics 

SQLServer:Backup Device 

UDPv6 

.NET CLR Security 

Outlook 

.NET CLR Jit 

SQLServer:Workload Group Stats 

WFP 

ASP.NET State Service 

SMSvcHost 4.0.0.0 

SQLServer:Databases 

SQLServer:Locks 

Server Work Queues 

ICMPv6 

NBT Connection 

.NET CLR Loading 

Server 

SQLAgent:Jobs 

RAS Port 

Per Processor Network Activity Cycles 

SMSvcHost 3.0.0.0 

ServiceModelEndpoint 3.0.0.0 

.NET CLR Networking 4.0.0.0 

ASP.NET 

USB 

SQLServer:Memory Manager 

Cache 

SQLServer:Replication Snapshot 

System 

Redirector 

SQLServer:Wait Statistics 

SQLAgent:Statistics 

LogicalDisk 

.NET Data Provider for Oracle 

.NET CLR Interop 

分布式事务处理协调器 

WMI Objects 

.NET CLR Memory 

SQLServer:Access Methods 

SQLServer:Buffer Partition 

SQLServer:Buffer Manager 

Database ==> Instances 

ReadyBoost Cache 

SQLServer:Plan Cache 

SQLServer:Replication Agents 

SQLServer:Exec Statistics 

Teredo Client 

Telephony 

IPsec IKEv1 IPv6 

Memory 

SQLServer:SQL Errors 

SQLServer:Replication Dist. 

ServiceModelOperation 3.0.0.0 

SQLServer:Buffer Node 

IPsec IKEv1 IPv4 

Search Gatherer Projects 

IPsec Driver 

SQLServer:Broker Statistics 

SQLServer:SQL Statistics 

SQLServer:CLR 

SQLAgent:Alerts 

UDPv4 

SQLServer:Catalog Metadata 

 

 

然后,每个类型下面对应多个实例,例如通过如下代码获得Processor的实例:

 

 

 

view plain

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.Diagnostics; 

 

namespace PerformanceCounterTest 

    class Program 

    { 

        static void Main(string[] args) 

        { 

            PerformanceCounterCategory[] pcc = PerformanceCounterCategory.GetCategories(); 

            for (int i = 0; i < pcc.Length; i++) 

            { 

                if (pcc[i].CategoryName=="Processor") 

                { 

                    Console.WriteLine("-----------------------------------"); 

                    Console.WriteLine(pcc[i].CategoryName); 

                    Console.WriteLine("-----------------------------------"); 

                    //Console.WriteLine(pcc[i].MachineName); 

                    string[] instanceNames = pcc[i].GetInstanceNames(); 

                    for (int j = 0; j < instanceNames.Length; j++) 

                    { 

                        Console.WriteLine("**** Instance Name **********"); 

               

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,