Performance Monitoring Class

Thought I’d share a class I use for performance monitoring to help identify areas of code that are causing bottlenecks or take a lengthy time to execute.

In SharePoint we can use the SPMonitoredScope class to wrap areas of code that we want to be monitored.

using (new SPMonitoredScope("SharePoint Performance"))
{
 // SharePoint related code
 //
}

This will output performance logging statistics to the developer dashboard (if enabled) and to the ULS.

For general SharePoint performance logging, SPMonitoredScope is ideal but it only logs methods calls to the SharePoint databases. The class I developed allows you to monitor any method, whether it’s SharePoint related or not.

Areas you might use the custom performance monitoring class would include calls to a custom database, web services or any potentially long running method calls.

Click here to download the performance monitoring class.

The class has the following public methods:

EnterMethodBlock
Call this to initiate monitoring for the current method.

EndMethodBlock
Call this to end monitoring of the current method.

LogPerformanceDetails
Call this to log performance details for the current execution point of the executing method.

EndMonitoring
Call this to end monitoring and log performance details to the configured logger, effectively resets all monitoring counters for the current thread.

ResetMonitor
Call this to end monitoring, similar to EndMonitoring but without writing anything to the configured logger.

An example usage of the class is:

private void UpdateDatabase()
{
 PerformanceMonitor.EnterMethodBlock();
 
 // code that updates a custom database table
 //
 //
 PerformanceMonitor.LogPerformanceDetails("Post update");
 
 // Process table rows
 //
 //
 PerformanceMonitor.LogPerformanceDetails("Post process");
 
 PerformanceMonitor.EndMethodBlock();
}

Checking the output, to whereever you configure the log4net component of the class to write to, you’ll see entries similar to:

“7 ‘UpdateDatabaseClassName’ ‘UpdateDatabase’ ‘Enter’ method elapsed time: 1.0001 Overall elapsed time: 1.0001”
“7 ‘UpdateDatabaseClassName’ ‘UpdateDatabase’ ‘Post update’ method elapsed time: 1577.1447 Overall elapsed time: 1579.1449”
“7 ‘UpdateDatabaseClassName’ ‘UpdateDatabase’ ‘Post process’ method elapsed time: 2002.1901 Overall elapsed time: 2002.1903”
“7 ‘UpdateDatabaseClassName’ ‘UpdateDatabase’ ‘Exit’ method elapsed time: 2008.1907 Overall elapsed time: 2010.1909”

Each performance entry writes the current thread id, (in this example it’s 7), the name of the class and method name executing and then performance time data.

So from the above, it’s easy to identify that the database update part of the UpdateDatabase method is taking 1.5 seconds to process and the processing of table rows half a second.

The above example is only monitoring a single method, whereas in the majority of cases, you’d have multiple methods being called and monitored. When doing this, add a call to EndMonitoring at the logical point where the code you want to monitor has completed.

private void Process()
{
 PerformanceMonitor.EnterMethodBlock();
 
 DataTable data = GetData();
 UpdateDatabase(data);
 
 PerformanceMonitor.EndMethodBlock();
 PerformanceMonitor.EndMonitoring();
}

private DataTable GetData()
{
 PerformanceMonitor.EnterMethodBlock();
 
 DataTable data
 // populate data variable
 //
 PerformanceMonitor.LogPerformanceDetails("Got data");
 
 PerformanceMonitor.EndMonitoring();
 
 return data;
}

private void UpdateDatabase(DataTable data)
{
 PerformanceMonitor.EnterMethodBlock();
 
 // code that updates a custom database table
 //
 //
 PerformanceMonitor.LogPerformanceDetails("Post update");
 
 PerformanceMonitor.EndMonitoring();
}

Click here to download the performance monitoring class.

This entry was posted in SharePoint and tagged , . Bookmark the permalink.
0 0 votes
Article Rating
Subscribe
Notify of
guest

Solve the maths problem shown below before posting: *

0 Comments
Inline Feedbacks
View all comments