Code example detailing performance counters for C # server performance monitoring

Source: Internet
Author: User
The previous article, "WMI for server Performance monitoring", describes how to get server performance through remote COM (which can also be used locally), so this article mainly says that the Windows system comes with performance monitoring----->performancecouonter.

Open management Tools--performance, we can immediately see the server's CPU, process run time, disk capacity and other performance parameters chart. However, more than just these, we can look at other performance metrics by adding a technical device:

If you say it's too much trouble, OK, we'll take these values out of C # to implement our own performance monitoring:

1. Add a reference:

Using System.Diagnostics;

2. Create and Instantiate PerformanceCounter

public static System.Diagnostics.PerformanceCounter pc= new System.Diagnostics.PerformanceCounter ();p ublic static System.Diagnostics.PerformanceCounter pcm= New System.Diagnostics.PerformanceCounter ();p ublic static System.Diagnostics.PerformanceCounter pcb= New System.Diagnostics.PerformanceCounter ();p ublic static System.Diagnostics.PerformanceCounter pcc= new System.Diagnostics.PerformanceCounter ();//We use four objects to do different things, Note: It is static, otherwise the data taken each time is the initial value, such as CPU utilization is 0

3. Constructors

Static Capabilityscout () ... {PC. CategoryName = "Processor";p C. CounterName = "% Processor time";p c. INSTANCENAME = "_total";p C. MachineName = "."; Pcm. CategoryName = "Memory";p cm. CounterName = "% Committed Bytes in use";p cm. MachineName = "."; Pcb. CategoryName = "Windows Media Unicast Service";p CB. CounterName = "Allocated Bandwidth";p CB. MachineName = "."; Pcc. CategoryName = "Windows Media Unicast Service";p cc. CounterName = "Connected clients";p cc. MachineName = ".";}

4. Get Counter Value

    Get CPU Utilization #region Get CPU utilization public        static string Getcpuusage ()        ... {            string used = pc. NextValue (). ToString ();            return used;        }        #endregion        Get memory usage #region get memory usage public        static string GetMemory () ...        {            float used = PCM. NextValue ();            return used. ToString ();        }        #endregion        get WMS Connections #region Get the number of WMS connections public        static string Getconnectedcount () ...        {            string count = PCC. NextValue (). ToString ();            return count;        }        #endregion        Get network traffic #region get network traffic public        static string Getserverbandwidth ()        ... {            String bandwidth = PCB. NextValue (). ToString ();            return bandwidth;        }        #endregion

Of course, this is only a few of them, but by using the same approach, we can get more performance and run the process, but the point is that the data obtained must be provided by the Windows service, and of course we can write some Windows services ourselves. Added to the system PerformanceCounter, which is also very convenient for. Net.

How, compared with WMI, is not a little more convenient, hehe ~ ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.