Performance Counter的使用

原文地址:http://blog.csdn.net/jiangxinyu/article/details/5480401 html

一 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 獲取性能計數器類別下的實例的名稱實例下的性能計數器的名稱post

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

 

 

 

 

 

Performance Counter的使用this

 

客戶端性能測試經過performanceCounter監控客戶端性能指標.net

     PerformanceCounter PTCounter = new PerformanceCounter("Process",
                                        "% Processor Time",
                                         "AliIM");
            logfile("% Processor Time:" + PTCounter.NextValue().ToString());
            //內存
            PerformanceCounter WSCounter = new PerformanceCounter("Process",
                                                                    "Working Set",
                                                                     "AliIM");
            logfile("Working Set:" + ((double)WSCounter.NextValue() / 1024).ToString());線程

            //內存最高值
            PerformanceCounter MemeryCounter = new PerformanceCounter("Process",
                                                                    "Working Set Peak",
                                                                     "AliIM");
            logfile("Working Set Peak:" + ((double)MemeryCounter.NextValue() / 1024).ToString());


            //虛擬內存
            PerformanceCounter PBCounter = new PerformanceCounter("Process",
                                                              "Private Bytes",
                                                               "AliIM");
            logfile("Private Bytes:" + ((double)PBCounter.NextValue() / 1024).ToString());

            //句柄數            PerformanceCounter HCCounter = new PerformanceCounter("Process",                                    

相關文章
相關標籤/搜索