Walkthrough: Profiling Applications
This walkthrough demonstrates how to profile an application to identify performance problems.
In this walkthrough, you will step through the process of profiling a managed application, and using sampling and instrumentation to isolate and identify performance problems in the application.
In this walkthrough, you will follow these steps:
Profile an application by using the sampling method.
Analyze sampled profiling results to locate and fix a performance issue.
Profile an application by using the instrumentation method.
Analyze instrumented profiling results to locate and fix a performance issue.
Prerequisites
Microsoft Visual Studio 2005 Team System.
Intermediate understanding of C#.
A copy of the PeopleTrax Sample (Profiling Tools).
To work with the information provided by profiling, it is best to have debugging symbol information available.
Profiling by Using the Sampling Method
Sampling is a profiling method by which the process in question is periodically polled to determine the active function. The resulting data provides a count of how frequently the function in question was on top of the call stack when the process was sampled.
To profile an application by using the sampling method
Open the PeopleTrax solution in Microsoft Visual Studio 2005.
The PeopleTrax solution now populates Solution Explorer.
On the Analyze menu, click Launch Performance Wizard.
The Performance Wizard appears.
From the Available targets drop-down list, select PeopleTrax, and then click Next.
Click Sampling, and then click Next.
Click Finish.
Set the project configuration setting to Release.
It is recommended to use a release build to detect performance problems in your application.
A release build is recommended for profiling because a debug build has additional information compiled into it that may adversely affect performance and fail to illustrate performance issues accurately.
A release build does not automatically give you symbol information. To configure your build so symbol information is visible in profiler performance reports, see /Z7, /Zi, /ZI (Debug Information Format) and How to: Reference Windows Symbol Information.
On the Performance Explorer toolbar, click Launch.
Visual Studio 2005 builds the project and starts to profile the application. The PeopleTrax application window appears.
Click Get People.
Click ExportData.
Notepad opens and displays a new file that contains the exported data from PeopleTrax.
Close Notepad, and then close PeopleTrax application.
Visual Studio 2005 generates a performance session report (*.vsp) and automatically loads it.
To analyze sampled profiling results
On the Profiler window toolbar, open the report view drop down box and select the Functions view of the report.
Right-click the data grid and then click Add/Remove Columns.
The Add/Remove Columns dialog box appears.
From the column list, select Source File Name, and then click OK.
On the data grid, click the Source File Name column to sort the data grid.
By sorting on this column, all the rows that apply to the PeopleTrax solution are grouped.
Locate the rows that display data for the PeopleTrax solution.
After examining these rows, you see that the following functions were sampled more frequently than other portions of the PeopleTrax solution:
PeopleTrax.Form1.GetPeopleButton_Click
PeopleNS.People.GetPeople
PeopleTrax.Form1.ExportToExcel_Click
PeopleTrax.Form1.ExportData
Focus on the GetPeopleButton_Click event.
Right-click the PeopleTrax.Form1.GetPeople_Click row and then click View Source.
Form1.cs opens in the code editor and the pointer appears in the GetPeopleButton_Click event.
Review the source code for the GetPeopleButton_Click event.
After reviewing this code, you may identify areas for optimization:
If you want to add items one at a time by using the Add method of the ListView.ListViewItemCollection class, you can use the BeginUpdate method to prevent the control from repainting the list view every time that you add an item. After you have completed the task of adding items to the control, call the EndUpdate method to enable the list view to repaint. This method of adding items can prevent flickered drawing of the list view when you add a lot of items to the control, and will also reduce the time spent populating the list view.
The default setting in App.Config is 1500 names.
When you set the fore color of the list view item, you can increase performance by using the KnownColor enumeration instead of specifying the color in a string and calling the Color.FromName method.
This event calls a function named People.GetPeople to retrieve an array of names to add to the list view.
Right-click the call to GetPeople and then click Go To Definition.
After reviewing this code, you may identify areas for optimization:
The code to retrieve a new array of fullNames and companyNames is accessed repeatedly; these arrays should be created one time and re-used.
To replace these problem areas with optimized code, add OPTIMIZED_GETPEOPLE as a conditional compilation symbol to both the PeopleTrax and People projects.
In Solution Explorer, right-click the PeopleTrax project and then click Properties.
The PeopleTrax project properties form appears.
Click the Build tab.
In the Conditional Compilation Symbols text box, add OPTIMIZED_GETPEOPLE.
Repeat steps 18 - 20 for the People project.
Close the project property forms and then save all when you are prompted to save.
It is recommended that you run the profiling session again, even if there are user visible improvements in performance. Reviewing the data after you fix a problem is important if the first problem obscures some other problem.
Now, profile the application by using the instrumentation method.
Profiling by Using the Instrumentation Method
Instrumentation is a profiling method by which specially built versions of the profiled binaries contain probe functions that collect timing information at the entry and exit to functions in an instrumented module. Because this method of profiling is more invasive than sampling, it incurs a greater amount of overhead. Instrumented binaries are also larger than debug or release binaries and are not intended for deployment.
To profile an existing application by using the instrumentation method
In Performance Explorer, click Instrumentation from the drop-list.
On the Performance Explorer toolbar, click the Launch button.
Microsoft Visual Studio 2005 builds the project and starts to profile the application. The PeopleTrax application window appears.
Click Get People.
The PeopleTrax data grid populates with data.
Click Export Data.
Notepad starts and displays a new file that contains a list of people from PeopleTrax.
Close Notepad, and then close PeopleTrax application.
Microsoft Visual Studio 2005 generates a performance session report (*.vsp).
To analyze instrumented profiling results
On the Summary view of the report, review the summary tables to identify which function was most frequently called and where most of the application execution occurred.
Based on this information, you see that the application spent lots of time running System.String.Concat operations.
Double-click System.String.Concat in any one of the summary tables.
The report switches to the Function view and highlights System.String.Concat.
Right-click the System.String.Concat row and then click Show in Caller/Callee View.
The report switches to the Caller/Callee view and shows that this function is called by PeopleTrax.Form1.ExportData.
Right-click PeopleTrax.Form1.ExportData and then click View Source.
Form1.cs opens in the code editor and the pointer appears in the ExportData function.
Review the source code for the ExportData function.
After reviewing this code, you might notice that there are no literal calls to System.String.Concat. Instead, there are several uses of the += operand, which is replaced with calls to System.String.Concat in IL. Any modifications to a string in the .NET Framework cause a new string to be allocated. The .NET Framework includes a StringBuilder class that is optimized for string concatenation.
To replace this problem area with optimized code, add OPTIMIZED_EXPORTDATA as a conditional compilation symbol to the PeopleTrax project.
In Solution Explorer, right-click the PeopleTrax project and then click Properties.
The PeopleTrax project properties form appears.
Click the Build tab.
In the Conditional Compilation Symbols text box, add OPTIMIZED_EXPORTDATA.
Close the project property form and choose save all when you are prompted.
When you run the application again, you will see marked improvements in performance. It is recommended that you run the profiling session again, even if there are user visible improvements in performance. Reviewing the data after you fix a problem is important if the first problem obscures some other problem.
See Also
Reference
/Z7, /Zi, /ZI (Debug Information Format)