日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

简单的创建一个性能计数器

發布時間:2023/11/29 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单的创建一个性能计数器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、性能監控的作用

????????性能監控可以用于獲取關于應用程序的正常行為的一般消息,性能監控是一個強大的工具,有助于理解系統的工作負載,觀察變化和趨勢,尤其是運行在服務器上的應用程序

?

二、性能監控類(System.Diagnostics):
PerformanceCounter類:監控計數與寫入計數。還可以使用這個類創建新的性能類別
PerformanceCounterCategory類:可以查看所有的已有的類別,以及創建類別。可以以編程的方式獲得一個類別中的所有計數器
performanceCounterInstall類:用于安裝性能計數器

?

需要引用WindowsBase

?

三、創建性能類別(兩種創建方式):

1,圖形化創建:

?

2,代碼創建+操作(可以監控不同應用程序的性能計數):

?using?System; using?System.Collections.Generic; using?System.ComponentModel; using?System.Data; using?System.Drawing; using?System.Linq; using?System.Text; using?System.Threading.Tasks; using?System.Windows.Forms; using?System.Windows.Threading; using?System.Diagnostics; namespace?PerformanceCountTest2 {public?partial?class?Form1?:?Form{//性能計數器的實例名稱private?const?string?_NewPerformanceCounterName?=?"PerformanceCountTest2";//性能計數器的類型名稱private?const?string?_PerformanceCounterCategoryName?=?"MyZhangDi";//性能計數器名稱private?SortedList<string,?Tuple<string,?string>>?_PerformanceCounterNames;//-----性能計數器(組件)//記錄按鈕點擊的次數private?PerformanceCounter?buttonClickCount;//記錄按鈕每秒點擊的次數private?PerformanceCounter?buttonClickCountSec;//存放按鈕每秒點擊的次數的變量private?int?Record_buttonClickCount?=?0;//初始化性能計數器名稱private?void?InitialziePerformanceCounterNames(){_PerformanceCounterNames?=?new?SortedList<string,?Tuple<string,?string>>();_PerformanceCounterNames.Add("buttonClickCount",?Tuple.Create("buttonClickCount",?"記錄按鈕點擊的次數"));_PerformanceCounterNames.Add("buttonClickCountSec",?Tuple.Create("buttonClickCountSec",?"記錄按鈕每秒點擊的次數"));}//初始化性能計數器(組件)private?void?InitializePerformanceCounter(){buttonClickCount?=?new?PerformanceCounter{CategoryName?=?_PerformanceCounterCategoryName,//性能計數器類型名稱CounterName?=?_PerformanceCounterNames["buttonClickCount"].Item1,//性能計數器名稱MachineName?=?".",//本地計算機InstanceLifetime?=?PerformanceCounterInstanceLifetime.Process,//生命周期ReadOnly?=?false,//可寫InstanceName?=?_NewPerformanceCounterName//實例名稱};buttonClickCountSec?=?new?PerformanceCounter{CategoryName?=?_PerformanceCounterCategoryName,//性能計數器類型名稱CounterName?=?_PerformanceCounterNames["buttonClickCountSec"].Item1,//性能計數器名稱MachineName?=?".",//本地計算機InstanceLifetime?=?PerformanceCounterInstanceLifetime.Process,//生命周期ReadOnly?=?false,//可寫InstanceName?=?_NewPerformanceCounterName//實例名稱};}//注冊性能計數器private?void?RegisterPerformanceCounter(){//判斷此計數器類型名稱是否存在if?(!PerformanceCounterCategory.Exists(_PerformanceCounterCategoryName)){var?CounterCreationDatas?=?new?CounterCreationData[2];CounterCreationDatas[0]?=?new?CounterCreationData{CounterName?=?_PerformanceCounterNames["buttonClickCount"].Item1,CounterHelp?=?_PerformanceCounterNames["buttonClickCount"].Item2,CounterType?=?PerformanceCounterType.NumberOfItems32};CounterCreationDatas[1]?=?new?CounterCreationData{CounterName?=?_PerformanceCounterNames["buttonClickCountSec"].Item1,CounterHelp?=?_PerformanceCounterNames["buttonClickCountSec"].Item2,CounterType?=?PerformanceCounterType.RateOfCountsPerSecond32};CounterCreationDataCollection?counts?=?new?CounterCreationDataCollection(CounterCreationDatas);//創建類型PerformanceCounterCategory.Create(_PerformanceCounterCategoryName,?"類型描述",?PerformanceCounterCategoryType.MultiInstance,?counts);}}public?Form1(){InitializeComponent();}private?void?Form1_Load(object?sender,?EventArgs?e){InitialziePerformanceCounterNames();InitializePerformanceCounter();DispatcherTimer?timer?=?new?DispatcherTimer(TimeSpan.FromSeconds(1),DispatcherPriority.Background,delegate{//存放按鈕每秒點擊的次數的變量?賦值給?每秒點擊的次數buttonClickCountSec.RawValue?=?this.Record_buttonClickCount;//初始化值(存放按鈕每秒點擊的次數的變量)this.Record_buttonClickCount?=?0;},Dispatcher.CurrentDispatcher);//開啟時間計數器timer.Start();}///?<summary>///?注冊性能計數器///?</summary>///?<param?name="sender"></param>///?<param?name="e"></param>private?void?button1_Click(object?sender,?EventArgs?e){RegisterPerformanceCounter();}///?<summary>///?點擊測試///?</summary>///?<param?name="sender"></param>///?<param?name="e"></param>private?void?button2_Click(object?sender,?EventArgs?e){buttonClickCount.Increment();Record_buttonClickCount++;//用于每秒點擊的次數}} }

?

3,使用性能監視器查看

?

轉載于:https://blog.51cto.com/962410314/1605962

總結

以上是生活随笔為你收集整理的简单的创建一个性能计数器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。