Поделиться через


Walkthrough: Profiling Applications

This walkthrough demonstrates profiling 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 perform the following 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.

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

Profiling 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

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

    The PeopleTrax solution now populates Solution Explorer.

  2. On the Tools menu, point to Performance Tools, and then click 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.

    Important

    Set the project configuration setting to Release. It is recommended to use release build to detect performance problems in your application.

  6. On the Performance Explorer toolbar, click the Launch button.

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

  7. Click Get People.

  8. Click Export Data.

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

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

    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:

It is recommended that you run the profiling session again, even if there are user visible improvements in performance. Taking another look at the data after fixing a problem is important if the first problem obscures some other problem.

Now, profile the application by using the instrumentation method.

Profiling 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 a considerable amount 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 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. Taking another look at the data after fixing a problem is important if the first problem obscures some other problem.