C#——性能计数器
简要Windows性能监视器:
打开Windows性能监视器的步骤如下:
开始→运行→perfmon→确定


在这里我们可以选择添加我们要监控的计数器,比如:cpu使用率、内存使用量等,作为asp.net攻城师我们还可以使用它来监控我们站点的请求队列、应道队列数量、请求总数等。比如我们要开可用内存的信息:

可用内存大小事实数据如下:

瞬间感觉到在微软怀抱下的孩纸好幸福有木有。好啦接下来我们来看看C#是如何调用它的,并且是如何自定义自己的计数器的呢?
C#如何调用本地主机Windows性能监视器获取数据
上代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
using System;using System.Collections.Generic;using System.Text;//应引用此名门空间using System.Diagnostics;using System.Threading;namespace Performance_Demo{ class Class1 { static void Main(string[] arge) { //性能计数器组件类 PerformanceCounter cpu = new PerformanceCounter("Memory", "Available MBytes", ""); while (true) { Console.WriteLine("{0} MB",cpu.NextValue()); Thread.Sleep(1000); } } }} |
结果如下:

代码很简单,最主要的就是一个PerformanceCounter类的实例cpu,PerformanceCounter类有5个构造函数重载,就代码中的构造函数讲述,构造函数为new PerformanceCounter(“计数器类型名称”,“计数器名称”,“计数器实例名称”),(如果该计数器为单实例,那么计数器实例名称可为“”)。可使用实例的NextValue()方法获取当前值。
呵呵,当你看到代码时会说“如此 so easy”,但你可能对“计数器类型名称”,“计数器名称”,“计数器实例名称”这三个名称有点糊涂啦吧,别着急,先看一张图:

