温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

简单的创建一个性能计数器

发布时间:2020-10-18 18:04:01 来源:网络 阅读:774 作者:1473348968 栏目:编程语言

一、性能监控的作用

        性能监控可以用于获取关于应用程序的正常行为的一般消息,性能监控是一个强大的工具,有助于理解系统的工作负载,观察变化和趋势,尤其是运行在服务器上的应用程序

 

二、性能监控类(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<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,使用性能监视器查看

简单的创建一个性能计数器

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI