NOTE: This is primarly a .NET Fx Post
In a previous project I wrote a server process to do various calculations on available data. I was given the task to see how many calculations are done per/second. Having never done a task like this on the desktop I decided to calculate this manually using QueryPerformanceCounter, similar to something I did previously using .NET Compact Framework. This worked and logged to a text file but it wasn't graphical, and from my experience non-techies like seeing graphical images.
Since it was a server process I decided to take advantage of the System Monitor and the System.Diagnostics.PerformanceCounterCategory and System.Diagnostics.PerformanceCountrer. Since I just had to calculate the number of calculations per/second this was farily straight forward.
You setup your PerformanceCounterCategory then setup a PerformanceCounter and call Increment(). Not a big deal just a few lines of code.
The point of the post, Operator Overloading. During this project someone asked me how to do operator overloading in C#. I pointed him to the MSDN documentation but actually doing something via code is better than just reading something. The following class was created from that exercise.
/// <summary>/// Adds overloaded operator to increment a persecond monitor/// </summary>public class PerformanceCounter2{ private PerformanceCounter perfCounter; public PerformanceCounter2(string categoryName, string counterName, string instanceName, bool readOnly, bool reset) { this.perfCounter = new PerformanceCounter(categoryName, counterName, instanceName, readOnly); if (reset) { this.perfCounter.RawValue = 0; } } public static PerformanceCounter2 operator +(PerformanceCounter2 perfCounter, long incrementBy) { lock (perfCounter) { perfCounter.perfCounter.IncrementBy(incrementBy); } return perfCounter; } public static PerformanceCounter2 operator ++(PerformanceCounter2 perfCounter) { lock (perfCounter) { perfCounter.perfCounter.Increment(); } return perfCounter; }}
Now instead of creating a PerformanceCounter you create the new PerformanceCounter2 object from the above class. When you want to increment your performance counter, instead of calling
perfCounter.Increment();
you would call
perfCounter++;
The above class was created from an exercise to teach someone operator overloading. It was used in production but I don't think added any real benefit. The sample application included puts this class to use and the image below is a screen shot from using the sample application.
PerfCounter.zip (12.56 KB)
Powered by: newtelligence dasBlog 1.9.7174.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
E-mail
Theme design by Jelle Druyts