简单的创建一个性能计数器
一、性能监控的作用
网站设计、成都网站制作介绍好的网站是理念、设计和技术的结合。创新互联拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。
性能监控可以用于获取关于应用程序的正常行为的一般消息,性能监控是一个强大的工具,有助于理解系统的工作负载,观察变化和趋势,尤其是运行在服务器上的应用程序
二、性能监控类(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> _PerformanceCounterNames; //-----性能计数器(组件) //记录按钮点击的次数 private PerformanceCounter buttonClickCount; //记录按钮每秒点击的次数 private PerformanceCounter buttonClickCountSec; //存放按钮每秒点击的次数的变量 private int Record_buttonClickCount = 0; //初始化性能计数器名称 private void InitialziePerformanceCounterNames() { _PerformanceCounterNames = new SortedList >(); _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(); } /// /// 注册性能计数器 /// /// /// private void button1_Click(object sender, EventArgs e) { RegisterPerformanceCounter(); } ////// 点击测试 /// /// /// private void button2_Click(object sender, EventArgs e) { buttonClickCount.Increment(); Record_buttonClickCount++;//用于每秒点击的次数 } } }
3,使用性能监视器查看
当前题目:简单的创建一个性能计数器
URL链接:http://scyanting.com/article/jijgdc.html