十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
一、性能監(jiān)控的作用
網(wǎng)站設計、成都網(wǎng)站制作介紹好的網(wǎng)站是理念、設計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設計理念、多方位的設計風格、經(jīng)驗豐富的設計團隊。提供PC端+手機端網(wǎng)站建設,用營銷思維進行網(wǎng)站設計、采用先進技術(shù)開源代碼、注重用戶體驗與SEO基礎,將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。
性能監(jiān)控可以用于獲取關于應用程序的正常行為的一般消息,性能監(jiān)控是一個強大的工具,有助于理解系統(tǒng)的工作負載,觀察變化和趨勢,尤其是運行在服務器上的應用程序
二、性能監(jiān)控類(System.Diagnostics):
PerformanceCounter類:監(jiān)控計數(shù)與寫入計數(shù)。還可以使用這個類創(chuàng)建新的性能類別
PerformanceCounterCategory類:可以查看所有的已有的類別,以及創(chuàng)建類別??梢砸跃幊痰姆绞将@得一個類別中的所有計數(shù)器
performanceCounterInstall類:用于安裝性能計數(shù)器
需要引用WindowsBase
三、創(chuàng)建性能類別(兩種創(chuàng)建方式):
1,圖形化創(chuàng)建:
2,代碼創(chuàng)建+操作(可以監(jiān)控不同應用程序的性能計數(shù)):
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 { //性能計數(shù)器的實例名稱 private const string _NewPerformanceCounterName = "PerformanceCountTest2"; //性能計數(shù)器的類型名稱 private const string _PerformanceCounterCategoryName = "MyZhangDi"; //性能計數(shù)器名稱 private SortedList> _PerformanceCounterNames; //-----性能計數(shù)器(組件) //記錄按鈕點擊的次數(shù) private PerformanceCounter buttonClickCount; //記錄按鈕每秒點擊的次數(shù) private PerformanceCounter buttonClickCountSec; //存放按鈕每秒點擊的次數(shù)的變量 private int Record_buttonClickCount = 0; //初始化性能計數(shù)器名稱 private void InitialziePerformanceCounterNames() { _PerformanceCounterNames = new SortedList >(); _PerformanceCounterNames.Add("buttonClickCount", Tuple.Create("buttonClickCount", "記錄按鈕點擊的次數(shù)")); _PerformanceCounterNames.Add("buttonClickCountSec", Tuple.Create("buttonClickCountSec", "記錄按鈕每秒點擊的次數(shù)")); } //初始化性能計數(shù)器(組件) private void InitializePerformanceCounter() { buttonClickCount = new PerformanceCounter { CategoryName = _PerformanceCounterCategoryName,//性能計數(shù)器類型名稱 CounterName = _PerformanceCounterNames["buttonClickCount"].Item1,//性能計數(shù)器名稱 MachineName = ".",//本地計算機 InstanceLifetime = PerformanceCounterInstanceLifetime.Process,//生命周期 ReadOnly = false,//可寫 InstanceName = _NewPerformanceCounterName//實例名稱 }; buttonClickCountSec = new PerformanceCounter { CategoryName = _PerformanceCounterCategoryName,//性能計數(shù)器類型名稱 CounterName = _PerformanceCounterNames["buttonClickCountSec"].Item1,//性能計數(shù)器名稱 MachineName = ".",//本地計算機 InstanceLifetime = PerformanceCounterInstanceLifetime.Process,//生命周期 ReadOnly = false,//可寫 InstanceName = _NewPerformanceCounterName//實例名稱 }; } //注冊性能計數(shù)器 private void RegisterPerformanceCounter() { //判斷此計數(shù)器類型名稱是否存在 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); //創(chuàng)建類型 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 { //存放按鈕每秒點擊的次數(shù)的變量 賦值給 每秒點擊的次數(shù) buttonClickCountSec.RawValue = this.Record_buttonClickCount; //初始化值(存放按鈕每秒點擊的次數(shù)的變量) this.Record_buttonClickCount = 0; }, Dispatcher.CurrentDispatcher ); //開啟時間計數(shù)器 timer.Start(); } /// /// 注冊性能計數(shù)器 /// /// /// private void button1_Click(object sender, EventArgs e) { RegisterPerformanceCounter(); } ////// 點擊測試 /// /// /// private void button2_Click(object sender, EventArgs e) { buttonClickCount.Increment(); Record_buttonClickCount++;//用于每秒點擊的次數(shù) } } }
3,使用性能監(jiān)視器查看