Walkthrough: Profiling a SharePoint Application
This walkthrough shows how to use the profiling tools in Visual Studio to optimize the performance of a SharePoint application. The example application is a SharePoint feature event receiver that contains an idle loop that degrades the performance of the feature event receiver. The Visual Studio profiler enables you to locate and eliminate the most expensive (slowest-performing) part of the project, also known as the hot path.
This walkthrough demonstrates the following tasks:
You need the following components to complete this walkthrough:
Supported editions of Microsoft Windows and SharePoint. For more information, see Requirements for Developing SharePoint Solutions.
Visual Studio 2012.
First, create a SharePoint project.
On the menu bar, choose File, New, Project to display the New Project dialog box.
Expand the SharePoint node under either Visual C# or Visual Basic, and then choose the 2010 node.
In the templates pane, choose the SharePoint 2010 Project template.
In the Name box, enter ProfileTest, and then choose the OK button.
The SharePoint Customization Wizard appears.
On the Specify the site and security level for debugging page, enter the URL for the SharePoint server site where you want to debug the site definition, or use the default location (http://system name/).
In the What is the trust level for this SharePoint solution? section, choose the Deploy as a farm solution option button.
Currently, you can only profile farm solutions. For more information about sandboxed solutions versus farm solutions, see Sandboxed Solution Considerations.
Choose the Finish button. The project appears in Solution Explorer.
Next, add a feature to the project along with an event receiver for the feature. This event receiver will contain the code to be profiled.
In Solution Explorer, open the shortcut menu for the Features node, choose Add Feature, and leave the name at the default value, Feature1.
In Solution Explorer, open the shortcut menu for Feature1, and then choose Add Event Receiver.
This adds a code file to the feature with several commented-out event handlers and opens the file for editing.
In the event receiver class, add the following variable declarations.
FeatureActivatedprocedure with the following code.
Public Overrides Sub FeatureActivated(properties As SPFeatureReceiverProperties) Try Using site As New SPSite(siteUrl) Using web As SPWeb = site.OpenWeb(webUrl) ' Reference the lists. Dim announcementsList As SPList = web.Lists("Announcements") ' Add a new announcement to the Announcements list. Dim listItem As SPListItem = announcementsList.Items.Add() listItem("Title") = "Activated Feature: " & Convert.ToString(properties.Definition.DisplayName) listItem("Body") = Convert.ToString(properties.Definition.DisplayName) & " was activated on: " & DateTime.Now.ToString() ' Waste some time. TimeCounter() ' Update the list. listItem.Update() End Using End Using Catch e As Exception Console.WriteLine("Error: " & e.ToString()) End Try End Sub
Add the following procedure below the
In Solution Explorer, open the shortcut menu for the project (ProfileTest), and then choose Properties.
In the Properties dialog box, choose the SharePoint tab.
In the Active Deployment Configuration list, choose No Activation.
Selecting this deployment configuration enables you to manually activate the feature later in SharePoint.
Save the project.
Now that the SharePoint project is ready, configure it and deploy it to the SharePoint server.
On the Analyze menu, choose Launch Performance Wizard.
On page one of the Performance Wizard, leave the method of profiling as CPU sampling and choose the Next button.
The other profiling methods can be used in more advanced profiling situations. For more information, see Understanding Performance Collection Methods.
On page two of the Performance Wizard, leave the profile target as ProfileTest and choose the Next button.
If a solution has multiple projects, they appear in this list.
On page three of the Performance Wizard, clear the Enable Tier Interaction Profiling check box, and then choose the Next button.
The Tier Interaction Profiling (TIP) feature is useful for measuring the performance of applications that query databases and for showing you the number of times a web page is requested. Because that data is not required for this example, we will not enable this feature.
On page four of the Performance Wizard, leave the Launch profiling after the wizard finishes check box selected, and then choose the Finish button.
The wizard enables application profiling on the server, displays the Performance Explorer window, and then builds, deploys, and runs the SharePoint application.
Activate the feature in SharePoint, triggering the
FeatureActivation event code to run.
In SharePoint, open the Site Actions menu, and then choose Site Settings.
In the Site Actions list, choose the Manage site features link.
In the Features list, choose the Activate button next to ProfileTest Feature1.
There is a pause when you do this, due to the idle loop being called in the
On the Quick Launch bar, choose Lists and then in the Lists list, choose Announcements.
Notice that a new announcement has been added to the list stating that the feature was activated.
Close the SharePoint site.
After you close SharePoint, the profiler creates and displays a Sample Profiling Report and saves it as a .vsp file in the ProfileTest project’s folder.
Now that you have run and profiled the SharePoint application, view the test results.
In the Functions Doing the Most Individual Work section of the Sample Profiling Report, notice that
TimeCounteris near the top of the list.
This location indicates that
TimeCounterwas one of the functions with the highest number of samples, meaning it's one of the biggest performance bottlenecks in the application. This situation isn't surprising, however, because it was purposely designed that way for demonstration purposes.
In the Functions Doing the Most Individual Work section, choose the
ProcessRequestlink to display the cost distribution for the
In the Called functions section for
ProcessRequest, notice that the FeatureActiviated function is listed as the most expensive called function.
In the Called functions section, choose the FeatureActivated button.
In the Called functions section for FeatureActivated, the
TimeCounterfunction is listed as the most expensive called function. In the Function Code View pane, the highlighted code (
TimeCounter) is the hotspot and indicates where the correction is needed.
Close the Sample Profiling Report.
To view the report again at any time, open the .vsp file in the Performance Explorer window.
Now that hotspot function in the SharePoint application has been identified, fix it.
In the feature event receiver code, comment out the
TimeCountermethod call in
FeatureActivatedto prevent it from being called.
Save the project.
In Performance Explorer, open the Targets folder, and then choose the ProfileTest node.
On the Performance Explorer toolbar, in the Actions tab, choose the Start Profiling button.
If you want to change any of the profiling properties prior to reprofiling the application, choose the Launch Performance Wizard button instead.
Follow the instructions in the Running the SharePoint Application section, previously in this topic.
The feature should activate much faster now that the call to the idle loop has been eliminated. The Sample Profiling Report should reflect this.