PerformanceCounter 基本介绍以及示例方法

摘要:
CounterName性能计数器的名称。InstanceName性能计数器类别实例的名称,或空字符串(“”)。

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

免责声明:文章转载自《PerformanceCounter 基本介绍以及示例方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vim相关命令单独记载echarts 饼图中间添加图片下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Scala入门系列(十二):隐式转换

引言 Scala提供的隐式转换和隐式参数功能,是非常有特色的功能。是Java等编程语言所没有的功能。它可以允许你手动指定,将某种类型的对象转换成其他类型的对象。通过这些功能可以实现非常强大而且特殊的功能。 Scala的隐式转换,其实最核心的就是定义隐式转换函数,即implicit conversion function。定义的隐式转换函数,只要在编写的...

AESTest

usingGaea.MySql; usingSystem; usingSystem.Data; usingSystem.IO; usingSystem.Security.Cryptography; usingMicrosoft.Extensions.DependencyInjection; usingSystem.Text; usingSys...

Asp.net 面向接口可扩展框架之数据处理模块及EntityFramework扩展和Dapper扩展(含干货)

接口数据处理模块是什么意思呢?实际上很简单,就是使用面向接口的思想和方式来做数据处理。 还提到EntityFramework和Dapper,EntityFramework和Dapper是.net环境下推崇最高的两种ORM工具。 1、EntityFramework是出自微软根正苗红的.net下的ORM工具,直接在Vs工具和Mvc框架中集成了,默认生成的项目就...

RestTemplate 发送Post 多个参数请求

        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>(); requestEntity.add("clientFlag", clientFlag); requ...

说说接口封装

今天给同事封装了一个接口,说起接口封装的事情,其实其实很有的聊。很多时候,说一个服务好,一个服务烂,实际上都是在吐槽服务队外暴露的接口好坏。不管什么语言,封装接口,抽象起来,就是由一个函数名,若干个参数,若干个返回值组成的。封装的好坏,就在这几个上面。 函数名 首先是函数名。函数名的好坏很明显,我的观点,是否简单,不重复。比如在一个User类中你封装一个...

php Redis函数使用总结(string,hash,list, set , sort set )

  对于:string, set , sort set , hash 的增,改操作,是同一个命令,但是把它当改操作时,及时成功返回值依旧为0 对于:list结构来说,增删改查自有一套方法。   1 <?php 2 /*1.Connection*/ 3 $redis = new Redis(); 4 $redis-...