簡單的建立一個性能計數器
阿新 • • 發佈:2020-10-09
一、效能監控的作用
效能監控可以用於獲取關於應用程式的正常行為的一般訊息,效能監控是一個強大的工具,有助於理解系統的工作負載,觀察變化和趨勢,尤其是執行在伺服器上的應用程式
二、效能監控類(System.Diagnostics):
PerformanceCounter類:監控計數與寫入計數。還可以使用這個類建立新的效能類別
PerformanceCounterCategory類:可以檢視所有的已有的類別,以及建立類別。可以以程式設計的方式獲得一個類別中的所有計數器
performanceCounterInstall類:用於安裝效能計數器
需要引用WindowsBase
三、建立效能類別(兩種建立方式):
1,圖形化建立:
2,程式碼建立+操作(可以監控不同應用程式的效能計數):
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; usingSystem.Windows.Threading; usingSystem.Diagnostics; namespacePerformanceCountTest2 { publicpartialclassForm1:Form { //效能計數器的例項名稱 privateconststring_NewPerformanceCounterName="PerformanceCountTest2"; //效能計數器的型別名稱 privateconststring_PerformanceCounterCategoryName="MyZhangDi"; //效能計數器名稱 privateSortedList<string,Tuple<string,string>>_PerformanceCounterNames; //-----效能計數器(元件) //記錄按鈕點選的次數 privatePerformanceCounterbuttonClickCount; //記錄按鈕每秒點選的次數 privatePerformanceCounterbuttonClickCountSec; //存放按鈕每秒點選的次數的變數 privateintRecord_buttonClickCount=0; //初始化效能計數器名稱 privatevoidInitialziePerformanceCounterNames() { _PerformanceCounterNames=newSortedList<string,Tuple<string,string>>(); _PerformanceCounterNames.Add("buttonClickCount",Tuple.Create("buttonClickCount","記錄按鈕點選的次數")); _PerformanceCounterNames.Add("buttonClickCountSec",Tuple.Create("buttonClickCountSec","記錄按鈕每秒點選的次數")); } //初始化效能計數器(元件) privatevoidInitializePerformanceCounter() { buttonClickCount=newPerformanceCounter { CategoryName=_PerformanceCounterCategoryName,//效能計數器型別名稱 CounterName=_PerformanceCounterNames["buttonClickCount"].Item1,//效能計數器名稱 MachineName=".",//本地計算機 InstanceLifetime=PerformanceCounterInstanceLifetime.Process,//生命週期 ReadOnly=false,//可寫 InstanceName=_NewPerformanceCounterName//例項名稱 }; buttonClickCountSec=newPerformanceCounter { CategoryName=_PerformanceCounterCategoryName,//效能計數器型別名稱 CounterName=_PerformanceCounterNames["buttonClickCountSec"].Item1,//效能計數器名稱 MachineName=".",//本地計算機 InstanceLifetime=PerformanceCounterInstanceLifetime.Process,//生命週期 ReadOnly=false,//可寫 InstanceName=_NewPerformanceCounterName//例項名稱 }; } //註冊效能計數器 privatevoidRegisterPerformanceCounter() { //判斷此計數器型別名稱是否存在 if(!PerformanceCounterCategory.Exists(_PerformanceCounterCategoryName)) { varCounterCreationDatas=newCounterCreationData[2]; CounterCreationDatas[0]=newCounterCreationData { CounterName=_PerformanceCounterNames["buttonClickCount"].Item1, CounterHelp=_PerformanceCounterNames["buttonClickCount"].Item2, CounterType=PerformanceCounterType.NumberOfItems32 }; CounterCreationDatas[1]=newCounterCreationData { CounterName=_PerformanceCounterNames["buttonClickCountSec"].Item1, CounterHelp=_PerformanceCounterNames["buttonClickCountSec"].Item2, CounterType=PerformanceCounterType.RateOfCountsPerSecond32 }; CounterCreationDataCollectioncounts=newCounterCreationDataCollection(CounterCreationDatas); //建立型別 PerformanceCounterCategory.Create(_PerformanceCounterCategoryName,"型別描述",PerformanceCounterCategoryType.MultiInstance,counts); } } publicForm1() { InitializeComponent(); } privatevoidForm1_Load(objectsender,EventArgse) { InitialziePerformanceCounterNames(); InitializePerformanceCounter(); DispatcherTimertimer=newDispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Background, delegate { //存放按鈕每秒點選的次數的變數賦值給每秒點選的次數 buttonClickCountSec.RawValue=this.Record_buttonClickCount; //初始化值(存放按鈕每秒點選的次數的變數) this.Record_buttonClickCount=0; }, Dispatcher.CurrentDispatcher ); //開啟時間計數器 timer.Start(); } ///<summary> ///註冊效能計數器 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbutton1_Click(objectsender,EventArgse) { RegisterPerformanceCounter(); } ///<summary> ///點選測試 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbutton2_Click(objectsender,EventArgse) { buttonClickCount.Increment(); Record_buttonClickCount++;//用於每秒點選的次數 } } }
3,使用效能監視器檢視
轉載於:https://blog.51cto.com/962410314/1605962