Simple creation of a performance counter

Source: Internet
Author: User

The role of performance monitoring

Performance monitoring can be used to obtain general messages about the normal behavior of the application, and performance monitoring is a powerful tool that helps to understand the workload of the system, observing changes and trends, especially applications running on the server

Second, performance monitoring Class (System.Diagnostics):
PerformanceCounter class: Monitor count and write count. You can also use this class to create a new performance category
PerformanceCounterCategory class: You can view all of the existing categories and create categories. You can programmatically get all the counters in a category
Performancecounterinstall class: For installing performance counters

III. Create a performance category (two ways to create):

1, Graphical creation:

650) this.width=650; "style=" Float:none; "title=" Sogou 20150120005034.png "src=" http://s3.51cto.com/wyfs02/M01/58/D0/ Wkiom1s9nl_jfh07aakmtt4nm1y094.jpg "alt=" Wkiom1s9nl_jfh07aakmtt4nm1y094.jpg "/>

650) this.width=650; "style=" Float:none; "title=" Sogou 20150120005125.png "src=" http://s3.51cto.com/wyfs02/M02/58/CD/ Wkiol1s9nzlyupawaahk1368vio290.jpg "alt=" Wkiol1s9nzlyupawaahk1368vio290.jpg "/>

2, Code creation + operation (can monitor performance counts for different applications):

650) this.width=650; "title=" Sogou 20150119230912.png "src=" http://s3.51cto.com/wyfs02/M00/58/CD/ Wkiol1s9n9wstnc6aabxi9pdwgi941.jpg "alt=" Wkiol1s9n9wstnc6aabxi9pdwgi941.jpg "/>

 using System;using System.Collections.Generic;using System.ComponentModel;using  system.data;using system.drawing;using system.linq;using system.text;using  system.threading.tasks;using system.windows.forms;using system.windows.threading;using  System.diagnostics;namespace performancecounttest2{    public partial class  form1 : form    {        // Instance name of the performance counter         private const string _ newperformancecountername =  "PerformanceCountTest2";         Type name of the performance counter         private const string _ performancecountercategoryname =  "Myzhangdi";         // Performance counter name         private sortedlist<string, tuple<string, string>> _performancecounternames;         //-----Performance Counter (component)         //record the number of button clicks          private PerformanceCounter buttonClickCount;         //Record button hits per second         private  performancecounter buttonclickcountsec;        //the number of clicks per second that the button holds         private int Record_buttonClickCount = 0;         //Initialize performance counter name          Private void initialzieperformancecounternames ()         {             _PerformanceCounterNames =  New sortedlist<strinG, tuple<string, string>> ();             _performancecounternames.add ("Buttonclickcount",  tuple.create ("Buttonclickcount",  " Record the number of button clicks "));             _ Performancecounternames.add ("Buttonclickcountsec",  tuple.create ("Buttonclickcountsec",  "record button hits per second" ));         }        // Initialize performance counters (components)         private void  Initializeperformancecounter ()         {             buttonClickCount = new PerformanceCounter             {                 categoryname = _performancecountercategoryname,//performance counter type name                  countername = _performancecounternames["Buttonclickcount"]. item1,//Performance counter name                  MachineName =  ".",//local computer                  instancelifetime = performancecounterinstancelifetime.process,//life cycle                 ReadOnly  = false,//can write                  instancename = _newperformancecountername//Instance Name              };             Buttonclickcountsec = new performancecounter            {                 categoryname = _ performancecountercategoryname,//performance counter type name                  countername = _performancecounternames["ButtonClickCountSec"]. item1,//Performance counter name                  MachineName =  ".",//local computer                  instancelifetime = performancecounterinstancelifetime.process,//life cycle                 ReadOnly  = false,//can write                  instancename = _newperformancecountername//Instance Name             };         }        //Register Performance Counters          private void registerperformancecounter ()          {            //determine if this counter type name exists             if  (! Performancecountercategory.exists (_performancecountercategoryname))              {                 var CounterCreationDatas = new CounterCreationData[2];                 countercreationdatas[0] =  new countercreationdata                {                     countername =  _performancecounternames["Buttonclickcount"]. item1,                     counterhelp = _performancecounternames["Buttonclickcount"]. item2,                     CounterType = PerformanceCounterType.NumberOfItems32                 };                 CounterCreationDatas[1] = new  countercreationdata               & nbSp {                     countername = _performancecounternames["Buttonclickcountsec"]. item1,                     counterhelp = _performancecounternames["Buttonclickcountsec"]. item2,                     CounterType = PerformanceCounterType.RateOfCountsPerSecond32                 };                 countercreationdatacollection counts  = new countercreationdatacollection (Countercreationdatas);                 //Creating a ClassType                  Performancecountercategory.create (_performancecountercategoryname,  "type description",  performancecountercategorytype.multiinstance, counts);             }        }          public form1 ()         {             initializecomponent ();         }        private void form1_load (object  Sender, eventargs e)         {             initialzieperformancecounternames ();             initializeperformanCecounter ();             dispatchertimer timer  = new dispatchertimer (Timespan.fromseconds (1),                 DispatcherPriority.Background,                 delegate                 {                     //the number of clicks per second that the button is placed on   assigned to   the number of clicks per second                      buttonclickcountsec.rawvalue = this. record_buttonclickcount;                     //initialization value (variable that holds the number of clicks per second of the button)                      This. record_buttonclickcount = 0;                 },                 Dispatcher.CurrentDispatcher                 );             // Turn on Time counter             timer. Start ();         }        ///  <summary>        ///  Register Performance Counters          /// </summary>        /// < Param name= "Sender" ></param>        /// <param name= "E" ></param>         private void button1_click (Object sender, eventargs  e)         {             registerperformancecounter ();        }         /// <summary>         ///  Click Test         /// </summary>         /// <param name= "Sender" ></param>         /// <param name= "E" ></param>         private void button2_click (object sender, eventargs e)          {            buttonclickcount.increment ();             record_buttonclickcount++;//number of hits per second         }    }}

3, use Performance Monitor to view

650) this.width=650; "style=" width:731px;height:470px; "title=" Sogou 20150120003709.png "src=" http://s3.51cto.com/ Wyfs02/m00/58/d0/wkiom1s9ny2xlsxsaaryi9hv28q159.jpg "width=" 1001 "height=" 562 "alt=" Wkiom1s9ny2xlsxsaaryi9hv28q159.jpg "/>

This article is from the "program Ape's Home--hunter" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1605962

Simple creation of a performance counter

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.