一、性能監(jiān)控的作用
網(wǎng)站設(shè)計、成都網(wǎng)站制作介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風(fēng)格、經(jīng)驗豐富的設(shè)計團(tuán)隊。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營銷思維進(jìn)行網(wǎng)站設(shè)計、采用先進(jìn)技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。
性能監(jiān)控可以用于獲取關(guān)于應(yīng)用程序的正常行為的一般消息,性能監(jiān)控是一個強(qiáng)大的工具,有助于理解系統(tǒng)的工作負(fù)載,觀察變化和趨勢,尤其是運(yùn)行在服務(wù)器上的應(yī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)控不同應(yīng)用程序的性能計數(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ù)器的實(shí)例名稱
private const string _NewPerformanceCounterName = "PerformanceCountTest2";
//性能計數(shù)器的類型名稱
private const string _PerformanceCounterCategoryName = "MyZhangDi";
//性能計數(shù)器名稱
private SortedList<string, Tuple<string, string>> _PerformanceCounterNames;
//-----性能計數(shù)器(組件)
//記錄按鈕點(diǎn)擊的次數(shù)
private PerformanceCounter buttonClickCount;
//記錄按鈕每秒點(diǎn)擊的次數(shù)
private PerformanceCounter buttonClickCountSec;
//存放按鈕每秒點(diǎn)擊的次數(shù)的變量
private int Record_buttonClickCount = 0;
//初始化性能計數(shù)器名稱
private void InitialziePerformanceCounterNames()
{
_PerformanceCounterNames = new SortedList<string, Tuple<string, string>>();
_PerformanceCounterNames.Add("buttonClickCount", Tuple.Create("buttonClickCount", "記錄按鈕點(diǎn)擊的次數(shù)"));
_PerformanceCounterNames.Add("buttonClickCountSec", Tuple.Create("buttonClickCountSec", "記錄按鈕每秒點(diǎn)擊的次數(shù)"));
}
//初始化性能計數(shù)器(組件)
private void InitializePerformanceCounter()
{
buttonClickCount = new PerformanceCounter
{
CategoryName = _PerformanceCounterCategoryName,//性能計數(shù)器類型名稱
CounterName = _PerformanceCounterNames["buttonClickCount"].Item1,//性能計數(shù)器名稱
MachineName = ".",//本地計算機(jī)
InstanceLifetime = PerformanceCounterInstanceLifetime.Process,//生命周期
ReadOnly = false,//可寫
InstanceName = _NewPerformanceCounterName//實(shí)例名稱
};
buttonClickCountSec = new PerformanceCounter
{
CategoryName = _PerformanceCounterCategoryName,//性能計數(shù)器類型名稱
CounterName = _PerformanceCounterNames["buttonClickCountSec"].Item1,//性能計數(shù)器名稱
MachineName = ".",//本地計算機(jī)
InstanceLifetime = PerformanceCounterInstanceLifetime.Process,//生命周期
ReadOnly = false,//可寫
InstanceName = _NewPerformanceCounterName//實(shí)例名稱
};
}
//注冊性能計數(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
{
//存放按鈕每秒點(diǎn)擊的次數(shù)的變量 賦值給 每秒點(diǎn)擊的次數(shù)
buttonClickCountSec.RawValue = this.Record_buttonClickCount;
//初始化值(存放按鈕每秒點(diǎn)擊的次數(shù)的變量)
this.Record_buttonClickCount = 0;
},
Dispatcher.CurrentDispatcher
);
//開啟時間計數(shù)器
timer.Start();
}
/// <summary>
/// 注冊性能計數(shù)器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
RegisterPerformanceCounter();
}
/// <summary>
/// 點(diǎn)擊測試
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
buttonClickCount.Increment();
Record_buttonClickCount++;//用于每秒點(diǎn)擊的次數(shù)
}
}
}
3,使用性能監(jiān)視器查看

本文名稱:簡單的創(chuàng)建一個性能計數(shù)器
網(wǎng)站URL:http://chinadenli.net/article12/jijgdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、微信公眾號、用戶體驗、網(wǎng)站內(nèi)鏈、電子商務(wù)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)