ANTS Profiler™
How to profile ASP.NET applications
This article explains how you can use ANTS Profiler to profile web applications. We'll look at how you profile web applications hosted in the ASP.NET Development Server (also known as WebDev or Cassini), and web services and websites that are hosted in IIS.
Profiling applications hosted in the built-in web server
The ASP.NET Development Server is the built-in web server for your development environment. It's a good place to start debugging the web application you're developing because, unlike in IIS, you don't have to worry about configuration and security settings. You also don't have to worry about stopping and restarting the web server. However, it does have limitations so, eventually, you will want to test your web application under IIS. This is especially true if your web application has pages where only users with the appropriate security settings can access particular pages.
When you're ready to start profiling, you'll need a debug build of your web application. Then, you set up the profiler using the Profiler Project Wizard. First, choose whether to profile performance or memory usage. On the second page of the wizard, select ASP.NET web application hosted in the ASP.NET development web server. Then, on the next page, you specify the path to your application, identified by the file extension .aspx. Note that you'll probably find that the .aspx file isn't located in the Visual Studio Projects folder as you would normally expect!

When you profile your web application, ANTS Profiler profiles the server-side code in your application, rather than any client-side code. If you're interested in profiling a .NET ActiveX control that runs on the client, you can profile it as part of a desktop application (on the second page of the wizard, select .NET desktop application) and then, on the next page, specify the Internet Explorer process.
You can specify a virtual folder (/ is the root folder) and, if you want your application to be hosted on a particular port, enter the details in Port to bind web server to.
Profiling a web service hosted in IIS
Now, we'll show you how to profile a web service and look at some typical results.
On the first page of the Profiler Project Wizard, select Performance profiler – Detailed Mode to get line-level timings for web methods.
Next, you select the type of application you want to profile, in this case, ASP.NET web application hosted in IIS.
On the next page of the wizard, you specify the path to the web service. Web services are identified by the file extension .asmx. If you are running IIS 5 or 6, ANTS Profiler automatically detects the web services that are hosted on your local machine when you click the Path to ASP.NET web application list button.

On the final page, select Only .NET methods that have source code, so that you're profiling just your own source code, and click Finish. ANTS Profiler asks you if you want to start profiling your application. If you are running IIS 5, ANTS Profiler warns you that it will stop and restart IIS; it does this so that it can initialize the profiler. Note that for IIS 6 and 7 ANTS Profiler does not need to stop and restart IIS.
ANTS Profiler then opens the test harness for the web service in your Internet browser.

When you click a method, for example GetEmployeeDetails, the test harness allows you to test the method.

You enter the required parameter values and click Invoke. Your request is made to the web service using the SOAP protocol. The web service then returns an XML response which is displayed in a separate window.
To get the performance results for the method you have just tested, click Take Snapshot in the ANTS Profiler main toolbar. ANTS Profiler gathers the results and displays them in the main window.

In this example, the slowest line of code occurs in the Application_Start method. The first time the application is run, there is some overhead associated with reading in the datasets. This will only happen once, so the next method to investigate is GetEmployeeDetails, the next method in the list of slowest lines of code. This is the method that was called when you clicked Invoke.

We see here that all the lines of code are being executed extremely quickly. This is as expected, since we are simply copying and querying a fairly small dataset. We would encounter serious performance problems if the dataset were much larger.
Now that you've investigated all the slowest lines of code, you stop the profiler by clicking Stop profiling and then set up the profiler to profile the website.
Profiling a website hosted in IIS
To profile a website, you set up the Profiler Project Wizard in the same way as for the web service, except that, on the third page of the wizard (shown below), you specify the path to the website. Websites are identified by the file extension .aspx.

When you click Finish, ANTS Profiler asks you if you want to start profiling your application and, if you are running IIS 5, it then warns you that it will stop and restart IIS.
ANTS Profiler then opens your application in your Internet browser. ANTS Profiler profiles all the code that has been run since your application started. Take a snapshot at this point.

The slowest line of code in this example occurs in the Page_Load method. If we look at the source code for the Page_Load method, we see that there are two slow lines of code. The first "slow line" occurs when instantiating the proxy object for the web service call. Although the actual code is simple, a lot of methods are called "under the hood" to build up a complete model of the API exposed by the web service. The second "slow line" happens when we make the first call to the web service. Again, a large amount of initialization takes place to build up the necessary structures to handle packaging up and unpackaging any SOAP messages that are transferred. Both of these performance hits occur only once, as all the necessary data structures are cached.
You can see this by clicking Reset profiler to clear all the performance data in ANTS Profiler. Then, in the Internet browser, you click Refresh to reload the page, and take another snapshot.

This time, the time taken to construct the proxy object (Line 25) is 0 seconds, and the performance is similar to instantiating a local object. Also, the time taken to make the call to the web service is reduced to just over a couple of hundredths of a second. This is the intrinsic overhead of making the HTTP/SOAP request.
Conclusion
In this article, we've shown you how to profile a web application hosted in the built-in web server in your development environment. We've also shown you how to profile web services and websites hosted in IIS, and looked at the kind of results you will see when you profile your own web application.





