Using SPMonitoredScope to monitor performance of your code

Posted Friday, May 28, 2010 10:43 AM by CoreyRoth

As a SharePoint developer, I’m sure you were excited to see the Developer Dashboard.  It’s a great way to see what’s going ton with the performance of a page.  Did you know you can add your own performance monitors to it though?  It’s actually quite easy with the SPMonitoredScope class.  Simply, put the code in question that you want to monitor inside a using block and SPMonitoredScope does the rest.  For today’s example, I want to monitor the performance of some code that inserts an item into a list.  Here is what it looks like.

using (SPMonitoredScope monitoredScope = new SPMonitoredScope("My Monitored Scope"))

{

    // put code to monitor performance on here

    SPList testList = site.Lists.TryGetList("Test List");

    if (testList != null)

    {

        SPListItem listItem = testList.Items.Add();

        listItem["Title"] = string.Format("Test Item {0}", Guid.NewGuid().ToString());

        listItem["City"] = "Somewhere";

        listItem["Quantity"] = 3;

        listItem.Update();

    }

}

I just give the SPMonitoredScope a name and put my code inside the using block.  Don’t you just love TryGetList?  This code assumes I already have access to a SPWeb.  In this case I am running it inside a Visual Web Part and I left out some of the other code involved.  When we view the page and have the developer dashboard running it looks like this.

SPPerformanceMonitorDashboard1

There you can see My Monitored Scope in the list at a runtime of 2442.77 ms.  Wow, that is some poorly performing code.  It would be nice if we could get some more detail.  Well what is cool is that you can actually nest using blocks to get performance data at a more granular level. 

using (SPMonitoredScope monitoredScope = new SPMonitoredScope("My Monitored Scope"))

{

    SPList testList;

 

    using (SPMonitoredScope getListMonitor = new SPMonitoredScope("Get List"))

    {

        testList = site.Lists.TryGetList("Test List");

    }

 

    using (SPMonitoredScope addListItemMonitor = new SPMonitoredScope("Add List Item"))

    {

        if (testList != null)

        {

            SPListItem listItem = testList.Items.Add();

            listItem["Title"] = string.Format("Test Item {0}", Guid.NewGuid().ToString());

            listItem["City"] = "Somewhere";

            listItem["Quantity"] = 3;

            listItem.Update();

        }

    }

}

Now, I have added blocks to track the time it takes to get an instance of the list as well as the time it takes to add the item.

SPMonitoredScopeDeveloperDashboard2

From here we can see that the bulk of the time spent is on adding the list item.  Almost a full two seconds.  You have to love virtual machines. :-)  I think this class is very powerful and I can see it quickly becoming a best practice to having one or more SPMonitoredScope wrapped around the code you are executing.  Start using it today!

Comments

# Twitter Trackbacks for Using SPMonitoredScope to monitor performance of your code - Corey Roth - DotNetMafia.com - Tip of the Day [dotnetmafia.com] on Topsy.com

Pingback from  Twitter Trackbacks for                 Using SPMonitoredScope to monitor performance of your code - Corey Roth - DotNetMafia.com - Tip of the Day         [dotnetmafia.com]        on Topsy.com

# re: Using SPMonitoredScope to monitor performance of your code

Saturday, May 29, 2010 1:34 PM by 3Peso

Just a suggestion. May it be, that you use SPList.Items unintentionally? You should not use this property, execpt you want to get all items in the list back. This one of many SharePoint API flaws.

# re: Using SPMonitoredScope to monitor performance of your code

Monday, June 28, 2010 8:43 PM by Eric Fang

Good tip!    Thanks, Corey!

# re: Using SPMonitoredScope to monitor performance of your code

Monday, April 4, 2011 4:42 PM by Moutasem al-awa

Nice one i liked it and its easy to be used.

# My resource to learn on 70-573 « martinbodocky

Wednesday, May 11, 2011 5:23 AM by My resource to learn on 70-573 « martinbodocky

Pingback from  My resource to learn on 70-573   « martinbodocky

# Instrumentação com SharePoint 2010 « Fabian Andr?? Gehrke

Pingback from  Instrumentação com SharePoint 2010 « Fabian Andr?? Gehrke

# Instrumentação com SharePoint 2010 « SharePointersBR

Monday, September 5, 2011 7:31 PM by Instrumentação com SharePoint 2010 « SharePointersBR

Pingback from  Instrumentação com SharePoint 2010 « SharePointersBR

# re: Using SPMonitoredScope to monitor performance of your code

Monday, August 5, 2013 1:16 PM by webguynj

Should references to spMonitoredScope be removed from production deployment?  Is there a production performance impact?

# re: Using SPMonitoredScope to monitor performance of your code

Monday, August 5, 2013 1:26 PM by CoreyRoth

@webguynj I would probably say no.  If you look at the developer dashboard, you will see that SPMonitoredScope is used throughout it to monitor it's internal calls to things.  It's good data to have when it is in production.  Does it affect performance?  Probably to a degree but I would think it is negligible.

# Instrumentação com SharePoint 2010

Friday, November 29, 2013 5:35 AM by Fabian André Gehrke

Instrumentação, de acordo com a Wikipédia , significa “ a ciência que estuda, desenvolve e aplica instrumentos

# Instrumenta????o com SharePoint 2010 | MSDN Blogs

Friday, November 29, 2013 10:59 AM by Instrumenta????o com SharePoint 2010 | MSDN Blogs

Pingback from  Instrumenta????o com SharePoint 2010 | MSDN Blogs

# Instrumenta????o com SharePoint 2010 | MSDN Blogs

Friday, November 29, 2013 11:59 AM by Instrumenta????o com SharePoint 2010 | MSDN Blogs

Pingback from  Instrumenta????o com SharePoint 2010 | MSDN Blogs

Leave a Comment

(required) 
(required) 
(optional)
(required)