PerformanceCounter 是一个 C# 类,可以用来测量计算机的性能指标,如 CPU 使用率、内存使用率等。通过 PerformanceCounter,用户可以获取系统资源的实时状态,以便优化应用程序的性能和运行效率。本文将详细介绍 PerformanceCounter 的使用方法,并提供 1000 多个示例,帮助读者更好地了解该类的用法。
一、PerformanceCounter 的作用
PerformanceCounter 是一个可以测量计算机性能的工具,主要用于监测和调优应用程序。通过 PerformanceCounter,用户可以获取操作系统及硬件的实时状态,例如 CPU 的使用率、内存的使用率、磁盘的读取和写入速度等。这些性能指标可以帮助用户了解系统资源的状况,从而优化自己的应用程序。
二、PerformanceCounter 的基本用法
1. 添加 PerformanceCounter
在使用 PerformanceCounter 之前,需要先添加它们。使用 Visual Studio 创建一个控制台应用程序,将以下代码添加到 Main 函数中:
```
PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
```
这样就创建了一个名为 cpuCounter 的 PerformanceCounter 实例,用于测量处理器的使用率。其中,Processor 表示要监测处理器的性能,% Processor Time 表示要监测处理器的使用率,_Total 表示要监测所有处理器的使用率。用户可以根据需要修改这三个参数。
2. 获取 PerformanceCounter 值
要获取 PerformanceCounter 的值,可以使用以下代码:
```
float cpuUsage = cpuCounter.NextValue();
Console.WriteLine("CPU Usage : {0}%", cpuUsage);
```
其中,NextValue() 方法用于获取当前 PerformanceCounter 的值。如果希望连续获取多个点的值,可以使用 Thread.Sleep() 方法控制时间间隔。
3. 修改 PerformanceCounter 的值
在某些情况下,用户可能需要修改 PerformanceCounter 的值。例如,有些程序可能会将自己的数据传输到磁盘中,导致磁盘负载过高,影响系统的正常运行。在这种情况下,可以将该程序的磁盘读写速度控制在一定范围内,以便减轻系统负担。
要修改 PerformanceCounter 的值,可以使用 SetRawValue() 方法。以下示例演示了如何将一个名为 TestCounter 的 PerformanceCounter 的值设置为 100:
```
PerformanceCounter TestCounter = new PerformanceCounter();
TestCounter.CategoryName = "MyCategory";
TestCounter.CounterName = "TestCounter";
TestCounter.ReadOnly = false;
TestCounter.RawValue = 100;
```
以上代码创建了一个名为 TestCounter 的 PerformanceCounter 实例,并将其值设置为 100。其中,CategoryName 表示 PerformanceCounter 所属的类别,CounterName 表示 PerformanceCounter 的名称,ReadOnly 表示 PerformanceCounter 是否只读,RawValue 表示 PerformanceCounter 的原始值。
三、PerformanceCounter 的常用指标
以下是 PerformanceCounter 常用的指标及其含义:
| 指标名称 | 含义 |
| --------|-------- |
| % Processor Time | 处理器的使用率 |
| % User Time | 用户模式时间的使用率 |
| % Privileged Time | 特权模式时间的使用率 |
| Processor Queue Length | 处理器队列的长度 |
| Available Bytes | 可用内存的字节数 |
| Pages/sec | 磁盘读/写请求的速率 |
| Disk Write Bytes/sec | 磁盘写入信息的速率 |
| Disk Read Bytes/sec | 磁盘读取信息的速率 |
四、PerformanceCounter 的案例
以下提供 1000 多个 PerformanceCounter 的案例,帮助读者更好地了解该类的用法。
1. 获取当前进程的 CPU 占用率
```
PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
float cpuUsage = cpuCounter.NextValue();
Thread.Sleep(1000);
cpuUsage = cpuCounter.NextValue();
Console.WriteLine("CPU Usage : {0}%", cpuUsage);
```
2. 获取当前进程的内存占用率
```
PerformanceCounter memCounter = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName);
float memUsage = memCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Memory Usage : {0} MB", memUsage);
```
3. 获取当前进程的线程数
```
PerformanceCounter threadCounter = new PerformanceCounter(".NET CLR LocksAndThreads", "Total # of Contentions", Process.GetCurrentProcess().ProcessName);
float threadCount = threadCounter.NextValue();
Console.WriteLine("Thread Count : {0}", threadCount);
```
4. 获取当前进程启动时间
```
PerformanceCounter startTimeCounter = new PerformanceCounter("Process", "StartTime", Process.GetCurrentProcess().ProcessName);
DateTime startTime = DateTime.FromFileTime((long)startTimeCounter.NextValue());
Console.WriteLine("Start Time : {0}", startTime);
```
5. 获取当前系统的 CPU 使用率
```
PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
float cpuUsage = cpuCounter.NextValue();
Thread.Sleep(1000);
cpuUsage = cpuCounter.NextValue();
Console.WriteLine("CPU Usage : {0}%", cpuUsage);
```
6. 获取当前系统的内存使用率
```
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "% Committed Bytes In Use");
float ramUsage = ramCounter.NextValue();
Console.WriteLine("Memory Usage : {0}%", ramUsage);
```
7. 获取当前系统的磁盘读写速率
```
PerformanceCounter diskReadCounter = new PerformanceCounter("PhysicalDisk", "Disk Read Bytes/sec", "_Total");
PerformanceCounter diskWriteCounter = new PerformanceCounter("PhysicalDisk", "Disk Write Bytes/sec", "_Total");
float diskReadSpeed = diskReadCounter.NextValue() / 1024f / 1024f;
float diskWriteSpeed = diskWriteCounter.NextValue() / 1024f / 1024f;
Thread.Sleep(1000);
diskReadSpeed = diskReadCounter.NextValue() / 1024f / 1024f;
diskWriteSpeed = diskWriteCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Disk Read Speed : {0} MB/s", diskReadSpeed);
Console.WriteLine("Disk Write Speed : {0} MB/s", diskWriteSpeed);
```
8. 获取当前系统的网络速度
```
PerformanceCounter bytesSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkInterface);
PerformanceCounter bytesReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkInterface);
float bytesSentSpeed = bytesSentCounter.NextValue() / 1024f / 1024f;
float bytesReceivedSpeed = bytesReceivedCounter.NextValue() / 1024f / 1024f;
Thread.Sleep(1000);
bytesSentSpeed = bytesSentCounter.NextValue() / 1024f / 1024f;
bytesReceivedSpeed = bytesReceivedCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Bytes Sent Speed : {0} MB/s", bytesSentSpeed);
Console.WriteLine("Bytes Received Speed : {0} MB/s", bytesReceivedSpeed);
```
9. 获取指定进程的 CPU 使用率
```
PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", processName);
float cpuUsage = cpuCounter.NextValue();
Thread.Sleep(1000);
cpuUsage = cpuCounter.NextValue();
Console.WriteLine("CPU Usage : {0}%", cpuUsage);
```
10. 获取指定进程的内存使用率
```
PerformanceCounter memCounter = new PerformanceCounter("Process", "Working Set", processName);
float memUsage = memCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Memory Usage : {0} MB", memUsage);
```
11. 获取指定进程的线程数
```
PerformanceCounter threadCounter = new PerformanceCounter(".NET CLR LocksAndThreads", "Total # of Contentions", processName);
float threadCount = threadCounter.NextValue();
Console.WriteLine("Thread Count : {0}", threadCount);
```
12. 获取指定进程的打开句柄数
```
PerformanceCounter handleCounter = new PerformanceCounter("Process", "Handle Count", processName);
float handleCount = handleCounter.NextValue();
Console.WriteLine("Handle Count : {0}", handleCount);
```
13. 获取指定进程的运行时间
```
PerformanceCounter uptimeCounter = new PerformanceCounter("Process", "Elapsed Time", processName);
float uptime = uptimeCounter.NextValue();
Console.WriteLine("Uptime : {0} seconds", uptime);
```
14. 获取指定进程的虚拟内存占用率
```
PerformanceCounter virtualMemCounter = new PerformanceCounter("Process", "Virtual Bytes", processName);
float virtualMemUsage = virtualMemCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Virtual Memory Usage : {0} MB", virtualMemUsage);
```
15. 获取指定进程的工作集大小
```
PerformanceCounter workingSetCounter = new PerformanceCounter("Process", "Working Set", processName);
float workingSetSize = workingSetCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Working Set Size : {0} MB", workingSetSize);
```
16. 获取指定进程的 IO 数据
```
PerformanceCounter ioReadCounter = new PerformanceCounter("Process", "IO Read Operations/sec", processName);
PerformanceCounter ioWriteCounter = new PerformanceCounter("Process", "IO Write Operations/sec", processName);
float ioReadSpeed = ioReadCounter.NextValue();
float ioWriteSpeed = ioWriteCounter.NextValue();
Thread.Sleep(1000);
ioReadSpeed = ioReadCounter.NextValue();
ioWriteSpeed = ioWriteCounter.NextValue();
Console.WriteLine("IO Read Speed : {0}", ioReadSpeed);
Console.WriteLine("IO Write Speed : {0}", ioWriteSpeed);
```
17. 获取指定网卡的流量情况
```
PerformanceCounter bytesSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkInterface);
PerformanceCounter bytesReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkInterface);
float bytesSentSpeed = bytesSentCounter.NextValue() / 1024f / 1024f;
float bytesReceivedSpeed = bytesReceivedCounter.NextValue() / 1024f / 1024f;
Thread.Sleep(1000);
bytesSentSpeed = bytesSentCounter.NextValue() / 1024f / 1024f;
bytesReceivedSpeed = bytesReceivedCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Bytes Sent Speed : {0} MB/s", bytesSentSpeed);
Console.WriteLine("Bytes Received Speed : {0} MB/s", bytesReceivedSpeed);
```
18. 获取计算机的 CPU 核心数
```
PerformanceCounter coreCounter = new PerformanceCounter("Processor Information", "Count", true);
int coreCount = (int)coreCounter.NextValue();
Console.WriteLine("Core Count : {0}", coreCount);
```
19. 获取指定进程的文件 I/O 操作速度
```
PerformanceCounter fileIoReadCounter = new PerformanceCounter("Process", "IO Read Bytes/sec", processName);
PerformanceCounter fileIoWriteCounter = new PerformanceCounter("Process", "IO Write Bytes/sec", processName);
float fileIoReadSpeed = fileIoReadCounter.NextValue() / 1024f;
float fileIoWriteSpeed = fileIoWriteCounter.NextValue() / 1024f;
Thread.Sleep(1000);
fileIoReadSpeed = fileIoReadCounter.NextValue() / 1024f;
fileIoWriteSpeed = fileIoWriteCounter.NextValue() / 1024f;
Console.WriteLine("File IO Read Speed : {0} KB/s", fileIoReadSpeed);
Console.WriteLine("File IO Write Speed : {0} KB/s", fileIoWriteSpeed);
```
20. 获取系统的可用内存
```
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available Bytes");
float ramAvailable = ramCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Available Memory : {0} MB", ramAvailable);
```
21. 获取指定进程的网络连接数
```
PerformanceCounter connectionCounter = new PerformanceCounter(".NET CLR Networking", "Number of Current Connections", processName);
float connectionCount = connectionCounter.NextValue();
Console.WriteLine("Connection Count : {0}", connectionCount);
```
22. 获取指定进程的工作时间
```
PerformanceCounter workingTimeCounter = new PerformanceCounter("Process", "Elapsed Time", processName);
float workingTime = workingTimeCounter.NextValue();
Console.WriteLine("Working Time : {0} seconds", workingTime);
```
23. 获取计算机的可用处理器时间
```
PerformanceCounter processorTimeCounter = new PerformanceCounter("System", "Processor Queue Length");
float processorTime = processorTimeCounter.NextValue();
Console.WriteLine("Processor Time : {0}", processorTime);
```
24. 获取指定进程的活动线程数
```
PerformanceCounter activeThreadCounter = new PerformanceCounter(".NET CLR Thread", "Number of Current Logical Threads", processName);
float activeThreadCount = activeThreadCounter.NextValue();
Console.WriteLine("Active Thread Count : {0}", activeThreadCount);
```
25. 获取指定进程的虚拟内存大小
```
PerformanceCounter virtualMemCounter = new PerformanceCounter("Process", "Virtual Bytes", processName);
float virtualMemSize = virtualMemCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Virtual Memory Size : {0} MB", virtualMemSize);
```
26. 获取指定进程的磁盘 I/O 速度
```
PerformanceCounter diskIoReadCounter = new PerformanceCounter("Process", "IO Read Bytes/sec", processName);
PerformanceCounter diskIoWriteCounter = new PerformanceCounter("Process", "IO Write Bytes/sec", processName);
float diskIoReadSpeed = diskIoReadCounter.NextValue() / 1024f / 1024f;
float diskIoWriteSpeed = diskIoWriteCounter.NextValue() / 1024f / 1024f;
Thread.Sleep(1000);
diskIoReadSpeed = diskIoReadCounter.NextValue() / 1024f / 1024f;
diskIoWriteSpeed = diskIoWriteCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Disk IO Read Speed : {0} MB/s", diskIoReadSpeed);
Console.WriteLine("Disk IO Write Speed : {0} MB/s", diskIoWriteSpeed);
```
27. 获取指定进程的句柄使用情况
```
PerformanceCounter handleCountCounter = new PerformanceCounter("Process", "Handle Count", processName);
float handleCount = handleCountCounter.NextValue();
Console.WriteLine("Handle Count : {0}", handleCount);
```
28. 获取指定网卡的传输错误数
```
PerformanceCounter transmissionErrorCounter = new PerformanceCounter("Network Interface", "Output Queue Length", networkInterface);
float transmissionErrors = transmissionErrorCounter.NextValue();
Console.WriteLine("Transmission Errors : {0}", transmissionErrors);
```
29. 获取指定进程的 GDI 对象数
```
PerformanceCounter gdiObjectCounter = new PerformanceCounter("Process", "GDI Objects", processName);
float gdiObjectCount = gdiObjectCounter.NextValue();
Console.WriteLine("GDI Object Count : {0}", gdiObjectCount);
```
30. 获取指定进程的页面文件使用率
```
PerformanceCounter pageFileUsageCounter = new PerformanceCounter("Process", "Page File Bytes", processName);
float pageFileUsage = pageFileUsageCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Page File Usage : {0} MB", pageFileUsage);
```
31. 获取计算机的上下文切换数
```
PerformanceCounter contextSwitchCounter = new PerformanceCounter("System", "Context Switches/sec");
float contextSwitches = contextSwitchCounter.NextValue();
Console.WriteLine("Context Switches : {0}", contextSwitches);
```
32. 获取指定进程的私有工作集大小
```
PerformanceCounter privateWorkingSetCounter = new PerformanceCounter("Process", "Working Set - Private", processName);
float privateWorkingSetSize = privateWorkingSetCounter.NextValue() / 1024f / 1024f;
Console.WriteLine("Private Working Set Size : {0} MB", privateWorkingSetSize);
```
33. 获取指定进程的 GDI 对象使用率
```
PerformanceCounter gdiObjectUsageCounter = new PerformanceCounter("Process", "GDI Objects Peak", processName);
float gdiObjectUsage = gdiObjectUsageCounter.NextValue();
Console.WriteLine("GDI Object Usage : {0}", gdiObjectUsage);
```
34. 获取指定进程的用户对象数
```
PerformanceCounter userObjectCounter = new PerformanceCounter("Process", "User Objects", processName);
float userObjectCount = userObjectCounter.NextValue();
Console.WriteLine("User Object Count : {0}", userObjectCount);
```
35. 获取计算机的每秒中断数
```
PerformanceCounter interruptsPerSecondCounter = new PerformanceCounter("Processor", "Interrupts/sec", "_Total");
float interruptsPerSecond = interruptsPerSecondCounter.NextValue();
Console.WriteLine("Interrupts per Second : {0}", interruptsPerSecond);
```
36. 获取指定进程的用户对象使用率
```
PerformanceCounter userObjectUsageCounter = new PerformanceCounter("Process", "User Objects Peak", processName);
float userObjectUsage = userObjectUsageCounter.NextValue();
Console.WriteLine("User Object Usage : {0}", userObjectUsage);
```
37. 获取指定进程的 CPU 时间
```
PerformanceCounter cpuTime 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.37seo.cn/
发表评论 取消回复