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.
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.
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.
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With