zoukankan      html  css  js  c++  java
  • c#实现自定义window performance counter

    下面是一个简单的实现 操作自定义window performance counter的实例。在运行程序的过程中我们通过操作系统的performance面板查看或者写log文件,对我们应用程序的性能进行监视,分析。将有助于我们分析解决系统的性能等问题。

    代码功能: 

    1,实现添加一个counter 类别

    2,添加一个或者多个counter对象

    3,获取counter对象,并赋值。

    在系统的performance 面板中查看。效果如下图:

    代码:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;

    namespace WritePerformanceLog
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    if (!PerformanceCounterCategory.Exists("test_Category_1"))
                {
                    
    // Create a collection of type CounterCreationDataCollection.
                    System.Diagnostics.CounterCreationDataCollection CounterDatas =
                       
    new System.Diagnostics.CounterCreationDataCollection();
                    
    // Create the counters and set their properties.
                    System.Diagnostics.CounterCreationData cdCounter1 =
                       
    new System.Diagnostics.CounterCreationData();
                    System.Diagnostics.CounterCreationData cdCounter2 
    =
                       
    new System.Diagnostics.CounterCreationData();
                    cdCounter1.CounterName 
    = "Counter1";
                    cdCounter1.CounterHelp 
    = "help string1";
                    cdCounter1.CounterType 
    = System.Diagnostics.PerformanceCounterType.NumberOfItems64;
                    cdCounter2.CounterName 
    = "Counter2";
                    cdCounter2.CounterHelp 
    = "help string 2";
                    cdCounter2.CounterType 
    = System.Diagnostics.PerformanceCounterType.NumberOfItems64;

                    
                    
    // Add both counters to the collection.
                    CounterDatas.Add(cdCounter1);
                    CounterDatas.Add(cdCounter2);
                    
    // Create the category and pass the collection to it.
                    System.Diagnostics.PerformanceCounterCategory.Create(
                       
    "test_Category_1""Category help", CounterDatas);
                }
                
    else
                {

                    PerformanceCounter cdCounter1 
    = new PerformanceCounter("test_Category_1""Counter1"false);
                    PerformanceCounter cdCounter2 
    = new PerformanceCounter("test_Category_1""Counter2"false);
                    
                    cdCounter1.ReadOnly 
    = false;

                    
    forint i=0;i<10000;i++ )
                    {
                        cdCounter1.RawValue 
    = i;
                        
    //cdCounter1.Increment();

                        cdCounter2.RawValue 
    = i+1;

                        Thread.Sleep(
    100);
                    }
                                               

                    Console.WriteLine(cdCounter1.NextValue());
                    Console.WriteLine(cdCounter2.RawValue);
                    Console.WriteLine(cdCounter1.NextSample());


                    Console.Read();

                }
            }
        }
    }

  • 相关阅读:
    无尽的冒险
    推荐一款好用的markdown编辑器,还可以引入vue主题
    你是微光
    Echarts 空心饼图示例
    vue + element-ui 制作tab切换(切换vue组件,踩坑总结)
    vue + element-ui 制作tab切换(适用于单页切换不同标记显示不同内容)
    Element UI 封装Table --> 实现动态创建表头和单元格数据(单元格内可动态增加非纯文本的内容)
    Element UI 封装Table --> 实现动态创建表头和单元格数据(无需写死表头和单元格数据)
    vue 部署到生产出现语法错误和css警告(Resource interpreted as Stylesheet but transferred with MIME type text/html: "www.aaa.com/cal/static/css/app.c06.css". vendor.4b6.js:1 Uncaught SyntaxError: Unexpected token '<')
    使用vue-cookies插件操作cookie
  • 原文地址:https://www.cnblogs.com/luyinghuai/p/1272662.html
Copyright © 2011-2022 走看看