C#——性能计数器

栏目: C# · 发布时间: 6年前

内容简介:C#——性能计数器

打开Windows性能监视器的步骤如下:

开始→运行→perfmon→确定

C#——性能计数器

C#——性能计数器

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

C#——性能计数器

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

C#——性能计数器

瞬间感觉到在微软怀抱下的孩纸好幸福有木有。好啦接下来我们来看看C#是如何调用它的,并且是如何自定义自己的计数器的呢?

C#如何调用本地主机Windows性能监视器获取数据

上代码:

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);
            }
        }
    }
}

结果如下:

C#——性能计数器

代码很简单,最主要的就是一个PerformanceCounter类的实例cpu,PerformanceCounter类有5个构造函数重载,就代码中的构造函数讲述,构造函数为new PerformanceCounter(“计数器类型名称”,“计数器名称”,“计数器实例名称”),(如果该计数器为单实例,那么计数器实例名称可为“”)。可使用实例的NextValue()方法获取当前值。

呵呵,当你看到代码时会说“如此 so easy”,但你可能对“计数器类型名称”,“计数器名称”,“计数器实例名称”这三个名称有点糊涂啦吧,别着急,先看一张图:

C#——性能计数器

对这张图不陌生吧,没错,添加可用内存计数器时见过,那么“1”就是“计数器类型名称”,“2”就是“计数器名称”,“3”就是“计数器实例名称”(应为该计数器是单实例的,所以“3”下面没有具体的实例),所以三者的关系我们可以归纳为:计数器类型》计数器》计数器实例。赶紧试一下吧小伙伴们。。。

C#如何调用远程主机Windows性能监视器获取数据

上代码

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性能监视器中找到并且实时查看队列积压情况啦。(开始动手吧)

先上代码:

(代码就语无伦次吧,神马都不讲究啦)

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();
            }
             
        }
    }
}

我们先在性能监视器中找到我们的计数器如下:

C#——性能计数器

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

C#——性能计数器

这个代码可以检测msmq里消息的数量:

using System.Diagnostics;

PerformanceCounter objCounter = new PerformanceCounter("MSMQ Queue", "Messages in Queue", @"mymachine\private$\MyQueue");

int count = (int)(objCounter.NextValue());

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Designing with Web Standards (2nd Edition)

Designing with Web Standards (2nd Edition)

Jeffrey Zeldman / Peachpit Press / 2006-07-06 / USD 44.99

Best-selling author, designer, and web standards evangelist Jeffrey Zeldman has updated his classic, industry-shaking guidebook. This new edition--now in full color--covers improvements in best prac......一起来看看 《Designing with Web Standards (2nd Edition)》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具