Windows Performance Monitor is an easy-to-use built-in monitoring tool that can easily meet some basic and simple monitoring requirements. This article mainly summarizes how to expose some performance data in your application to the Performance Monitor for convenient management.
What? I don't know what Windows Performance Monitor is?
"Microsoft Management Console (MMC) A management unit that provides tools for analyzing system performance. You can monitor applications and hardware performance in real time from a single console, customize the data to be collected in logs, and define the thresholds for alarms and automatic operations, generate Reports and view past performance data in various ways." -MSDN[1]
You need to record performance data of your own applications. For example, if you have a WCF application, you need to record the number of requests processed by the application per minute and the average request processing time. The data can be used to monitor and analyze applications. If you store the data in your own form, it will lead to a lot of other work. If you expose the data to performance monitors, it will be much easier.
Key types:
PerformanceCounterCategory: This corresponds to a group on the monitor, which can contain many counters (Counter)
CounterCreationData: defines the counter name and some help string classes, mainly used during creation.
PerformanceCounterType: each counter requires a type, which provides all counter types. Refer to MSDN to see the applicability of each type and the calculation formula of this type. [3]
PerformanceCounter: this is the type that truly exposes data. With this type, we can expose the data we have collected to the Performance Monitor.
Steps:
An example is written to record the total number of operations.
1. Create a custom type and add it 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 ";
//= =
1. It is strongly recommended that you create a counter category during application installation or deployment, instead of adding a counter category during running. If added during running, the performance monitor may be too late to refresh, resulting in an error. [2]
2. The Administrator permission is required to create a counter category. In addition, it is recommended that you create a counter category during installation or deployment because it is very likely that you do not have the Administrator permission during operation.
3. When selecting PerformanceCounterType, you 'd better go to MSDN to find the type that best suits your needs. [3]
Ii. Create PerformanceCounter at runtime
PerformanceCounter m_totalOperationCounter = PerformanceCounter(CategoryName, PerformanceCounterTotalOperationName, );
Note:
1. Find the previously registered counter from the system using the category name and counter name. The last parameter is set to false because we need to perform the write operation.
2. In addition to writing a counter, this class can also read a Specified Counter from the system, such as CPU usage, memory usage, and other data.
3. In addition to reading performance parameters from a local computer, you can also read data from other computers in the LAN.
3. Write Data
You can change the counter according to your needs during running. You can call the Increment or other methods to auto-Increment the counter, or assign values to RawData directly (but pay attention to thread security)
( i = ; i < ; i++ Random().Next(
Another better way is to record data in one place instead of directly operating the counter, and then regularly refresh the data to the counter.
( i = ; i < ; i++++ Thread.Sleep( Random().Next(
Timer m_Timer = = = ;m_Timer.Elapsed +=
Timer_Elapsed(=
1. I think the main thing here is to pay attention to thread security and the calculation method of the final counter value (this depends on your needs ).
4. Add our counters to Performance Monitor
First, run the program to register our counters in the system, and then enable the performance monitor to add our counters to the monitoring list.
Success!
Sample Code:
Reference:
1. MSDN, Windows performance monitor Overview
2. MSDN, PerformanceCounterCategory. Create
3. MSDN, PerformanceCounterType Enumeration