一、性能监控的作用
性能监控可以用于获取关于应用程序的正常行为的一般消息,性能监控是一个强大的工具,有助于理解系统的工作负载,观察变化和趋势,尤其是运行在服务器上的应用程序
二、性能监控类(System.Diagnostics):
PerformanceCounter类:监控计数与写入计数。还可以使用这个类创建新的性能类别
PerformanceCounterCategory类:可以查看所有的已有的类别,以及创建类别。可以以编程的方式获得一个类别中的所有计数器
performanceCounterInstall类:用于安装性能计数器
三、创建性能类别(两种创建方式):
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,使用性能监视器查看
本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1605962
原文地址:http://962410314.blog.51cto.com/7563109/1605962