对这张图不陌生吧,没错,添加可用内存计数器时见过,那么“1”就是“计数器类型名称”,“2”就是“计数器名称”,“3”就是“计数器实例名称”(应为该计数器是单实例的,所以“3”下面没有具体的实例),所以三者的关系我们可以归纳为:计数器类型》计数器》计数器实例。赶紧试一下吧小伙伴们。。。
C#如何调用远程主机Windows性能监视器获取数据
上代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
class Class3{ //访问远程主机的性能监视器 static void Main(string[] arge) { //先于远程IP建立连接 string tmpstr = "net use \\\\" + "172.16.164.000" + "\\ipc$ " + "密码" + " /user:" + "用户名"; RunDOS(tmpstr); //性能计数器组件类 PerformanceCounter cpu = new PerformanceCounter("Memory", "Available MBytes", "", "172.16.164.215"); while (true) { Console.WriteLine("{0} MB", cpu.NextValue()); Thread.Sleep(1000); } } //使用DOS运行命令 static void RunDOS(string DOS) { Process p = null; p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); string strOutput = ""; p.StandardInput.WriteLine(DOS); p.StandardInput.WriteLine("exit"); while (p.StandardOutput.EndOfStream) { strOutput = p.StandardOutput.ReadLine(); } p.WaitForExit(); p.Close(); }} |
代码也很简单,与调用本地主机不同之处就是多了一段运行DOS命令的代码,目的就是先与远程主机建立连接,需要指定远程主机IP、用户名、密码(可以为一般管理员身份),此时需要注意的是远程主机上的“Remote Registry”服务应处于启动状态,目的是为了能让远程用户能修改此计算机上的注册表。
C#如何自定义计数器
前面我们学习如何使用C#调用Windows性能监视器,来获取系统的各个计数器实时数据,那么我们可不可以自己定义一个计数器,并且添加到性能监视器中供我们实时查看呢?答案是肯定的。试想一下我们在日常开发当中会有类似这样的需求:我们有一个队列(可能是各种命令啊或者是消息啊、订单啊等等),那么我们想有一个可视化的东西来监控一下这个队列中积压了多少内容,当然啦我们万能的攻城师们肯定希望积压数量永远是0啦,哈哈,此时我们就可以为我们的队列设计一个计数器,那么我们就可以在Windows性能监视器中找到并且实时查看队列积压情况啦。(开始动手吧)
先上代码:
(代码就语无伦次吧,神马都不讲究啦)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Collections;namespace Performance_Demo{ class Class2 { //我的队列 static Queue<int> myQueue = new Queue<int>(); //计数器实例 static PerformanceCounter counter1 = null; static void Main(string[] arge) { //计数器类型名称 string CategoryName = "a_yigebeiyiwangdebaozi"; //计数器名称 string CounterName = "a_yigebeiyiwangdebaozi_counter1"; if (PerformanceCounterCategory.Exists(CategoryName)) { PerformanceCounterCategory.Delete(CategoryName); } CounterCreationDataCollection ccdc = new CounterCreationDataCollection(); //计数器 CounterCreationData ccd = new CounterCreationData(CounterName, "myCounter", PerformanceCounterType.NumberOfItems64); ccdc.Add(ccd); //使用PerformanceCounterCategory.Create创建一个计数器类别 PerformanceCounterCategory.Create(CategoryName, "", PerformanceCounterCategoryType.MultiInstance, ccdc); //初始化计数器实例 counter1 = new PerformanceCounter(); counter1.CategoryName = CategoryName; counter1.CounterName = CounterName; counter1.InstanceName = "myCounter1"; counter1.InstanceLifetime = PerformanceCounterInstanceLifetime.Process; counter1.ReadOnly = false; counter1.RawValue = 0; while (true) { EnqueueQueue(); System.Threading.Thread.Sleep(1000); DequeueQueue(); } } static void EnqueueQueue() { int C = new Random().Next(10); for (int i = 0; i < C; i++) { myQueue.Enqueue(i); //计数器加一 counter1.Increment(); } } static void DequeueQueue() { int C = new Random().Next(20); for (int i = 0; i < C; i++) { if (myQueue.Count == 0) break; myQueue.Dequeue(); //计数器减一 counter1.Decrement(); } } }} |
我们先在性能监视器中找到我们的计数器如下:

我们队列的实时数据如下:

这个代码可以检测msmq里消息的数量:
using System.Diagnostics;
PerformanceCounter objCounter = new PerformanceCounter("MSMQ Queue", "Messages in Queue", @"mymachine\private$\MyQueue");
int count = (int)(objCounter.NextValue());
C#——性能计数器的更多相关文章
- C# 利用性能计数器监控网络状态
本例是利用C#中的性能计数器(PerformanceCounter)监控网络的状态.并能够直观的展现出来 涉及到的知识点: PerformanceCounter,表示 Windows NT 性能计数器 ...
- 使用PowerShell收集多台服务器的性能计数器
写在前面 当管理多台Windows Server服务器时(无论是DB.AD.WEB以及其他的应用服务器),当出现性能或其他问题后,参阅性能计数器都是一个非常好的维度从而推测出问题可能出现的原因 ...
- SQL Server性能计数器部署(批量)
一.计数器部署项目介绍 SQL Server每个服务器,日常需要监控的计数器指标高达上百,若一个个手动添加非常麻烦.此项目通过命令行工具针对指定计数器集成部署,提高部署效率.此包括开发数据库互联(OD ...
- SQL Server性能计数器收集汇总方案(Reporting Service)
通过收集计数器信息,并将计数器信息汇总为不同粒度存储,以Reporting Service报表服务器显示.以下是计数器收集汇总的基本架构. 笔者需要收集的SQL Server计数器包括:SQL Ser ...
- 性能计数器与profiler的组合性能诊断
性能计数器和sql profiler都是常用的性能诊断工具和优化工具,最近和群友聊天发现很多人竟然不知道这两个可以“组合”使用,所以这篇算是一篇扫盲贴吧. 两种工具简述 通过计数器可以收集两部分内容: ...
- Buffer cache hit ratio性能计数器真的可以作为内存瓶颈的判断指标吗?
Buffer cache hit ratio官方是这么解释的:“指示在缓冲区高速缓存中找到而不需要从磁盘中读取的页的百分比.” Buffer cache hit ratio被很多人当做判断内存的性能指 ...
- SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法
操作步骤: 1. 在 Microsoft Windows 2003 或 Windows XP 桌面上,依次单击"开始"."运行",然后在"打开&quo ...
- 使用WMI和性能计数器监控远程服务器权限设置
应用场景:在web服务器中,通过.NET编码使用WMI查询远程服务器的一些硬件配置信息,使用性能计数器查询远程机器的运行时资源使用情况.在网上没有找到相关的东西,特记录与大家共享. 将web服务器和所 ...
- (转载)Windows常见性能计数器(较好的说明)
转载地址:http://blog.csdn.net/dfbrt56/article/details/3341591 Windows常见性能计数器 性能计数器(counter)是描述服务器或操作系统性能 ...
- sqlserver服务器常用的性能计数器
sqlserver服务器常用的性能计数器,在此标记. 性能对象 计数器 说明 Processor %Processor Time %Privileged Time 建议值:持续低于80 建议值:持续低 ...
随机推荐
- element-UI 下拉条数多渲染慢
本文地址:https://www.cnblogs.com/veinyin/p/10120398.html 如果渲染为普通下拉框,用户难以找到要选择的那一项,增加模糊搜索功能,可解决渲染缓慢问题,但用户 ...
- shell 示例1 从1叠加到100
从1叠加到100 echo $[$(echo +{..})] echo $[(+)*(/)] seq -s |bc
- MongoDB 之 手把手教你增删改查 MongoDB - 2
我们在 MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1 中学习了如果安装部署一个 MongoDB 如果没看到我的金玉良言的话,就重新打开一次客户端和服务端吧 本章我们 ...
- centos7和centos6的区别【转】
最近发的文章,有人咨询我,说为啥不用centos7,而用centos6,这个跟个人习惯和生产环境元素决定的.centos7和6变化的就很大的.我收集了一些区别,给大家看看. 注意:生产环境推荐cent ...
- C++获取文件夹下所有文件名
查找文件需要一个结构体和几个函数.结构体为struct _finddata_t,函数为_findfirst.findnext和_findclose. struct _finddata_t 这个结构体是 ...
- 使用badblocks命令检测、修复硬盘坏道(待验证)
今天我的新硬盘到了.暂时没想好怎么用它.可以把它装入光驱位硬盘架给G430用,也可以把它当成移动硬盘来用. 想起以前记录过一个badblocks的用法,用来检查坏道,这里再贴一遍备用. ####### ...
- OS X 10.11:在exFAT分区的外置硬盘上使用Time Machine。
Time Machine默认需要使用HFS+分区的外置硬盘,但在网络硬盘上也可以使用单个的 .sparsebundle 镜像文件备份.在本地USB或Firewire等接口连接的外置硬盘,只有exFAT ...
- Redis Scan命令
原地址:https://www.cnblogs.com/tekkaman/p/4887293.html [Redis Scan命令] SCAN cursor [MATCH pattern] [COUN ...
- 记一些使用PyQt的问题
本文自用,日常记录,不断更新 环境 1.使用 PyCharm IDE 2.PyQt5 3. 扩展配置 PyUIC转换后的代码处理 PyUIC 用于 将 QtDesigner 生成的 .ui 文件转换为 ...
- P(查准率),R(查全率),F1 值
起源: 我们平时用的精度 accuracy,也就是整体的正确率 acc=predict_right_num/predict_num 这个虽然常用,但不能满足所有任务的需求.比如,因为香蕉太多了,也不能 ...