Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a PerfMon counter to record an average per call (C#)

Tags:

How can I use PerfMon counters to record the average execution time of a method in C#?

So far I've only found sample code to incrememnt or decrement a PerfMon counter.

like image 974
Justin Avatar asked Sep 11 '09 08:09

Justin


People also ask

How do I create a performance counter?

In the navigation pane, expand Monitoring Tools, and then choose Performance Monitor. In the console pane toolbar, choose the Add button. In the Add Counters window, in the Select counters from computer drop-down list, choose the computer that is running Business Central Server.

How do you analyze a perfmon counter?

To analyze counter data with the Perfmon Viewer, you can use the controls and indicators that exist in the lower and upper sectors of the viewer interface, which are referred to as the Counter Table and the Data Grid, respectively. The controls can change the way you display the data to facilitate analysis.


1 Answers

Here's some sample code I once wrote to do exactly that.

First, you need to specify and install the performance counters in question. You can do this by using an Installer:

public class CreditPerformanceMonitorInstaller : Installer {     private PerformanceCounterInstaller counterInstaller_;      public CreditPerformanceMonitorInstaller()     {         this.counterInstaller_ = new PerformanceCounterInstaller();         this.counterInstaller_.CategoryName = CreditPerformanceCounter.CategoryName;         this.counterInstaller_.CategoryType = PerformanceCounterCategoryType.SingleInstance;          CounterCreationData transferAverageData = new CounterCreationData();         transferAverageData.CounterName = CreditPerformanceCounter.AverageTransferTimeCounterName;         transferAverageData.CounterHelp = "Reports the average execution time of transfer operations";         transferAverageData.CounterType = PerformanceCounterType.AverageTimer32;         this.counterInstaller_.Counters.Add(transferAverageData);          CounterCreationData transferAverageBaseData = new CounterCreationData();         transferAverageBaseData.CounterName = CreditPerformanceCounter.AverageTransferTimeBaseCounterName;         transferAverageBaseData.CounterHelp = "Base for average transfer time counter";         transferAverageBaseData.CounterType = PerformanceCounterType.AverageBase;         this.counterInstaller_.Counters.Add(transferAverageBaseData);          this.Installers.Add(this.counterInstaller_);     }      public Installer PerformanceCounterInstaller     {         get { return this.counterInstaller_; }     } } 

To write to the performance counter, you can do it like this:

public void RecordTransfer(long elapsedTicks) {     using (PerformanceCounter averageTransferTimeCounter = new PerformanceCounter(),         averageTransferTimeBaseCounter = new PerformanceCounter())     {         averageTransferTimeCounter.CategoryName = CreditPerformanceCounter.CategoryName;         averageTransferTimeCounter.CounterName = CreditPerformanceCounter.AverageTransferTimeCounterName;         averageTransferTimeCounter.ReadOnly = false;          averageTransferTimeBaseCounter.CategoryName = CreditPerformanceCounter.CategoryName;         averageTransferTimeBaseCounter.CounterName = CreditPerformanceCounter.AverageTransferTimeBaseCounterName;         averageTransferTimeBaseCounter.ReadOnly = false;          averageTransferTimeCounter.IncrementBy(elapsedTicks);         averageTransferTimeBaseCounter.Increment();     } } 
like image 85
Mark Seemann Avatar answered Oct 19 '22 10:10

Mark Seemann