Search This Blog

Thursday, March 19, 2015

Developer Dashboard SharePoint 2010

When developing solutions, developers have the concern to deliver functional solutions that conform to the requested requirements. Although I always analyse the performance of my applications, most of the business solutions that I have worked performance is not a critical factor.
Depending on the scenario, what if performance is a requirement? How to get diagnostic information from your solutions in SharePoint 2010?
Precisely now there is the Developer Dashboard that brings a dashboard of tracing info.
In this article I will show how to enable the Developer Dashboard, how to use the APIs to display its Tracing info and how to display the rendering results of your page graphically.

Introducing the Developer Dashboard

Like something unknown, first of all I would like to introduce the Developer Dashboard, as we can see it below:
1D806A05AA43783B_453_0[1] 
Figure 1 - SharePoint 2010 Dashboard Developer
Note: Note that the dashboard border is green, because neither asserts nor critical events are displayed. I'll talk about the color changes later in this post.
The left side shows the http handler events executed during the page rendering and their execution times, whilst in the right side a mix of comprehensive information and also details of the request can be found, such as the queries sent to database.
Interestingly, the links show the callstack of the message and other details, according to the figure below:
1D806A05AA43783B_453_1[1] 
Figure 2 - Details of a database query
Once this is not unknown anymore for you, let's check what matters.

Enabling and disabling the Dashboard Developer

By default the Dashboard Developer is disabled and in order to display it there are three possibilities:
  • .Net code
  • STSADM
  • PowerShell
There are also three display levels of the Dashboard. As described below:
  • "On" - the dashboard will always be displayed;
  • "Off" - disables the dashboard;
  • "OnDemand" - adds an icon to the page, which allows the visualization of the dashboard only when clicked (toggling between On and Off).
The figure below displays this icon:
1D806A05AA43783B_453_2[1] 
Figure 3 - Developer Dashboard Icon
NOTE: It is important to note that when the Dashboard is enabled using OnDemand, only Administrators and Site Collection Owners have access to it. Members and visitors will not see the icon. In this post the Developer Dashboard will be displayed using the display level "OnDemand", which in my point of view is the ideal.

.Net Code

A Feature that enables or disables the dashboard can be created, instead of using scripts for that. This way the activation or deactivation can be done directly through the Site Collection Features page.
Create a project in Visual Studio 2010 of Empty SharePoint Project called DeveloperDashboard. Choose the option of a Farm Solution. Your initial project should look like this:
1D806A05AA43783B_453_3[1] 
Figure 4 – DeveloperDashboard Feature (Initial Project)
Add an Event Receiver to the Feature according the figure 5:
1D806A05AA43783B_453_4[1] 
Figure 5 – Adding an Event Receiver
The class DeveloperDashboardEventReceiver will be created automatically, with all the methods commented out. Add the following code for the Feature Activation and Deactivation:
Code Snippet
  1. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  2. {
  3.     SPWebService contentService = SPWebService.ContentService;
  4.     contentService.DeveloperDashboardSettings.DisplayLevel =SPDeveloperDashboardLevel.OnDemand;
  5.     contentService.DeveloperDashboardSettings.Provision();
  6. }
  7.  
  8. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  9. {
  10.     SPWebService contentService = SPWebService.ContentService;
  11.     contentService.DeveloperDashboardSettings.DisplayLevel = SPDeveloperDashboardLevel.Off;
  12.     contentService.DeveloperDashboardSettings.Unprovision();
  13. }
In the end your project should look like the figure below:
1D806A05AA43783B_453_5[1] 
Figure 6 – DeveloperDashboard Feature (Final Project)
Compile and deploy the project directly from the Visual Studio 2010 in development environments. For production environments deploy it via STSADM according the figure below:
1D806A05AA43783B_453_6[1] 
Figure 7 - Deploy of the feature on Production
In the end this feature must be enabled as the following figure:
1D806A05AA43783B_453_7[1] 
Figure 8 - Deploy of the Feature "Developer Dashboard"
Download the solution here.

STSADM

Open the Command console and enter the following cmdlet to enable the Developer Dashboard:
stsadm –o setproperty –pn developer-dashboard –pv OnDemand
To disable it:
stsadm –o setproperty –pn developer-dashboard –pv Off
NOTE: The option-pv is case-sensitive.
Result:
1D806A05AA43783B_453_8[1] 
Figure 9 - Execution of commands via STSADM

