zoukankan      html  css  js  c++  java
  • 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", @"mymachineprivate$MyQueue");
    
    int count = (int)(objCounter.NextValue());
    

      

  • 相关阅读:
    【原】基础篇:第九篇,Ext组件系列之field组件的基本用法
    为什么要返回byte[]
    Migrate Mysql to SQL Server 2005
    关于编码规范
    RPM删除包的时候报127错误
    邮政储蓄的线上故障
    string.replaceAll与StringUtils.replace
    EJB工作原理
    OJB Connection
    找工作
  • 原文地址:https://www.cnblogs.com/tuyile006/p/6894488.html
Copyright © 2011-2022 走看看