usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;namespacePerformanceCounterHelper{publicclassPerformanceHelperClass{[DllImport("Kernel32.dll")]publicstaticexternvoidQueryPerformanceCounter(reflongticks);publicstaticlongGetCurrentTick(){longvalue=0;QueryPerformanceCounter(refvalue);returnvalue;}}publicinterfaceIPerformanceDescription{stringCategory{get;}stringDescription{get;}}publicclassPerformanceCounter<T>whereT:IPerformanceDescription,new(){///<summary>///Counterforcountingtotalnumberofoperations///</summary>privatePerformanceCounterm_totalOperations;///<summary>///Counterforcountingnumberofoperationspersecond///</summary>privatePerformanceCounterm_operationsPerSecond;///<summary>///Counterforcountingdurationaverages///</summary>privatePerformanceCounterm_averageDuration;///<summary>///Counterforcountingdurationaveragesbase///</summary>privatePerformanceCounterm_averageDurationBase;///<summary>///Createsanewperformancecountercategorym_perfClass.Categoryifitdoesnotalreadyexistsandaddssomecounterstoit.///</summary>///privateTm_perfClass;privatestaticreadonlyPerformanceCounter<T>m_instance=newPerformanceCounter<T>();publicstaticPerformanceCounter<T>Instance{get{returnm_instance;}}privatePerformanceCounter(){m_perfClass=newT();if(!PerformanceCounterCategory.Exists(m_perfClass.Category)){CounterCreationDataCollectioncounters=newCounterCreationDataCollection();//1.counterforcountingtotals:PerformanceCounterType.NumberOfItems32CounterCreationDatatotalOps=newCounterCreationData();totalOps.CounterName="# operations executed";totalOps.CounterHelp="Total number of operations executed";totalOps.CounterType=PerformanceCounterType.NumberOfItems32;counters.Add(totalOps);//2.counterforcountingoperationspersecond:PerformanceCounterType.RateOfCountsPerSecond32CounterCreationDataopsPerSecond=newCounterCreationData();opsPerSecond.CounterName="# operations / sec";opsPerSecond.CounterHelp="Number of operations executed per second";opsPerSecond.CounterType=PerformanceCounterType.RateOfCountsPerSecond32;counters.Add(opsPerSecond);//3.counterforcountingaveragetimeperoperation:PerformanceCounterType.AverageTimer32CounterCreationDataavgDuration=newCounterCreationData();avgDuration.CounterName="average time per operation";avgDuration.CounterHelp="Average duration per operation execution";avgDuration.CounterType=PerformanceCounterType.AverageTimer32;counters.Add(avgDuration);//4.basecounterforcountingaveragetimeperoperation:PerformanceCounterType.AverageBaseCounterCreationDataavgDurationBase=newCounterCreationData();avgDurationBase.CounterName="average time per operation base";avgDurationBase.CounterHelp="Average duration per operation execution base";avgDurationBase.CounterType=PerformanceCounterType.AverageBase;counters.Add(avgDurationBase);//createnewcategorywiththecountersabovePerformanceCounterCategory.Create(m_perfClass.Category,m_perfClass.Description,counters);}//createcounterstoworkwithm_totalOperations=newPerformanceCounter();m_totalOperations.CategoryName=m_perfClass.Category;m_totalOperations.CounterName="# operations executed";m_totalOperations.MachineName=".";m_totalOperations.ReadOnly=false;m_totalOperations.RawValue=0;m_operationsPerSecond=newPerformanceCounter();m_operationsPerSecond.CategoryName=m_perfClass.Category;m_operationsPerSecond.CounterName="# operations / sec";m_operationsPerSecond.MachineName=".";m_operationsPerSecond.ReadOnly=false;m_operationsPerSecond.RawValue=0;m_averageDuration=newPerformanceCounter();m_averageDuration.CategoryName=m_perfClass.Category;m_averageDuration.CounterName="average time per operation";m_averageDuration.MachineName=".";m_averageDuration.ReadOnly=false;m_averageDuration.RawValue=0;m_averageDurationBase=newPerformanceCounter();m_averageDurationBase.CategoryName=m_perfClass.Category;m_averageDurationBase.CounterName="average time per operation base";m_averageDurationBase.MachineName=".";m_averageDurationBase.ReadOnly=false;m_averageDurationBase.RawValue=0;}publicvoidInitialize(){}///<summary>///Incrementscounters.///</summary>///<paramname="ticks">ThenumberoftickstheAverageTimer32countermustbeincrementedby</param>publicvoidDoPerformanceCounter(longticks){//simplyincrementthecountersm_totalOperations.Increment();m_operationsPerSecond.Increment();m_averageDuration.IncrementBy(ticks);//incrementthetimerbythetimecostoftheoperationm_averageDurationBase.Increment();//incrementbasecounteronlyby1}}}