zoukankan      html  css  js  c++  java
  • 如何创建自定义性能计数器

    为什么要创建自定义的性能计数器?

    You create categories and custom performance counters if you want to track data that is not captured by the standard counters provided with Microsoft Windows. For example, you might create a custom counter to track the total number of users logged on to your Web site, or to track the orders processed per second by your site. When you create a counter, you add it to a performance counter category, and you assign it a type that governs how it will behave.

    参考:

    http://msdn.microsoft.com/zh-cn/library/ty9fywea(v=VS.71).aspx

    创建自定义的性能计数器的方法:

    Creating a Single Performance Counter Using PerformanceCounterCategory

     

    1. Create a new text file named CreateCounter.cs and add the following code.

    // CreateCounter.cs

    using System;

    using System.Diagnostics;

    public class CustomCounter

    {

    public static void Main()

    {

    Console.WriteLine("Creating custom counter");

    CreateCounter();

    Console.WriteLine("Done");

    Console.ReadLine();

    }

    public static void CreateCounter()

    {

    if (!PerformanceCounterCategory.Exists("MySingleCategory"))

    {

    PerformanceCounterCategory.Create ("MySingleCategory",

    "My New Perf Category Description", "MyCounter",

    "My New Perf Counter Desc");

    }

    else

    {

    Console.WriteLine("Counter already exists");

    }

    }

    }

    2. Compile the code using the following command line.

    csc.exe /out:CreateCounter.exe /t:exe /r:system.dll CreateCounter.cs

    3. Run CreateCounter.exe from a command prompt to create your new performance

    counter.

    CreateCounter.exe

    Validating Your Performance Counter Category and Performance Counter

    Use Regedt32.exe to verify that your performance counter category and your custom performance counter are created in the following registry folder.

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

    The performance counter category is named MySingleCategory, and the performance counter is named MyCounter.

     

    Using Server Explorer

    If you have Visual Studio .NET, you can create a custom performance counter manually by using Server Explorer.

    _ To create a custom performance counter with Server Explorer

    1. Open Server Explorer and connect to your local server.

    2. Right-click Performance Counters and select Create New Category.

    3. Enter MyCategory in the category name box and optionally enter a description in the Performance Counter Builder box.

    4. Click New at the bottom of the form and enter MyCounter in the counter name box.

    5. Click OK.

     

    Calling PerformanceCounter.Increment

    The following ASP.NET page shows you how to use your custom counter. This code calls PerformanceCounter.Increment to set the new counter value.

    IncrementCounter.aspx

    <%@ language=C# %>

    <%@ import namespace="System.Diagnostics" %>

    <script runat=server>

    void IncrementCounter(Object sender, EventArgs e)

    {

    // get an instance of our perf counter

    PerformanceCounter counter = new PerformanceCounter();

    counter.CategoryName = "mySingleCategory";

    counter.CounterName = "myCounter";

    counter.ReadOnly = false;

    // increment and close the perf counter

    counter.Increment();

    counter.Close();

    Response.Write("Counter is incremented");

    }

    </script>

    <form runat=server>

    <input type="submit" id="btnSubmit" Value="Increment Counter"

    OnServerClick="IncrementCounter" runat=server />

    </form>

     

     

    参考:

    1、《Improving .NET Application Performance and Scalability》

    2、http://msdn.microsoft.com/zh-cn/library/cc437982(v=vs.71).aspx

  • 相关阅读:
    HDU2027 统计元音 一点点哈希思想
    湖南工业大学第一届ACM竞赛 数字游戏 字符串处理
    湖南工业大学第一届ACM竞赛 我素故我在 DFS
    HDU3293sort
    HDU2082 找单词 母函数
    HDU1018 Big Number 斯特林公式
    湖南工业大学第一届ACM竞赛 分糖果 位操作
    UVA 357 Let Me Count The Ways
    UVA 147 Dollars
    UVA 348 Optimal Array Multiplication Sequence
  • 原文地址:https://www.cnblogs.com/preftest/p/1933928.html
Copyright © 2011-2022 走看看