Custom Windows performance monitor

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Windows The performance monitor is a very good use of the built-in monitoring tools, for some basic simple monitoring requirement can easily meet. This paper mainly summarizes how some performance data of their application in the exposed to the performance monitor is convenient for management.

What?? Do not know what is the Windows performance monitor?

“Microsoft   management console (MMC);   management unit, provided for system performance analysis tool. Only from a single console, can be real-time monitoring applications and hardware performance, custom to collect in the log data, define the alert and the automatic operation of the threshold, generate reports and past performance data view in various ways. ” - MSDN[1] 

Use scene

Need to record some of the properties of their apps in the aspects of the data, for example I have a   WCF application, I need to record the number of requests per minute processing application, the average processing time request. These data can be used to monitor the application, can also carry out some analysis. If these data will be stored into its own form will bring many other work, if exposed to the performance monitor would be much simpler.

Method of use

Key type:

PerformanceCounterCategory: This corresponds to a group on a monitor, can contain many counter( Counter)

CounterCreationData: The name of the class defined counter and some help string, is mainly used in the creation of the time.

PerformanceCounterType: Each counter is needed to develop a type, the enumeration provides all types of counter. Refer to   MSDN can see the applicable scope of each type and the calculation formula of this type. [3]

PerformanceCounter: This is the real exposure data types, the type we can turn to the statistical data is exposed to the performance monitor.

Step:

Write an example used to record the total number of operations.

First, create a custom type is added to the system:

private const string CategoryName = "Sample Category";

private const string CategoryHelp = "This is just a sample Category";

private const string TotalOperationName = "Total Operation Counter";

private const string TotalOperationHelp = "Count of Total Operation";


//Detection system is already exist for this category // if you need to modify the category of content words you need to delete and re add here is in order to delete if (PerformanceCounterCategory.Exists (CategoryName)) PerformanceCounterCategory.Delete (CategoryName); // create you need to counter CounterCreationDataCollection collection = new CounterCreationDataCollection(); CounterCreationData totalOperation = new CounterCreationData(TotalOperationName, TotalOperationHelp, PerformanceCounterType.NumberOfItems32); collection.Add(totalOperation); //Call the Create method on your local computer to register this counter categories and some counter PerformanceCounterCategory.Create the(CategoryName, CategoryHelp, PerformanceCounterCategoryType.SingleInstance, Collection);

Points for attention:

1 it is strongly recommended to create counter categories in the application installation or deployment, rather than at run time to add. Because if added at run time if possible performance monitor to refresh, leading to an error. [2]

2 create a counter categories require administrator privileges, which is another way of saying that it is best to create when installing or deployment, because most likely is running without administrator privileges.

3 select the PerformanceCounterType best go to MSDN to find a most suitable for your needs the type. [3]

Two, create a PerformanceCounter runtime

PerformanceCounter m_totalOperationCounter = new PerformanceCounter(CategoryName, PerformanceCounterTotalOperationName, false);

Points for attention:

Counter 1 by class name and counter name before finding from the system we registered, the last parameter is set to false because we need to write operation.

2 this class can be used in addition to write counter outside also can read some designated counter from the system, for example CPU usage, memory usage data.

3 in addition to read the performance parameters from the local computer, you can read data from the other computer in LAN.

Three, write data

Run time according to their own needs to change the counter, the counter increment by calling the Increment method, can also be directly to the RawData assignment (but note thread safe)

for (int i = 0; i <100; i++)

{

    //This is a method I own package

    PerfCounterSample.Instance.IncreaseOpCounter();

    //Analog processing

    Thread.Sleep(new Random().Next(2000));

}
public void IncreaseOpCounter()

{

    m_totalOperationCounter.Increment();

}

There is another good way is not the direct operation of the counter, but the data records in one place, then time to refresh to counter.

for (int i = 0; i <100; i++)
{
    count++;
    //Analog processing
    Thread.Sleep(new Random().Next(2000));
}
Timer m_Timer = new Timer();
m_Timer.AutoReset = true;
m_Timer.Interval = 1000.0;//1 Second
m_Timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
m_Timer.Start();
void Timer_Elapsed(object sender, ElapsedEventArgs e)

{
    lock (m_totalOperationCounter)
    {
        m_totalOperationCounter.RawValue = count;
    }
}

Points for attention:

1 I think here is mainly to pay attention to numerical thread safety and the counter mode of operation (this depends on your needs.).

Four, add our counter in the performance monitor

We first run the program counter register to the system, and then open the performance monitor counters to monitor our list of.


性能监视器类型

Be accomplished.!


监视结果

The sample code:

代码


Reference:

1. MSDN, Windows The performance monitor overview

2. MSDN, PerformanceCounterCategory.Create

3. MSDN, The PerformanceCounterType enumeration

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Kent at November 17, 2013 - 4:01 AM