PowerShell

Another possibility is to use PowerShell for running scripts.
To enable the Developer Dashboard:
$dash = [Microsoft.SharePoint.Administration.SPWebService]::ContentService $settings = $dash.DeveloperDashboardSettings $settings.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::OnDemand $settings.Update()
Result:
1D806A05AA43783B_453_9[1] 
Figure 10 - Script activation via PowerShell
To disable it:
$dash = [Microsoft.SharePoint.Administration.SPWebService]::ContentService $settings = $dash.DeveloperDashboardSettings $settings.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::Off $settings.Update()
Result:
1D806A05AA43783B_453_10[1] 
Figure 11 - Script deactivation via PowerShell

Measuring performance and adding messages via Tracing

Once the Developer Dashboard is active, we can proceed with the addition of Tracing messages in SharePoint 2010.
As already said this is an excellent way to measure the performance of code and add custom messages for tracking.
In order to measure the time of the code snippet that we intend to track, the class SPMonitoredScopeshould be used. This information will be available on the left side of the Dashboard.
In order to add log messages, warnings or assert messages, the class SPCriticalTraceCounter should be used. This information will be available on the right side of the dashboard, and a link will be available to visualize the message details.
NOTE: The callstack, which is in the message details, can add a few Kb on the page. Thus track the growth of the amount of custom messages that you have, they will increase the size (Kb) of your page if the Developer Dashboard is enabled.
Add a new project of Visual Web Part called Tracing. The solution should resemble the figure below:
1D806A05AA43783B_453_11[1] 
Figure 12 - Web Part Tracing Project
Add the code below in the file TracingWebPartUserControl.ascx.cs:
Code Snippet
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.Web.UI.WebControls.WebParts;
  5. using Microsoft.SharePoint.Utilities;
  6.  
  7. namespace Tracing.TracingWebPart
  8. {
  9.     public partial class TracingWebPartUserControl : UserControl
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {
  13.             // Using class for tracking the execution time
  14.             using (SPMonitoredScope sc = new SPMonitoredScope("LoadMeter"))
  15.             {
  16.                 for (uint i = 0; i < 10; i++)
  17.                 {
  18.                     // Adding custom messages
  19.                     SPCriticalTraceCounter.AddDataToScope(i, "Loop For", 15,string.Format("Mensagem monitorada - {0}", i.ToString()));
  20.                 }
  21.             }
  22.         }
  23.     }
  24. }
Class TracingWebPartUserControl
This code contains two classes SPMonitoredScope and SPCriticalTraceCounter as already commented. It is important to know that the TraceLevel (3rd parameter of the methodAddDataToScope) can be:
  • 1 – Critical
  • 4 - Exception (Watson)
  • 6 – Assert
  • 8 – Warning
  • 10 – Unexpected
  • 15 - Monitorable
Perform the addition of the Web Part (1), save it (2) and then visualize the Developer Dashboard (3) as shown below:
1D806A05AA43783B_453_12[1] 
Figure 13 - Adding Web Part Tracing
Check out the Developer Dashboard, its border color is no longer green color as displayed in Figure 1. Differently it is in red color, which shows that asserts and critical events occurred (in this case the messages created):
1D806A05AA43783B_453_13[1] 
Figure 14 - Time Scope and custom messages added
Click over one of the messages and note that the message details and callstack can be seen:
1D806A05AA43783B_453_14[1] 
Figure 15 - custom message displayed in details
Download the solution here.

Developer Dashboard Visualizer

We can improve the Dashboard with a graphic display called Developer Dashboard Visualizer, which was created by Jaap Vossers and consists of a UserControl that makes use of JQuery to view the axisEvents x Execution time (left side of the Developer Dashboard).
This project is available on Codeplex: http://devdashvis.codeplex.com/
Download the wsp package and install it using the STSADM, as shown below:
1D806A05AA43783B_453_15[1] 
Figure 16 - Enabling the Developer Dashboard Visualizer
The http handler events are displayed immediately above the dashboard:
1D806A05AA43783B_453_16[1] 
Figure 17 - Graphical View
In this article we checked how to enable the SharePoint 2010 Developer Dashboard. We have checked that diagnostic information and tracing messages are output on the page to the browser that made the request. Such information may help to clarify errors or undesired results during the processing of your solution in SharePoint 2010.
I hope these tips are helpful.


No comments:

Post a Comment