Export (0) Print
Expand All

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.

To work with the information provided by profiling, it is best to have debugging symbol information available.

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

  1. Open the PeopleTrax solution in Microsoft Visual Studio 2005.

    The PeopleTrax solution now populates Solution Explorer.

  2. On the Analyze menu, click Launch Performance Wizard.

    The Performance Wizard appears.

  3. From the Available targets drop-down list, select PeopleTrax, and then click Next.

  4. Click Sampling, and then click Next.

  5. Click Finish.

  6. 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.

  7. On the Performance Explorer toolbar, click Launch.

    Visual Studio 2005 builds the project and starts to profile the application. The PeopleTrax application window appears.

  8. Click Get People.

  9. Click Export Data.

    Notepad opens and displays a new file that contains the exported data from PeopleTrax.

  10. 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

  1. On the Profiler window toolbar, open the report view drop down box and select the Functions view of the report.

  2. Right-click the data grid and then click Add/Remove Columns.

    The Add/Remove Columns dialog box appears.

  3. From the column list, select Source File Name, and then click OK.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. In Solution Explorer, right-click the PeopleTrax project and then click Properties.

    The PeopleTrax project properties form appears.

  10. Click the Build tab.

  11. In the Conditional Compilation Symbols text box, add OPTIMIZED_GETPEOPLE.

  12. Repeat steps 18 - 20 for the People project.

  13. 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.

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

  1. In Performance Explorer, click Instrumentation from the drop-list.

  2. 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.

  3. Click Get People.

    The PeopleTrax data grid populates with data.

  4. Click Export Data.

    Notepad starts and displays a new file that contains a list of people from PeopleTrax.

  5. Close Notepad, and then close PeopleTrax application.

    Microsoft Visual Studio 2005 generates a performance session report (*.vsp).

To analyze instrumented profiling results

  1. 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.

  2. Double-click System.String.Concat in any one of the summary tables.

    The report switches to the Function view and highlights System.String.Concat.

  3. 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.

  4. 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.

  5. 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.

  6. To replace this problem area with optimized code, add OPTIMIZED_EXPORTDATA as a conditional compilation symbol to the PeopleTrax project.

  7. In Solution Explorer, right-click the PeopleTrax project and then click Properties.

    The PeopleTrax project properties form appears.

  8. Click the Build tab.

  9. In the Conditional Compilation Symbols text box, add OPTIMIZED_EXPORTDATA.

  10. 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.

Community Additions

ADD
Show:
© 2014 Microsoft