Figure 1 - ANTS (Advanced .NET Tool Suite).
ANTS (Advanced .NET Tool Suite) currently consists of two tools that aide you in .NET development. One is a Load Testing tool tool that simulates multiple concurrent users of a web-based system. The other tool is a profiling tool that allows the user to determine how much time particular parts of a .NET program spend executing. The profiling tool allows the programmer to fine tune their product and determine which areas of the program are causing sluggish bottlenecks. The user can also see which parts of the code are being called the most often.
The ANTS Profiler was very easy to install. You just run the installation and it launches immediately. No need to restart the computer. When you first run ANTS profiler you can't help noticing that the application has some parallels to Visual Studio.NET. For example, the profiler comes up with a start screen with html hyperlinks to several options (as shown in Figure 2 below):
Figure 2 - Start Page in Ants
I was able to dive immediately into the tutorial provided in the help (the tutorial is a good majority of the help file, but the product is so easy to use you don't really need much help). I began by clicking on the Set up new project for profiling a .NET application link which brings you to the project properties screen in Figure 3. In this screen you have a choice of profiling either web services and web applications, COM+ applications, or general applications. The applications that you can profile consist of Window Forms applications and Console .NET applications. In the tutorial, Red Gate gives a profiling of a Mandelbrot plotting executable which is conveniently already available in the dropdown list so you don't really need to search for it with the browse button.
Figure 3 - Projects Properties Screen
Clicking the button on the toolbar launches the Window Forms Program shown below in Figure 4. If you don't want to start profiling immediately, you can uncheck the Start taking measurements immediately check box in Figure 3. This way, after you hit the go button, it won't start profiling the application until you hit the go button again.
Figure 4 - Mandelbrot Tutorial Application being profiled.
Apparently you can click the button whenever you want, so I just hit after the drawing finished. This produces a professional report which you can save and print. A sample results report is shown in figure 5. This report shows procedure times and hits for all the top level functions as well as times and hits for the top 100 slowest functions in your application.
Figure 5 - Results Report from Mandelbrot tutorial.
The report is readable and dynamic. For example you can sort functions by time, hit count, or any of the column categories simply by clicking on the hyperlink header of each column. You can easily drill down into the reported methods by clicking the hyperlinks and figure out where your bottlenecks are located. One of the wonderful features of the ANTS profiler is that you can actually drill down to the lines of code and see the amount of time spent on each line. Also there is a cool bar graph for each significant time-intensive line, so you don't even really need to inspect each number next to the code if you are just looking for the large bottlenecks. Figure 6 shows the a screen shot of the EvaluateUsingDoubles method used to calculate the Mandelbrot convergence using double types in C#.
Figure 6 - Time Spent in each line of code of the EvaluateUsingDoubles method.
If you click on the Slow radio button and shown in Figure 4 and draw the mandelbrot set, you notice a significant reduction in speed due to the way the mandelbrot numbers are evaluated. The Slow version evaluates the mandelbrot set using complex numbers. Figure 7 from the ANTS profiler shows the significant time degradation due to calculating the mandelbrot set using complex number classes. Apparently you lose a lot of time overriding operators such as multiplication for a complex number class shown in Figure 8:
Figure 7 - Time Spent in each line of code of the EvaluateUsingComplexNumbers method
Figure 8 - Profiling Complex Number Multiplication
Seeing the power of the ANT profiler got me excited to try the profiler on one of my own applications. I picked a genetic algorithm app I've been toying around with that tries to predict an equation for prime numbers. Although I've had little luck with coming up with an equation for primes, I noticed the program ran a bit slow, so I decided to put ANTS to the test. It was just as easy to profile my own app. I simply started a new project, selected my bin/Debug/MEPPrimeAnalysis.exe into the project and pressed the GO button. After pressing the stop button, the following report was generated (shown in figure 9).
Figure 9 - Genetic Algorithm ANTS Profile Report for predicting equations for primes
Apparently my Genomes were spending a lot of time in the CalculatePrimeFitness function. Specifically, a lot of time was being spent in the Factorial method called by the fitness function. Looking at the factorial method at the source level, it becomes more obvious why this is occurring (as in figure 10). Factorials simply take a large amount of time to calculate for large iterations through the loop. Needless to say, after discovering the time intensive calculation of a factorial, I had second thoughts about using the factorial as part of my fitness function. Especially since it didn't seem to contribute much to evolving populations.
Figure 10 - Factorial calculation time shown by the ANTS Profiler
Minor Improvements:
I noticed a few things when working with the ANTS tool that may improve its functionality. First of all, although you can print and save the reports, you cannot copy and paste the reports into WORD or Excel as far as I can tell, nor can you save results in html format. Also it would be nice to be able to insert something inside your .NET code to automate when the profiler starts and when it stops. For example, if I push a button, I would place a line of code inside the button event handler that would trigger the profiler to start and maybe one in application exit to trigger the profiler to stop. I also thought it would be a cool idea to be able to group the slowest functions in the report based on the calling hierarchy on the call stack, although, you can get this information when you drill down into parent methods. Currently, the tool cannot profile Windows Services, which I believe is a limitation of the .NET framework. For the most part, I was very impressed with what the tool could do for me as a programmer who simply wants to quickly find those parts of my code that are hogging CPU time. Once these troubling sections are discovered in the code, I could fine tune the application for better performance.
Conclusion:
The ANTS Profiler is an excellent profiling tool that requires almost zero learning curve to use. It seems to work on my existing applications and I'm very satisfied at the level of detail it displays in profiling my application (down to the line of source code). If you want to download a trial version and see what I'm talking about, simply go to Red Gate's web site and give it a try. You may find some traffic jams in your code that are preventing you from smooth driving on the .NET highway.
Product: ANTS Profiler