Wednesday, 7 September 2011

Improving your perfomance with SPMonitoredScope

In the past some of us we used to build pieces of code like this to test the performance in Sharepoint:

DateTimer startTime = DateTime.Now;
MyMethod()
Trace.Write("MyMethod() time"+ DateTime.Now.Subtract(startTime).Seconds);

Microsoft has gone quite far with this subject and has decided to implement a class to monitor the time and show it in our shinny “Developer DashBoard”. The class is called SPMonitoredScope and it is really easy to implement.

I am going to post to examples, try both by checking the “Development DashBoard”. The first one is the slow one, the second one is the fast one.

Slow one:

using (SPMonitoredScope monitoredScope = new SPMonitoredScope("NetSourceCode Monitor"))
{
    //## This is just a simple operation...
    Threading.Thread.Sleep(5000);
}

Fast one:

using (SPMonitoredScope monitoredScope = new SPMonitoredScope("NetSourceCode Monitor"))
{ 
 //## I do NOTHING!
  int i=0;
}

You can check your “Developer DashBoard” and play around with it.


Note: To Enable and Disable the development DashBoard please, run this code with PowerShell:

Enable
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On
$addsetting.Update()
Disable
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$addsetting =$service.DeveloperDashboardSettings
$addsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::Off
$addsetting.Update()

By the way, this is how the Development Dashboard Looks like:


image