一、性能监控的作用

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

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

转载于:https://blog.51cto.com/962410314/1605962

简单的创建一个性能计数器相关推荐

  1. 怎样简单的创建一个vector类?(干货 ! ! !详细 ! ! ! ! !)

    Vector接口介绍1 Member functions(创建) 1.建立空vector 2.建立内置类型vector 3.建立自定义类型vector 4.数组方式建立(赋值) 5.迭代器方式建立 M ...

  2. yy神曲url解析php_使用PHP来简单的创建一个RPC服务

    RPC全称为Remote Procedure Call,翻译过来为"远程过程调用".主要应用于不同的系统之间的远程通信和相互调用. 比如有两个系统,一个是PHP写的,一个是JAVA ...

  3. php fwrite服务器上写不进去_使用PHP来简单的创建一个RPC服务

    RPC全称为Remote Procedure Call,翻译过来为"远程过程调用".主要应用于不同的系统之间的远程通信和相互调用. 比如有两个系统,一个是PHP写的,一个是JAVA ...

  4. 如何简单的创建一个多人在线聊天室

    学习目标: 在本教程中,我们将要使用PHP和jQuery创建一个简单的在线聊天工具. 这种实用性的模块对于你想要有实时在线客户支持系统的网站可以说是完美. 废话不多说直接开始. 步骤1:HTML的代码 ...

  5. python如何创建一个类_python (知识点:类)简单的创建一个类

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Mon Nov 14 01:01:29 2016 ...

  6. php创建一个类,JavaScript_创建一个类Person的简单实例,创建一个类Person,包含以下属 - phpStudy...

    创建一个类Person的简单实例 创建一个类Person,包含以下属性:姓名(name).年龄(age).朋友(friends数组).问候(sayhi方法,输出问候语,例如:"你好!&quo ...

  7. C#游戏编程之创建一个简单的卷轴射击游戏

    前几天很多朋友要求贴一篇有关C#游戏开发的实例.本篇文章是创建一个简单的卷轴射击游戏开发实例,内容比较完整,分享给大家,不过篇幅有些长,可以慢慢看哈!本文除CSDN博客外,任何媒体和出版物禁止转载和使 ...

  8. python写rest服务_Python 如何创建一个简单的REST接口

    问题 你想使用一个简单的REST接口通过网络远程控制或访问你的应用程序,但是你又不想自己去安装一个完整的web框架. 解决方案 构建一个REST风格的接口最简单的方法是创建一个基于WSGI标准(PEP ...

  9. Python | Socket01 - 创建一个TCP服务器(阻塞+单线程),将TCP客户端发过来的字符串原路返回

    一.前言 使用Python可以很简单地创建一个TCP服务器程序,该TCP服务器的程序目的是完成这个功能:TCP客户端发送一段字符串给TCP服务端,TCP服务端将字符串原路返回去. 我是从该B站视频教程 ...

最新文章

  1. python popen阻塞_对Python subprocess.Popen子进程管道阻塞详解
  2. AI Studio 不同环境下的执行速度
  3. python零基础实例-python零基础练手项目100+
  4. BZOJ 1444 [JSOI2009]有趣的游戏 (AC自动机、概率与期望DP、矩阵乘法)
  5. linux 产生0~2之间的随机数
  6. 如何在Java中将数组转换为列表
  7. 793. 阶乘函数后K个零 golang
  8. mysql cursor 嵌套_MYSQL 存储过程多游标嵌套遍历
  9. Editplus激活码
  10. 利用python如何进行数据挖掘
  11. 如何学习linux系统
  12. 计算机一级考试选择题知识点,计算机一级选择题必背知识点 考试题型有哪些...
  13. ipsec VPN 技术介绍(基础篇一)
  14. 第二篇学会感谢身边的所有人!
  15. PHP合并多个PDF文件
  16. C - Super Mario(分块+二分)
  17. Unity脚本(一)
  18. 计算机网络隧道工程,隧道工程
  19. CMD目录操作——del【删除普通文件】和rd命令【删除非空文件夹】
  20. Java用递归和遍历实现斐波那契数列

热门文章

  1. Windows7下Docker的安装
  2. 279. 完全平方数 golang
  3. redis源码剖析(十三)—— dump.rdb文件分析
  4. Effective C++学习第十天
  5. C++ STL容器之map 简单使用
  6. django和mysql写注册_Django电商项目---完成注册页面和用户登录
  7. LeetCode【11--盛水最多的容器】LeetCode【12 -- 整数转罗马数字】
  8. HDU5977-Garden of Eden-树分治+FWT
  9. CodeForces - 641ELittle Artem and Time Machine——map+树状数组
  10. 1095 解码PAT准考证 (25 分)