简单的创建一个性能计数器-创新互联
一、性能监控的作用

性能监控可以用于获取关于应用程序的正常行为的一般消息,性能监控是一个强大的工具,有助于理解系统的工作负载,观察变化和趋势,尤其是运行在服务器上的应用程序
二、性能监控类(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,使用性能监视器查看

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享题目:简单的创建一个性能计数器-创新互联
本文网址:http://www.jxjierui.cn/article/ddgoei.html


咨询
建站咨询
