Troubleshooting and Monitoring Performance with the Developer Dashboard

Hour 3 talked about the usefulness of the developer dashboard for diagnosis and troubleshooting server side code. The developer dashboard provides a number of performance counters and diagnostic information sections and can be set up in few simple steps. This section looks at some of the advanced concepts related to the developer dashboard, including creating custom monitored scopes and working with performance counters.

Next let’s look at a common scenario, where as a developer you want to monitor usage statistics for your code to determine any performance bottlenecks. SharePoint 2010 introduces the SPMonitoredScope class to assist you with this.

Using the SPMonitoredScope class you can create your own monitored scopes to get usage statistics and performance counters for your custom code. All you need to do is wrap the section of code to be monitored in an SPMonitoredScope block, as shown in the following code:

// Using SPMonitoredScope to monitor performance
using (SPMonitoredScope monitoredScope =
new SPMonitoredScope("My Monitored Scope"))
{
    TimeConsumingOperation();
}

Redeploy the web part and examine the developer dashboard contents. You should now have the My Monitored Scope being displayed on the dashboard, as shown in Figure 7.12.

Image

Figure 7.12. Creating a custom monitored scope

There are a number of performance counters that you can use with the SPMonitoredScope class to measure execution time (via the SPExecutionTimeCounter class), number of SPRequest objects (via the SPRequestUsageCounter class), and the number of SharePoint SQL Server queries (via the SPSqlQueryCounter class).

For example, the following code uses the SPExecutionTimeCounter class to display a critical message to the user if the TimeConsumingOperation takes more than 2 seconds to execute:

// Using SPMonitoredScope to monitor performance with performance counters
SPExecutionTimeCounter timeCounter = new SPExecutionTimeCounter(2000);
using (SPMonitoredScope monitoredScope =
        new SPMonitoredScope("My Monitored Scope", 2000, timeCounter))
{
    TimeConsumingOperation();

    if (timeCounter.ValueIsExcessive)
    {
        string message = String.Format(
                         "Operation TimeConsumingOperation"+
                         "expected to take {0}ms time"+
                         "took {1}ms time.",
                                timeCounter.MaximumValue,
                                timeCounter.Value);

        SPCriticalTraceCounter.AddDataToScope(
                               1,
                               "Performance Monitoring",
                               1,
                               message);
    }
}

Since the TimeConsumingOperation takes around 5 seconds to execute, the developer dashboard displays the Critical Performance Monitoring message in the Asserts and Critical Events section, as shown in Figure 7.13.

Image

Figure 7.13. Using performance counters

On clicking the Critical Performance Monitoring message, you get further details related to the event, as shown in Figure 7.14.

Image

Figure 7.14. Logging to the developer dashboard using the SPCriticalTraceCounter.AddDataToScope method

To write the performance monitoring related information to the developer dashboard’s Asserts and Critical Events section, we use the SPCriticalTraceCounter class’s AddDataToScope method in the preceding code. The method requires a TagID, Category, TraceLevel, and Message as parameters.


By the Way

SPMonitoredScope does not work with sandboxed solutions. Even if you try to use the SPMonitoredScope in a sandboxed solution, no related information or statistics will appear in the developer dashboard.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.135